简体   繁体   中英

TestNG: Execute parallel tests in suite file using multiple test classes

I am trying to run parallel tests configuring a suite file. The tests are for web application using Selenium. The suite file is composed from multiple tests. Every test contains more than one test classes. The first class in every test is used to initialize (@BeforeTest) the WebDriver and shut down it (@AfterTest). The WebDriver is static in order to pass it to the other classes (The reason is that we need to continue the tests from the point where the last test-class has end).

The tests are successfully running when the suite is configured to run the tests sequentially. But when the suite is configured to run the tests in parallel, all the suite tests are using only the last loaded webdriver and each test is not running in it respective browser.

Do you have any idea how i can overcome this situation?

Check the suite template below:

<suite name="Register and Login functionality" parallel="tests" thread-count="2">
    <test name="Test-1">
        <parameter name="browser" value="chrome" />
        <classes>
            <class name="Init" />
            <class name="Register" />
            <class name="Login" />
        </classes>
    </test>
    <test name="Test-2">
        <parameter name="browser" value="chrome" />
        <classes>
            <class name="Init" />
            <class name="Register" />
            <class name="Login" />
        </classes>
    </test>
</suite>

The template from the Init test-class:

public class Init{

    private static WebDriver driver;

    public WebDriver getWebDriver(){
        return this.driver;
    }

    @BeforeTest
    public void setUp(){
        driver = new ChromeWebDriver();
    }

    @AfterTest
    public void quit(){
        driver.stop();
    }

}

The template from the register test class:

public class Register{

    private WebDriver driver;

    @BeforeClass
    public void setUp(){
        driver = Init.getWebDriver();
    }

    @Test
    public void test1(){
        ........
        ........
    }

    @Test
    public void test2(){
        ........    
        ........
    }

}

The template from login test-class:

public class Login{

    private WebDriver driver;

    @BeforeClass
    public void setUp(){
        driver = Init.getWebDriver();
    }

    @Test
    public void test1(){
        ........
        ........
    }

    @Test
    public void test2(){
        ........    
        ........
    }

}

Static object will be single for the jvm. For your case, you can store them in ThreadLocal object. Just change your init class.

public static class Init{
    private static ThreadLocal<WebDriver> WEBDRIVER = new ThreadLocal<WebDriver>();

   public static WebDriver getWebDriver(){
      WebDriver driver= WEBDRIVER.get();
      if (driver== null) {
        driver = new ChromeWebDriver();
        WEBDRIVER.set(driver);
      }
      return driver;
   }

   @AfterTest
   public void quit(){
      getWebDriver().quit();
  }
 }

If you extend register and login class with init as base class. Then you can simply avoid mentioning init class in suite xml

you are using static webdriver in your init class which will ensure that only one instance of webdriver is initiated.

private static WebDriver driver;

As you mentioned in question each test is not running in it respective browser. you need to change static web driver. Remove static web driver and initiate web driver for each test which will open each test in a new browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM