当我尝试运行包含2-3个测试用例的测试套件时,我只遇到NullPointerException问题。但是,如果我单独运行测试用例,它运行良好。以下是 BaseSetup 类。
public class BaseSetup {
private WebDriver driver;
public WebDriver getDriver() {
return driver;
}
private void setDriver(String browserType, String appURL) {
switch (browserType) {
case "chrome":
driver = initChromeDriver(appURL);
break;
case "firefox":
driver = initFirefoxDriver(appURL);
break;
}
private static WebDriver initChromeDriver(String appURL) {
...
}
private static WebDriver initFirefoxDriver(String appURL) {
WebDriver driver = new FirefoxDriver();
driver.navigate().to(appURL);
return driver;
}
@Parameters({ "browserType", "appURL" })
@BeforeSuite
public void initializeTestBaseSetup(String browserType, String appURL) {
try {
setDriver(browserType, appURL);
} catch (Exception e) {
System.out.println("Error....." + e.getStackTrace());
}
}
@AfterTest
public void tearDown() {}
这是 LoginPage 类(页面元素)
public class LoginPage {
//web elements of login page
protected WebDriver driver;
private By txtUsername = By.xpath(".//*[@id='email']");
private By txtPassword = By.xpath(".//*[@id='pass']");
private By btnlogin = By.xpath(".//*[@id='loginbutton']");
private String lblError = null;
//constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
}
//verify using incorrect password
//enter username into username textbox
public void enterUsername (String username){
WebElement txtname = driver.findElement(txtUsername);
if (txtname.isDisplayed())
{
txtname.sendKeys(username);
}
}
//enter password into password textbox
public void enterPassword (String password){
WebElement txtpw = driver.findElement(txtPassword);
if (txtpw.isDisplayed())
{
txtpw.sendKeys(password);
}
}
public void clickOnLoginBtn (){
}
这是 LoginPageTest 类。
public class LoginPageTest extends BaseSetup {
private WebDriver driver;
private LoginPage loginPage;
@Test
public void verifyLoginFunction() {
loginPage = new LoginPage(driver);
loginPage.enterUsername("abcdef"); //log trace the error here
loginPage.enterPassword("abcdef");
loginPage.clickOnLoginBtn();
}
@BeforeClass
public void Setup() {
driver=getDriver();
}
}
这是 VerifyErrorMessage 类(第二种情况)
private WebDriver driver;
private LoginPage loginPage;
@Test
public void verifyLoginMessage() {
loginPage = new LoginPage(driver);
loginPage.enterUsername("abc");
loginPage.clickOnLoginBtn();
String errorMsg = loginPage.getErrorMessage();
System.out.println(errorMsg);
}
@BeforeClass
public void Setup() {
driver=getDriver();
}
}
这是TestNG中的错误。
java.lang.NullPointerException
at com.facebook.objects.LoginPage.enterUsername(LoginPage.java:56)
at com.facebook.tests.LoginPageTest.verifyLoginFunction(LoginPageTest.java:28)
... Removed 25 stack frames
所以我先运行 VerifyErrorMessage ,然后再运行 LoginPageTest 。错误跟踪在上面的块代码的注释中。
非常感谢。
答案 0 :(得分:0)
NullReferenceException
此行的代码行是什么?
WebElement txtname = driver.findElement(txtUsername);
如果是,那么driver
变量就是null
。因此,为了解决这个问题,您需要在代码中跟踪null
。
鉴于症状,我猜测问题是你只是在beforeSuite上初始化驱动程序,第一次测试正常,但第二次测试在获取驱动程序时会得到null。我没看到你的拆解方法(@AfterTest
)做了什么,所以很难验证,但我猜你在那里将驱动程序设置为空?
尝试将@BeforeSuite
更改为@BeforeTest
,看看是否能解决问题:
@Parameters({ "browserType", "appURL" })
@BeforeSuite
public void initializeTestBaseSetup(String browserType, String appURL) {
try {
setDriver(browserType, appURL);
} catch (Exception e) {
System.out.println("Error....." + e.getStackTrace());
}
}
或者......如果您确实在使用@AfterTest
注释的拆解方法中将驱动程序设置为null,则应将其更改为@AfterSuite
。