[英]How to close browser after runnning all tests in testng suite with selenium
I have a TESTNG
file like below: Where tests in credentials
and checkHome
are executed first before runnning DatacheckTest
.我有一个
TESTNG
文件,如下所示:在运行 DatacheckTest 之前首先执行credentials
和checkHome
中的DatacheckTest
。
DatacheckTest.xml DatacheckTest.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<suite-files>
<suite-file
path="./credentials.xml"></suite-file>
<suite-file path="./checkHome.xml"></suite-file>
</suite-files>
<test thread-count="5" name="Test">
<classes>
<class name="DatacheckTest" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
All the Testng
class file below extend Base
which gets driver information.下面的所有
Testng
class 文件都扩展了获取驱动程序信息的Base
。 I want to close the browser session after running all the tests in the suite.我想在运行套件中的所有测试后关闭浏览器 session。 for example when I run
DatacheckTest
, the Base
class afterSuite
methhod annotated with AfterSuite
, closes browser and ends the session soon after running credentials.xml
.例如,当我运行
DatacheckTest
时,使用 AfterSuite 注释的Base
AfterSuite
afterSuite
方法关闭浏览器并在运行credentials.xml
后立即结束 session。
public class Credentials extends Base {
@Test
void test1() {
}
}
public class CheckHome extends Base {
@Test
void test2() {
}
}
public class DatacheckTest extends Base {
@Test
void test3() {
}
}
public class Base extends {
@AfterSuite
public void afterSuite() {
driver.close();
}
}
@AfterSuite
will not work for you, since you refer to multiple suite files and this will be invoked multiple times. @AfterSuite
对你不起作用,因为你引用了多个套件文件,这将被多次调用。
IExecutionListener in TestNG TestNG 中的 IExecutionListener
The IExecutionListener
listener monitors the start and the end of the whole TestNG execution process. IExecutionListener
监听器监听整个TestNG执行过程的开始和结束。
It's the most generic listener, which allows executing some code before everything and after everything .它是最通用的侦听器,允许在一切之前和之后执行一些代码。
The IExecutionListener
contains two methods: IExecutionListener
包含两个方法:
onExecutionStart
onExecutionFinish
You have to implement a custom ExecutionListener.您必须实现自定义 ExecutionListener。
import org.testng.IExecutionListener
class ExecutionListener implements IExecutionListener {
@Override
public void onExecutionStart() {
// launch driver
System.out.println("onExecutionStart invoked");
}
@Override
public void onExecutionFinish() {
// close driver
System.out.println("onExecutionFinish invoked");
}
}
and apply it on Base class:并将其应用于 Base class:
import org.testng.annotations.Listeners
@Listeners(ExecutionListener.class)
class Base {
// some implementation
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.