lundi 30 juillet 2018

Selenium Java random select username for saucelabs cross browser execution

Project info: java, selenium, testng, maven. Running from Jenkins on saucelabs.

I have the method to select randomly the users name from the string of users:

public class RandomDataSelect {

public static String selectRandomUser(){

    String[] users = {"user1", "user2"};
    return users[(int) (Math.random()* users.length)];
}

and pass each username to each browser while the cross browser and parallel execution in saucelabs. The idea that each browser: chrome and FF should take one username from the list and perform the login.

Currently the issue is that one browser takes 2 usernames, placed them into login field and trying to login with them. For example: login "user1user2". Must be "user1" or "user2".

In the same time , chrome browser didn't get any command to enter the input.

Please, help me to figure out what is the issue.

The code of the main test:

 public class Login extends TestBase{


 @Test(dataProvider = "browsers")
 public void Login(String browser, String version, String os, Method method) throws Exception {

 this.createRemoteDriver(browser, version, os, method.getName());
 WebDriver driver = getWebDriver();

 System.out.println("Driver: " + driver.toString());

 app = new Application(driver);
 configRead = new ConfigFileReader();

        VisitPage page = VisitPage.getURL(driver);
        this.annotate("Visiting page...");

        app.loginPage().Login_as(RandomDataSelect.selectRandomUser(), configRead.getPassword());
        this.annotate("Submit login");

        driver.close();
        this.annotate("Driver close");
}

}

The code of the TestBase class:

 public class TestBase {

private WebDriver driver;

protected Application app;
RandomDataSelect randomuser;
ConfigFileReader configRead;
private static String baseUrl;
private PropertyLoader propertyRead;

private static final String SAUCE_ACCESS_KEY=System.getenv("SAUCE_ACCESS_KEY"
);
private static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");

private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
private ThreadLocal<String> sessionId = new ThreadLocal<String>();



public  WebDriver getWebDriver() {
    return webDriver.get();
}

private void setWebDriver(WebDriver driver) {
    webDriver.set(driver);
}

public String getSessionId() {
    return sessionId.get();
}

@DataProvider(name = "browsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) throws 
JSONException {

    String browsersJSONArrayString  = 
    System.getenv("SAUCE_ONDEMAND_BROWSERS");
    System.out.println(browsersJSONArrayString);
    JSONArray browsersJSONArrayObj = new JSONArray(browsersJSONArrayString);

    Object[][] browserObjArray = new Object[browsersJSONArrayObj.length()] 
    [3];
    for (int i=0; i < browsersJSONArrayObj.length(); i++) {
        JSONObject browserObj = 
    (JSONObject)browsersJSONArrayObj.getJSONObject(i);
        browserObjArray[i] = new Object[]{browserObj.getString("browser"), 
    browserObj.getString("browser-version"), browserObj.getString("os")};
    }
    return browserObjArray;
   }


void createRemoteDriver(String browser, String version, String os, String 
methodName) throws Exception {


    DesiredCapabilities capabilities = new DesiredCapabilities();

    Class<? extends RemoteSauceOnDemandBrowser> SLclass = this.getClass();

    capabilities.setCapability("browserName", browser);
    if (version != null) {
        capabilities.setCapability("browser-version", version);
    }
    capabilities.setCapability("platform", os);
    capabilities.setCapability("name", SLclass.getSimpleName());
    capabilities.setCapability("tunnelIdentifier", "hdsupply");

    capabilities.setCapability("idleTimeout",100);


    this.webDriver.set(new RemoteWebDriver(new URL
            ("http://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + 
"@ondemand.saucelabs.com:80/wd/hub"), capabilities));


   String jobName = methodName + '_' + os + '_' + browser + '_' + version;
   capabilities.setCapability("name", jobName);

    // set current sessionId
    String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString();
    sessionId.set(id);


    randomuser = new RandomDataSelect();
    configRead = new ConfigFileReader();
}

private void printSessionId() {

    String message = String.format("SauceOnDemandSessionID=%1$s job- 
name=%2$s",
            (((RemoteWebDriver) driver).getSessionId()).toString(), "some job 
name");
    System.out.println(message);
}



@AfterMethod(description = "Throw the test execution results into saucelabs")

public void tearDown(ITestResult result) {
    ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" 
+ (result.isSuccess() ? "passed" : "failed"));
    printSessionId();
    webDriver.get().quit();
}

void annotate(String text) {
    ((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + 
text);
  }
}




Aucun commentaire:

Enregistrer un commentaire