當我試圖運行帶有2-3個測試用例的測試套件時,我只面臨NullPointerException的問題。但是,如果我單獨運行測試用例,它運行良好。以下是BaseSetup類。Selenium - Eclipse:運行測試套件時出現NullPointerException
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後。錯誤跟蹤在上面的塊代碼註釋中。
非常感謝。