R.Donohoe
R.Donohoe

Reputation: 59

TestNG parallel execution with single driver instance

In my test suite, I have a Base class in which I declare a static WebDriver, which is then generated as a ChromeDriver, FirefoxDriver, etc, in a method that opens a base url. This method is used in my BeforeMethod in all my test cases, therefore a single static instance of driver is used in every test execution, and throughout a range of reusable methods utilised within the test cases (for example, a log in method).

This seems to be presenting a problem now I am attempting to introduce parallel execution, as it appears I need separate instances of driver for any scripts I want to run simultaneously to avoid thread interference. Is there any way I can make my scripts run in parallel without re-structuring my whole suite so that each script is written completely independently?

I've tried declaring an array of WebDrivers instead, and then selecting a particular index based on a parameter sent from the launch suite XML, but this didn't help. The second browser opens up, but nothing happens in it.

This is my Base class:

public class Base {


public static WebDriver driver = null;

//Input client under test
public static final String Client = "Client1";

//Input default environment under test
public static final String DefaultEnvironment = "Chrome_Hub";


//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL(String environment) throws InterruptedException, IOException {

    try{

        if(environment.equals("Chrome_Hub")) {
            System.setProperty("webdriver.chrome.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/chromedriver");
            driver = new ChromeDriver();
        }
        if(environment.equals("Firefox_Hub")) {
            System.setProperty("webdriver.gecko.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/geckodriver");
            driver = new FirefoxDriver();
        }

        driver.get(DataFinder.ReadData("front end url"));


    }
    catch(Exception E) {
        E.printStackTrace();
    }
}
}

Here is an example script:

public class LoginLogoutScript extends Base {


  @Test (priority = 2)
  public void login() throws InterruptedException, IOException {
        LoginLogout.loginFrontEnd();
        Assert.assertTrue(driver.getPageSource().contains("My Account") || driver.getPageSource().contains("My Dashboard"));
  }

  @Parameters({ "Environment" })
  @BeforeClass
  public void setUp(@Optional(DefaultEnvironment) String Environment) throws InterruptedException, IOException {
      Base.openURL(Environment);
  }

  @AfterClass public void tearDown() { 
      Base.driver.quit(); 
  }

}

Here is the method called in that script:

public class LoginLogout extends Base {

    public static void loginFrontEnd () throws InterruptedException, IOException {
        Thread.sleep(5000);
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement login2 = wait.until(ExpectedConditions.elementToBeClickable(By.linkText(DataFinder.ReadData("sign in link"))));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click();", login2);
        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
        driver.findElement(By.id("email")).sendKeys(DataFinder.ReadData("username entry"));
        driver.findElement(By.id("pass")).sendKeys("I$$\"Cnewton1");
        Thread.sleep(5000);
        driver.findElement(By.id("send2")).click();
    }
}

Here is another script:

@Listeners(CustomListener.class)
public class CreateDeleteCustomerAccountScript extends Base {

  @Test (priority = 1)
  public void createAccount() throws InterruptedException, IOException {

        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText(DataFinder.ReadData("navigate to register account 1"))));
        driver.findElement(By.partialLinkText(DataFinder.ReadData("navigate to register account 1"))).click();

        driver.findElement(By.cssSelector(DataFinder.ReadData("navigate to register account 2"))).click();

        driver.findElement(By.name("firstname")).sendKeys("Testy");
        driver.findElement(By.name("lastname")).sendKeys("McTester");
        driver.findElement(By.cssSelector("#email_address")).sendKeys(Misc.generateRandomString() + "@thepixel.com");
        driver.findElement(By.name("password")).sendKeys("I$$\"Cnewton1");
        driver.findElement(By.name(DataFinder.ReadData("password confirmation"))).sendKeys("I$$\"Cnewton1");

            //verify account created
        Thread.sleep(5000);
        Assert.assertTrue(driver.findElement(By.tagName("body")).getText().contains(DataFinder.ReadData("account created message")));
  }

  @Parameters({ "Environment" })
  @BeforeClass
  public void setUp(@Optional(DefaultEnvironment) String Environment) throws InterruptedException, IOException {
      Base.openURL(Environment, "front end");
  }

  @AfterClass public void tearDown() { 
      Base.driver.quit(); 
  }
}

Here is my XML file:

<?xml version="1.0" encoding="UTF-8"?>

<suite name="FullRegressionSuite" parallel="tests" thread-count="2">
<listeners>

</listeners>

  <test name="Test1">
   <parameter name ="DriverNo" value="1"/>
    <classes>
      <class name="userManagement.LoginLogoutScript"/>
    </classes>
  </test>
  <test name="Test2">
    <parameter name ="DriverNo" value="2"/>
    <classes>
      <class name="userManagement.CreateDeleteCustomerAccountScript"/>"/>
    </classes>
  </test>

       <!-- Test -->
</suite> <!-- Suite -->

Upvotes: 0

Views: 1527

Answers (2)

Dmitri T
Dmitri T

Reputation: 168147

If you're executing your tests via Maven you can reconsider Fork Options so each TestNG thread will be executed in a separate JVM instance

<forkCount>2</forkCount> <!- amend this to be equal to the number of TestNG threads -->
<reuseForks>false</reuseForks>

However it only masks the problem, the bigger constraint is that you're using static modifier for WebDriver instance, it definitely violates Parallel Testing Best Practices

Upvotes: 2

Murthi
Murthi

Reputation: 5347

You can use threadlocal webdriver as given below.

public class Base {


public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

//Input client under test
public static final String Client = "Client1";

//Input default environment under test
public static final String DefaultEnvironment = "Chrome_Hub";


//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL(String environment) throws InterruptedException, IOException {

    try{

        if(environment.equals("Chrome_Hub")) {
            System.setProperty("webdriver.chrome.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/chromedriver");
            driver.set(new ChromeDriver());
        }
        if(environment.equals("Firefox_Hub")) {
            System.setProperty("webdriver.gecko.driver", "/Users/rossdonohoe/Desktop/SeleniumJava/Drivers/geckodriver");
            driver.set(new FirefoxDriver());
        }

        driver.get(DataFinder.ReadData("front end url"));


    }
    catch(Exception E) {
        E.printStackTrace();
    }
}
}

And you can use driver.get() method to get the instance of it.

public class LoginLogoutScript extends Base {


  @Test (priority = 2)
  public void login() throws InterruptedException, IOException {
        LoginLogout.loginFrontEnd();
        Assert.assertTrue(driver.getPageSource().contains("My Account") || driver.get().getPageSource().contains("My Dashboard"));
  }

  @Parameters({ "Environment" })
  @BeforeClass
  public void setUp(@Optional(DefaultEnvironment) String Environment) throws InterruptedException, IOException {
      Base.openURL(Environment);
  }

  @AfterClass public void tearDown() { 
      Base.driver.get().quit(); 
  }

}

Upvotes: 0

Related Questions