gpt4 book ai didi

selenium - @AfterTest Selenium 注释在所有 @Test 执行之前执行

转载 作者:行者123 更新时间:2023-12-02 19:10:35 26 4
gpt4 key购买 nike

我曾从事 Selenium 和 TestNG 的工作。但我不是专家。我尝试为此寻找合适的答案,但找不到。因此请求帮助。

我有一个初始化类,如下所示。

public class Initialization {

private static Logger logger = Logger.getLogger(Initialization.class);
private WebDriver driver;

@BeforeTest
public void intiDriver() throws MalformedURLException {
logger.debug("Creating driver");
DesiredCapabilities dcap = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), dcap);
logger.debug("Driver created");
}

@AfterTest
public void closeDriver(){
logger.debug("Quitting driver");
if(driver != null){
driver.close();
driver.quit();
logger.debug("Driver destroyed");
}
}

public WebDriver getDriver() {
return driver;
}
}

然后我有2个测试用例类文件,它们扩展了上面的类文件(如下所示)

public class SampleTestCase1 extends Initialization {

@Test
public void sampleTest1(){
System.out.println("getDriver1: "+ getDriver());
getDriver().get("http://www.google.com");
}

@Test
public void sampleTest2(){
System.out.println("getDriver2: "+ getDriver());
getDriver().get("http://www.bing.com");
}
}

public class SampleTestCase2 extends Initialization {

@Test
public void sampleTest3(){
System.out.println("getDriver3: "+ getDriver());
getDriver().get("https://en.wikipedia.org/wiki/Main_Page");
}

@Test
public void sampleTest4(){
System.out.println("getDriver4: "+ getDriver());
getDriver().get("http://www.rediff.com/");
}
}

我观察到sampleTest3和sampleTest4测试用例没有被执行,因为getDriver()返回null。我相信在执行sampleTest1和smapleTest2后驱动程序实例被破坏。我使用了 @afterTest 注释,因此理想情况下,驱动程序实例应该在执行所有 @Test 后被销毁。

我不知道出了什么问题。有人可以帮忙吗?

堆栈跟踪:

java.lang.NullPointerException
at Sample.SampleTestCase2.sampleTest3(SampleTestCase2.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

最佳答案

TestNG 不是 JUnit。不要错误匹配 @BeforeTest/AfterTest@BeforeMethod/AfterMethod

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.

http://testng.org/doc/documentation-main.html#annotations

关于selenium - @AfterTest Selenium 注释在所有 @Test 执行之前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37298595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com