0

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

Y_Sh
  • 121
  • 2
  • 4
  • 14

1 Answers1

0

You use not the same class that in your example, you should use JSONArray instead of JsonArray, JSONObject instead of JsonObject - https://prnt.sc/k5hh4f

  • by using the capital-case JSON it underlines each JSON string. Added the dependency into pom.xml, but the issue is still there. – Y_Sh Jul 12 '18 at 14:15
  • your example was downloaded by me prntscr.com/k5njgy I haven't any undelines. try to add imports: import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; – Sergiy Konoplyaniy Jul 12 '18 at 14:19
  • it doesn't help. It seems like json dependency is never used. Wondering if a json package should be added into the path – Y_Sh Jul 12 '18 at 15:28
  • Possibly exist some problems with IDEA read solution here https://stackoverflow.com/questions/9980869/force-intellij-idea-to-reread-all-maven-dependencies – Sergiy Konoplyaniy Jul 12 '18 at 18:05