I am trying to run my automated tests(Selenium webdriver) in parallel using testng. this is the node which I am running:
java -Dwebdriver.gecko.driver=chromedriver.exe -jar selenium-server-standalone-3.4.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=chrome,maxInstances=2 -maxSession 2
this is my test class:
public class TestParallel {
Login login;
//@BeforeMethod(alwaysRun = true)
public SeleniumDriverCore testSetup() throws FileNotFoundException, IOException{
SeleniumDriverCore driver = new SeleniumDriverCore("config/chromeDriverConfig");
Properties config = new Properties();
config.load(new FileInputStream("config/testConfig"));
this.login = new Login(driver);
driver.browser.open("https://test.test.xyz");
driver.browser.maximize();
driver.waits.waitForPageToLoad();
return driver;
}
@Test(groups={"parallel"})
public void test_one() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}
@Test(groups={"parallel"})
public void test_two() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}
@Test(groups={"parallel"})
public void test_three() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}
@Test(groups={"parallel"})
public void test_four() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}
public void testCleanup(SeleniumDriverCore driver){
driver.close();
driver.quit();
}
}
and here is my xml:
<suite name="Ontega - All Tests Mobile" parallel="methods" thread-count="2">
<test name="Ontega - All Tests Mobile">
<groups>
<run>
<include name="parallel"/>
<exclude name="open-defects"/>
</run>
</groups>
<packages>
<package name="tests.*"/>
</packages>
</test>
</suite>
when I run the XML, I expect to have my tests to be running on two browsers in two threads at a time, however when I run the XML I get two browser instances running at the first time and then they are incremented and 50% of the tests are failing, as you can see I am trying to instantiate the driver in each of my methods, although it's not how my framework is working, but I am trying to get to the bottleneck of this issue. Any help would be very appreciated Thanks in advance
It seems you creating multiple driver per test. I guess you need to keep some part of code outside testSetup() method
I am sharing part of my code which I am using for parallel testing, may be that will help in your case
My xml
Here are some ways of doing this in TestNG. You basically manage your webdriver instantiation and cleanup via a
@BeforeMethod
and a@AfterMethod
config methods. So then you would need to decide how would you want to share the created webdriver instance with your@Test
method. For that you have three options:ThreadLocal
variant, because TestNG guarantees to you that it will execute@BeforeMethod
,@Test
and@AfterMethod
all in the same thread.Here's a sample that shows you this in action
ITestResult
object. Here's a sample that shows that in action.org.testng.IInvokedMethodListener
which sets the created webdriver into theITestResult
(as shown in option 2) or into aThreadLocal
(as shown in option 1). You can find more details about this option along with code snippets in my blog post.