TestNG.在所有测试之前需要运行特定的方法,在所有测试之后需要进行特定的测试 [英] TestNG. Need to run specific method before all tests and specifiic test after all tests
问题描述
使用了硒+ TestNG + Maven.
Used Selenium + TestNG + Maven.
我想使用OWASP ZAP自动执行测试漏洞.为此,我需要在所有测试之前启动ZAProxyScanner-在所有测试之前执行方法.
I want to automate testing vulnerabilities using OWASP ZAP. For this I need to start ZAProxyScanner before all tests - execute method before all tests.
public void initZap(){
zapScanner = new ZAProxyScanner(ZAP_PROXYHOST,ZAP_PROXYPORT,ZAP_APIKEY);
zapScanner.clear(); //Start a new session
zapSpider = (Spider)zapScanner;
}
并且在执行所有功能测试后-运行测试以搜索漏洞
and when all functional tests were executed - run test for searching vulnerabilities
@Test
public void scanning() throws ClientApiException{
spiderWithZap();
setAlertAndAttackStrength();
zapScanner.setEnablePassiveScan(true);
scanWithZap();
}
位于一类中的方法和测试,例如public class TestSecurity
Method and test located in one class, e.g. public class TestSecurity
这是我的testng.xml的示例,其中包含包含功能测试的软件包
Here is sample of my testng.xml with packages containing functional tests
<suite name="Chrome" thread-count="1" parallel="tests" configfailurepolicy="continue">
<test name="chrome">
<parameter name="browser" value="chrome"/>
<packages>
<package name="tests.suiteLogIn"></package>
<package name="tests.suiteSettings"></package>
<package name="tests.suiteSearch"></package>
</packages>
</test>
UPD.用AfterTest发布修改后的代码. 我只使用Before/AfterMethod注释
UPD. post modified code with AfterTest in it. I use only Before/AfterMethod annotations
@BeforeMethod(alwaysRun=true)
@Parameters({"browser", "environment"})
public void setUp(@Optional ("firefox") String browser, @Optional ("local") String environment, Method method) throws IOException {
System.out.println("Test name: " + method.getName());
WebDriver driver = getMyDriver(browser, environment);
System.setProperty(ESCAPE_PROPERTY, "false");
}
@AfterMethod(alwaysRun=true)
@Parameters("browser")
public void tearDown(@Optional ("firefox") String browser){
DriverMaster.stopDriver();
}
@BeforeSuite
@Parameters("browser")
public void startZap(@Optional ("firefox") String browser){
if(browser.equals("firefox")){
sec.initZap();
}
}
@AfterSuite
@Parameters("browser")
public void scanZap(@Optional ("firefox") String browser) throws ClientApiException{
if(browser.equals("firefox")){
LoginPage lp = new LoginPage(getDriverInstance()).load();
lp.login("name", "pass");
sec.scanning();
}
}
推荐答案
您基本上有两个选择:
-
使用@BeforeSuite和@AfterSuite并将其包含在文件中以运行或让您的所有类对其进行扩展
Use @BeforeSuite and @AfterSuite and include that in the files to run or make all your classes extend it
使用 ITestListener 或
Use ITestListener or ISuiteListener and put the setup and teardown code in their before and after methods.
有了听众,我可以看到的一个优势是,如果您想基于一些测试结果进行条件拆卸(扫描),您也可以控制它.
With listeners, one advantage that I can see is if you want to do conditional teardown (scanning) based on some testresults you can control that too.
这篇关于TestNG.在所有测试之前需要运行特定的方法,在所有测试之后需要进行特定的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!