1
votes

I 'm using testNg + selenium grid for my test session and i'm very happy with the whole stuff The point is that the number of tests is growing and now they need at least 3 hrs to be executed ( +-100 tests on thre browsers) I plan to have +-150 tests so, as You can imagine , I must find a solution and start them in parallel I tried to play a bit with this but no luck for now, what I see right now is that two browsers start but only one go through the test .The other one just wait :-) The structure of the test is like the one below, basically 1 test=1 class. I have a @BeforeClass ( start driver) and a @AfterClass (stop the driver) before/after every test

testNG.xml -> i changed parallel= to classes,methods,tests but no luck.Also changed thread-count value !

    <test name="Smoke Test in IE - IE REMOTE" preserve-order="true">
        <parameter name="browser" value="IE" ></parameter> 
            <classes>

                <class name="com.TestThis.Script1" />
                <class name="com.TestThis.Script2" />
            </classes>
    </test>

TEST CLASS LOOKS LIKE THIS public class Script1 extends SelTestCase {

try {   
    GlobalHelp.LogIn("usr","pwd");
    customVerification.verifyTrue("Verify main page loaded( check download zone is visible!!)", ObjectHelp.isElementVisible(ObjectsPaths.Main_DownloadZone,120000)==true);
    customVerification.SoftVerifyEquals("Verify main page is displayed " , driver.getTitle(), "Application Stuff");
} catch (Exception e) {

    Reporter.log("from exception" + e.toString());          
    customVerification.verificationErrors.append(e);    
}
}

}

MODULE WITH BEFORE & AFTER LOOK LIKE THIS public class SelTestCase {

@BeforeSuite
@Parameters("appURL")
public void setEnv(
        @Optional(ObjectsPaths.MAIN_URL) String appURL) {
    this.appURL = appURL;   

}

@BeforeClass
@Parameters({ "browser", "HUBip" })
public void launchBrowser(@Optional("IE") String browser, @Optional("localhost") String HUBip) throws MalformedURLException {

    String brw=browser;
    String ip=HUBip;

    try {
         if (sBrw.equalsIgnoreCase("Chrome")) {
            URL url = new URL( "http","localhost",4444, "/wd/hub" );
            DesiredCapabilities caps=DesiredCapabilities.chrome();
            driver = new ScreenShotRemoteWebDriver(url, caps); 

        } else if (sBrw.equalsIgnoreCase("FF")) {
            URL url = new URL( "http", "localhost",4444, "/wd/hub")
            DesiredCapabilities caps=DesiredCapabilities.firefox(); 
        } else {
            URL url = new URL( "http", "localhost",4444, "/wd/hub" ); 
            DesiredCapabilities caps=DesiredCapabilities.internetExplorer();
            driver = new ScreenShotRemoteWebDriver(url, caps); 

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    };
    }
    try {
        Reporter.log("Test started at: " + ObjectHelp.getDate() +  "<p>");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    driver.manage().timeouts().implicitlyWait( ObjectHelp.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS );
    driver.manage().timeouts().pageLoadTimeout(ObjectHelp.PAGE_LOAD_TIMEOUT,TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.navigate().to("http://www.google.com");          

}


//TBD
@AfterClass
public void closeBrowser() throws Exception {
    //driver.close();
    QuitDriver();
}
3
what is your question ?Shamik
the question is how to make them run in parallel!user1944151
How are your test classes getting the driver? See I cant understand your code much. But lets be clear if you want parallel instances of tests to run ( in same or different browsers) They should get different isntances of driver. Your BeforeClass should create separate drivers and pass onto the test classes. Then only you can achieve what you want. Are you doing that ?Shamik
Hi, basically my test classes have before/after methods which start/stop the driver session. Before/After calls the requested procedure situated in another class. So, every test class at before/after calls the same function but i suppose multiple instances of the driver should be created at that point right?user1944151
Yes. So In before class you need to create an intance which will only be used by the test class which will be called after that. But if the same driver variable is over written in each before call then that implementation wont work for parallel execution. I think your before classes are not creating separate instances for each testclass.Shamik

3 Answers

0
votes

So what you can do is instead of creating driver objects in before class, create driver objects inside the test classes and implement a before method in the super class which will create a driver object for you test class and assign it to the driver object in your test class, this driver object will be specific to your test classes. Then if you try to run in parallel it will work.

0
votes

In your XML add following to run test parallel :

parallel="tests" with your suite name , So like below :

 <suite name="Suite" parallel="tests" >

It will run test parallel in all your specified browsers.

0
votes

I ran into similar issue - where my parallel tests for iOS/Android devices were stepping on each-others' toes.

What I ended up doing was to identify all thread-specific variables; and declare them as ThreadLocal - like so

ThreadLocal<MyDataModel> threadLocalModel = new ThreadLocal<>();

The MyDataModel would house all thread-specific variables.

After implementing this change, the toe-stepping is drastically minimized.

Also, in your testNG xml file, you might want to specify the number of threads for execution of the tests in parallel (using thread-count="nn") - like so...

<suite name="Parallel Mobile Tests" parallel="tests" thread-count="2"
    preserve-order="true" configfailurepolicy="continue">

Hope this helps.