package com.saucelabs.common;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.saucelabs.saucerest.DataCenter;
import com.saucelabs.saucerest.SauceREST;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

import java.net.MalformedURLException;
import java.net.URL;


public class SauceHelperAcceptanceTest
{
    private String username = System.getenv("SAUCE_USERNAME");
    private String accesskey = System.getenv("SAUCE_ACCESS_KEY");
    @Test
    public void shouldPassTest() throws MalformedURLException {
        ChromeOptions caps = new ChromeOptions();
        caps.setCapability("version", "72.0");
        caps.setCapability("platform", "Windows 10");
        caps.setExperimentalOption("w3c", true);

        MutableCapabilities sauceOptions = new MutableCapabilities();
        sauceOptions.setCapability("username", username);
        sauceOptions.setCapability("accessKey", accesskey);
        sauceOptions.setCapability("seleniumVersion", "3.141.59");
        sauceOptions.setCapability("name", "shouldPassTest");

        caps.setCapability("sauce:options", sauceOptions);
        String SAUCE_REMOTE_URL = "https://ondemand.saucelabs.com/wd/hub";
        WebDriver driver = new RemoteWebDriver(new URL(SAUCE_REMOTE_URL), caps);
        SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
        driver.navigate().to("https://www.saucedemo.com");
        SauceHelper sauceHelper = new SauceHelper(driver);
        sauceHelper.setTestStatus("passed");

        driver.quit();
        SauceREST sauceRest = new SauceREST(username, accesskey, DataCenter.US);
        String job = sauceRest.getJobInfo(sessionId.toString());
        JsonElement jsonArray = new JsonParser().parse(job);
        Assert.assertEquals(true, ((JsonObject) jsonArray).get("passed").getAsBoolean());
    }
    //TODO need a test that doesn't set the test status with JS executor, in which case "passed" should be null
    //TODO need a test that makes sure that the JS executor from Selenium works as expected
//    @Test
//    public void shouldSetTestToPassUsingJSExecutor() throws MalformedURLException {
//        ChromeOptions caps = new ChromeOptions();
//        caps.setCapability("version", "72.0");
//        caps.setCapability("platform", "Windows 10");
//        caps.setExperimentalOption("w3c", true);
//
//        MutableCapabilities sauceOptions = new MutableCapabilities();
//        sauceOptions.setCapability("username", username);
//        sauceOptions.setCapability("accessKey", accesskey);
//        sauceOptions.setCapability("seleniumVersion", "3.141.59");
//        sauceOptions.setCapability("name", "shouldPassTest");
//
//        caps.setCapability("sauce:options", sauceOptions);
//        String SAUCE_REMOTE_URL = "https://ondemand.saucelabs.com/wd/hub";
//        WebDriver driver = new RemoteWebDriver(new URL(SAUCE_REMOTE_URL), caps);
//        SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
//        driver.navigate().to("https://www.saucedemo.com");
//        SauceHelper sauceHelper = new SauceHelper(driver);
//
//        //((JavascriptExecutor)driver).executeScript("sauce:job-result=passed");
//        //sauceHelper.setTestStatus("passed");
//        SauceREST sauceRest = new SauceREST(username, accesskey, DataCenter.US);
//        String job = sauceRest.getJobInfo(sessionId.toString());
////        Gson gson = new Gson();
////        String json = gson.toJson(job);
////        String[] jsonArr = gson.fromJson(json, String[].class);
//        //Assert.assertEquals();
//        driver.quit();
//    }
}

@@ -799,24 +799,96 @@
 CESS_KEY%22);%0A
+    String SAUCE_REMOTE_URL = %22https://ondemand.saucelabs.com/wd/hub%22;%0A%0A
     @Test%0A  
@@ -907,24 +907,37 @@
 d should
-PassTest
+SetTestStatusToPassed
 () throw
@@ -983,36 +983,35 @@
 eOptions caps = 
-new 
+get
 ChromeOptions();
@@ -1023,601 +1023,127 @@
     
-caps.setCapability(%22version%22, %2272.0%22);%0A        caps.setCapability(%22platform%22, %22Windows 10%22);%0A        caps.setExperimentalOption(%22w3c%22, true);%0A%0A        MutableCapabilities sauceOptions = new MutableCapabilities();%0A        sauceOptions.setCapability(%22username%22, username);%0A        sauceOptions.setCapability(%22accessKey%22, accesskey);%0A        sauceOptions.setCapability(%22seleniumVersion%22, %223.141.59%22);%0A        sauceOptions.setCapability(%22name%22, %22shouldPassTest%22);%0A%0A        caps.setCapability(%22sauce:options%22, sauceOptions);%0A        String SAUCE_REMOTE_URL = %22https://ondemand.saucelabs.com/wd/hub%22
+MutableCapabilities sauceOptions = getMutableCapabilities();%0A%0A        caps.setCapability(%22sauce:options%22, sauceOptions)
 ;%0A  
@@ -1290,24 +1290,25 @@
 essionId();%0A
+%0A
         driv
@@ -1458,17 +1458,16 @@
 ssed%22);%0A
-%0A
         
@@ -1477,24 +1477,25 @@
 ver.quit();%0A
+%0A
         Sauc
@@ -1784,16 +1784,739 @@
 );%0A    %7D
+%0A%0A    private MutableCapabilities getMutableCapabilities() %7B%0A        MutableCapabilities sauceOptions;%0A        sauceOptions = new MutableCapabilities();%0A        sauceOptions.setCapability(%22username%22, username);%0A        sauceOptions.setCapability(%22accessKey%22, accesskey);%0A        sauceOptions.setCapability(%22seleniumVersion%22, %223.141.59%22);%0A        sauceOptions.setCapability(%22name%22, %22shouldPassTest%22);%0A        return sauceOptions;%0A    %7D%0A%0A    private ChromeOptions getChromeOptions() %7B%0A        ChromeOptions caps;%0A        caps = new ChromeOptions();%0A        caps.setCapability(%22version%22, %2272.0%22);%0A        caps.setCapability(%22platform%22, %22Windows 10%22);%0A        caps.setExperimentalOption(%22w3c%22, true);%0A        return caps;%0A    %7D
 %0A    //T

Clean up acceptance test to extract setup into methods