Testng with multiple suites. @AfterSuite method closes driver after 1st suite and 2nd suite can't be run

冷暖自知 提交于 2020-01-04 13:29:49

问题


I use testng for running tests. I have several test suites(Suite1.xml, Suite2.xml and Suite3.xml), which are combined in one suite (MasterSuite.xml). Besides those, I have the class TestBase.java, where I configure such methods as @BeforeTest, @BeforeMethod, @BeforeSuite, @AfterSuite, etc.

Running the @AfterSuite method closes the driver (driver.quit()).

What I want: run MasterSuite which will run all my 3 suites one after another.

The problem: after the first suite (Suite1) is executed, the driver is closed and thus, Suite2 can't be run.

How can I resolve this issue/problem?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MasterSuite">
    <suite-files>
        <suite-file path="Suite1.xml" />
        <suite-file path="Suite2.xml"/>
        <suite-file path="Suite3.xml"/>
    </suite-files>
</suite>

回答1:


If you want to run one suite after another and not to run them in parallel, then it should be enough to start your driver in @BeforeSuite method.

In that way all your suites will have separate instance of WebDriver initialized.




回答2:


  1. Remove quit method form AfterSuite and Place it after all your test cases in the last suite
  2. Make Test cases independent, You can use BeforeMethod and AfterMethod to invoke browser if not and close browser if failure plus the login part if needed, and this can benefit later as you can have independent test cases
  3. AfterSuite will be called after each suite is done, so your quit driver is getting called after the first suite is called. You can use BeforeSuite to have a method which will invoke driver if it is not running, Can have a flag which will make sure to launch only one driver if it is not running
  4. If it is a small number of test cases, you can get them all together into one test suite and carry on with the same fashion without any issue



回答3:


I would recommend to use TestNG Listener ISuiteListener.

First, store the Driver as a suite attribute, for example inside @BeforeClass of TestBase:

@Listeners({ SuiteListener.class })
public class TestBase {

    public WebDriver driver;

    @BeforeClass
    public void init(ITestContext testContext) {
        driver = createWebDriver(...);
        ISuite suite = testContext.getSuite();
        suite.setAttribute("driver", driver);
    }
    ...
}

Then, in SuiteListener class, create onFinish() method to get the stored attribute, and close the Driver:

public class SuiteListener implements ISuiteListener {

    public WebDriver driver;

    /**
     * This method is invoked after the SuiteRunner (parent suite) has finished to run all the child suites.
     */
    public void onFinish(ISuite suite) {
        driver = (WebDriver) suite.getAttribute("driver");
        closeDriverAfterSuite(...);
    }


来源:https://stackoverflow.com/questions/31320851/testng-with-multiple-suites-aftersuite-method-closes-driver-after-1st-suite-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!