package com.specmate.uitests;

import com.saucelabs.common.SauceOnDemandAuthentication;

import org.junit.*;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

//import com.saucelabs.junit.SauceOnDemandTestWatcher;
import com.specmate.uitests.pagemodel.LoginElements;

import java.net.URL;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

import com.saucelabs.common.SauceOnDemandSessionIdProvider;

@Ignore
@RunWith(Parameterized.class)
public class TestBase implements SauceOnDemandSessionIdProvider {
	public static String username = System.getenv("SAUCE_USERNAME");
    public static String accesskey = System.getenv("SAUCE_ACCESS_KEY");
    public static String seleniumURI;
    public static String buildTag;
    public static final String tunnelidentifier = System.getenv("TRAVIS_JOB_NUMBER");
   
    //public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication();

    /**Mark the Sauce Job as passed/failed when the test succeeds or fails*/
    //@Rule
    //public SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication);

    @Rule
    public TestName name = new TestName() {
        public String getMethodName() {
            return String.format("%s", super.getMethodName());
        }
    };

    protected String browser;
    protected String os;
    protected String version;
    protected String deviceName;
    protected String deviceOrientation;
    protected String sessionId;
    protected WebDriver driver;

 
    /**Constructor for test instances*/
    public TestBase(String os, String version, String browser, String deviceName, String deviceOrientation) {
        super();
        this.os = os;
        this.version = version;
        this.browser = browser;
        this.deviceName = deviceName;
        this.deviceOrientation = deviceOrientation;
    }

    /**Browser configurations*/
    @Parameters
    public static LinkedList<String[]> browsersStrings() {
        LinkedList<String[]> browsers = new LinkedList<String[]>();

        browsers.add(new String[]{"Windows 10", "59.0", "Chrome", null, null});
        //browsers.add(new String[]{"Windows 10", "14.14393", "MicrosoftEdge", null, null});
        //browsers.add(new String[]{"Windows 10", "11.0", "internet explorer", null, null});
        return browsers;
    }

 
    @Before
    public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities(); 

        capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
        capabilities.setCapability(CapabilityType.VERSION, version);
        capabilities.setCapability("deviceName", deviceName);
        capabilities.setCapability("device-orientation", deviceOrientation);
        capabilities.setCapability("platform", os);
        capabilities.setCapability("tunnel-identifier", tunnelidentifier); 

        String methodName = name.getMethodName();
        capabilities.setCapability("name", methodName);

        if (buildTag != null) {
            capabilities.setCapability("build", buildTag);
        }
        this.driver = new RemoteWebDriver(new URL("https://" + username+ ":" + accesskey + seleniumURI +"/wd/hub"), capabilities);
        
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

        this.sessionId = (((RemoteWebDriver) driver).getSessionId()).toString();
    }

    @After
    public void tearDown() throws Exception {
    	((JavascriptExecutor)driver).executeScript("sauce:job-result=passed");
        driver.quit();
    }

    public String getSessionId() {
        return sessionId;
    }

    @BeforeClass
    public static void setupClass() {
        // Get the uri to send the commands to
        seleniumURI = "@ondemand.saucelabs.com:443";
        
        // Set the buildTag to the Travis Build number 
        buildTag = System.getenv("TRAVIS_BUILD_NUMBER");
        if (buildTag == null) {
            buildTag = System.getenv("SAUCE_BUILD_NAME");
        }
    }
    
	public void performLogin(LoginElements login) {
		login.username("username");
		login.password("password");
		login.changeToEnglish();
		login.changeToGerman();
		login.changeToProject("test-data");
		login.login();
	}
}

@@ -28,65 +28,61 @@
 s;%0A%0A
+%0A
 import 
-com.saucelabs.common.SauceOnDemandAuthentication;%0A
+org.junit.*;%0Aimport org.junit.rules.TestName;
 %0Aimp
@@ -87,33 +87,49 @@
 mport org.junit.
-*
+rules.TestWatcher
 ;%0Aimport org.jun
@@ -129,36 +129,40 @@
 org.junit.ru
-les.TestName
+nner.Description
 ;%0Aimport org
@@ -275,16 +275,58 @@
 meters;%0A
+import org.junit.runners.model.Statement;%0A
 import o
@@ -1226,309 +1226,429 @@
 %0A   
-%0A    //public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication();%0A%0A    /**Mark the Sauce Job as passed/failed when the test succeeds or fails*/%0A    //@Rule%0A    //public SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication)
+ private static boolean result;%0A%0A    %0A    @Rule%0A%09public final TestWatcher watcher = new TestWatcher() %7B%0A%09%09@Override%0A%09%09public Statement apply(Statement base, Description description) %7B%0A%09%09%09return super.apply(base, description);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected void failed(Throwable e, Description description) %7B%0A%09%09%09result = false;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected void succeeded(Description description) %7B%0A%09%09%09result = true;%0A%09%09%7D%0A%09%7D
 ;%0A%0A 
@@ -4121,17 +4121,50 @@
 ult=
-passed%22);
+%22 + (result ? %22passed%22 : %22failed%22));%0A    %09
 %0A   

Test Watcher