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>
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.
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(...);
}