I'm using Data Provider to pass the browsers combinations from Jenkins to the json objects. Please help what is wrong with that method.
My project is on Java, with TestNg and Maven.
package com.***.tests;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
public class RemoteSauceOnDemandBrowser {
protected WebDriver driver;
public static final String SAUCE_ACCESS_KEY =
System.getenv("SAUCE_ACCESS_KEY");
public static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
@DataProvider(name = "hardCodedBrowsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) throws
JsonException {
String browsersJSONArrayString =
System.getenv("SAUCE_ONDEMAND_BROWSERS");
JsonArray browsersJSONArrayObj = new JsonArray(browsersJSONArrayString);
//browsersJSONArrayString on new JsonArray(browsersJSONArrayString); - is underlined with JsonArray (int) in JsonArray can't be applied to java.lang.String
Object[][] browserObjArray = new Object[browsersJSONArrayObj.length()]
[3];
//length is underlined with can't resolve method length
for (int i=0; i<browsersJSONArrayObj.length(); i++)
//length is underlined with can't resolve method length
{
JsonObject browserObj =
(JsonObject)browsersJSONArrayObj.getAsJsonObject(i);
// i - is underlined with getAsJsonObject in JsonElement can't be applied to (int)
browserObjArray[i] = new Object[]{browserObj.get("browser"), browserObj.get("browser-version"), browserObj.get("os")};
}
return browserObjArray;
}
private void createDriver(String browser, String version, String os, String methodName) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", browser);
if (version != null) {
capabilities.setCapability(CapabilityType.VERSION, version);
}
capabilities.setCapability("platform", os);
String jobName = methodName + '_' + os + '_' + browser + '_' + version;
capabilities.setCapability("name", jobName);
driver = (new RemoteWebDriver(new URL("http://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"), capabilities));
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
Updated pom.xml with a new dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<scope>test</scope>
</dependency>
I'm referring to this example: https://github.com/KevinMarkVI/Java-TestNG-Jenkins-Selenium/blob/master/src/test/java/com/yourcompany/SampleSauceTest.java