gpt4 book ai didi

java - 配置失败: @AfterMethod tearDown

转载 作者:行者123 更新时间:2023-12-01 13:18:58 25 4
gpt4 key购买 nike

我是 selenium webdriver 的新手。我尝试在两个浏览器上运行 Testng 测试并行,但我遇到了以下错误。当尝试运行时。

package rough;


import org.testng.annotations.AfterMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class Browsers {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@BeforeMethod
public void tearDown1() throws Exception {
System.out.println("Hello starting");
}
@Parameters("BROWSER")
public void setUp(@Optional String BROWSER) throws Exception {

//To run test case parallely in different browsers
if(BROWSER.equalsIgnoreCase("FF"))
{
//System.out.println(“Firefox driver would be used”);
driver = new FirefoxDriver();
}
else
if(BROWSER.equalsIgnoreCase("IE"))
{
//System.out.println(“Ie webdriver would be used”);
System.setProperty("webdriver.ie.driver", "g:/Selenium Jar Files/IEDriverServer.exe");
driver = new InternetExplorerDriver();
}

// driver = new FirefoxDriver();
baseUrl = "http://book.theautomatedtester.co.uk/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
driver.get(baseUrl + "/chapter1");
driver.findElement(By.id("radiobutton")).click();
new Select(driver.findElement(By.id("selecttype"))).selectByVisibleText("Selenium RC");
}

@AfterMethod
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
Assert.fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}

private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}

private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}

下面是 XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="True">
<test name="Test">
<parameter name = "BROWSER" value="FF"></parameter>
<classes>
<class name="rough.Browsers"/>
</classes>
<test name="Test">
<parameter name = "BROWSER" value="IE"></parameter>
<classes>
<class name="rough.Browsers"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

我正在获取堆栈跟踪

[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--757511090\testng-customsuite.xml

Hello starting
FAILED CONFIGURATION: @AfterMethod tearDown
java.lang.NullPointerException
at rough.Browsers.tearDown(Browsers.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

请帮助我

最佳答案

真正的错误是NullPointerException。我猜测在您的设置(或测试)中的某个地方,您抛出了一个错误,然后您尝试访问驱动程序,但它是空的。

要么在调用driver.close()之前进行空检查,要么确保驱动程序永远不为空(我更喜欢第一个选项)。

关于java - 配置失败: @AfterMethod tearDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204211/

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