0

There is a situation where I have 150 test cases and I have set up Test NG parallel test execution. Unfortunately, I'm using the same web driver which initialized at the test base class on all the tests. Because of that, I believe my tests are getting failed. So is there any way to avoid this? Execute parallel test by using the same WebDriver? And I'm on IntelliJ Java.

Below is the sample code:

public class AppTest {
// driver;
String baseURL, nodeURL;
WebDriver driver;

@AfterTest
public void afterTest() {

}
@Test
public void sampleTest() throws MalformedURLException {

    System.setProperty("webdriver.chrome.driver", "C:\\SeleniumGrid\\chromedriver.exe");

    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.phptravels.net/");
    String Email = "cjay" + Math.random() + "[email protected]";
    String UserName = "Chathura";

    //        driver.findElement(By.xpath("/html/body/header/div[2]/div/div/div[2]/div/div/a[2]"));

    driver.findElement(By.xpath("//*[@class=\"theme-btn theme-btn-small waves-effect\"]")).click();

    driver.findElement(By.name("first_name")).sendKeys(UserName);
    driver.findElement(By.name("last_name")).sendKeys("Jayasinghe");
    driver.findElement(By.name("phone")).sendKeys("12321231");
    driver.findElement(By.name("email")).sendKeys(Email);
    driver.findElement(By.name("password")).sendKeys("Email");
    driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();

    driver.findElement(By.name("email")).sendKeys(Email);
    driver.findElement(By.name("password")).sendKeys("Email");
    driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();

    String name = driver.findElement(By.className("author__title")).getText();
    System.out.println(name);

    Assert.assertEquals(name, UserName);
    driver.quit();
}

@Test
public void sampleTest2() throws MalformedURLException, InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\\SeleniumGrid\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    Thread.sleep(4000);
    driver.get("https://www.phptravels.net/");
    String Email = "cjay" + Math.random() + "[email protected]";
    String UserName = "Chathura";

    // driver.findElement(By.xpath("/html/body/header/div[2]/div/div/div[2]/div/div/a[2]"));

    driver.findElement(By.xpath("//*[@class=\"theme-btn theme-btn-small waves-effect\"]")).click();

    driver.findElement(By.name("first_name")).sendKeys(UserName);
    driver.findElement(By.name("last_name")).sendKeys("Jayasinghe");
    driver.findElement(By.name("phone")).sendKeys("12321231");
    driver.findElement(By.name("email")).sendKeys(Email);
    driver.findElement(By.name("password")).sendKeys("Email");
    driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();

    driver.findElement(By.name("email")).sendKeys(Email);
    driver.findElement(By.name("password")).sendKeys("Email");
    Thread.sleep(4000);
    driver.findElement(By.xpath("//*[@class=\"btn btn-default btn-lg btn-block effect ladda-button waves-effect\"]")).click();

    String name = driver.findElement(By.className("author__title")).getText();
    System.out.println(name);

    Assert.assertEquals(name, UserName);
    driver.quit();
}
1
  • Can you show that part of your code where you set up your web driver?
    – Alexey R.
    Aug 18, 2021 at 10:21

1 Answer 1

1

Using the same webdriver for all tests in parallel execution defeats the purpose of concurrency. When you are using the same webdriver for two parallel tests, then webdriver calls will overlap for both tests. So the execution flow will go like, webdriver will execute some steps for test-1 followed by some steps for test-2, then again some steps for test-1.

Rather than the above, a better approach is to have every test have its own webdriver.

Sample code:

public abstract class BaseTest {

Webdriver webdriver;

@BeforeMethod
public void setup(){
    webdriver = intializeWebdriver();
}

@AfterMethod
public void tearDown(){
    webdriver.quit();
}

}

public class SampleTest extends BaseTest{

@Test
public void sampleTest1(){
    //test steps
}

@Test
public void sampleTest2(){
    //test steps
}

}

Using this approach you will make sure that before every test its webdriver is initialised and after test is finished, its webdriver is cleaned up. Therefore, two tests won't have any conflicts when running in-parallel, and better software testing solutions are achieved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.