【问题标题】:Selenium null pointer exception, when trying to use constructorSelenium 空指针异常,尝试使用构造函数时
【发布时间】:2015-08-10 17:53:03
【问题描述】:

我创建了一个类,其中定义了页面对象。然后我试图通过使用构造函数在测试类中使用该类。但是,当我使用 JUnit 运行测试类时,我收到了 NullPointerException 错误。

Page_Objects 类:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

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

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;

public class Page_Objects {

    private WebDriver driver;

    WebElement Username = driver.findElement(By.id("username"));
    // ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
    WebElement Password = driver.findElement(By.id("password"));
    WebElement Login_Button = driver.findElement(By.id("Login"));
    WebElement Logout_Button = driver.findElement(By.linkText("Logout"));

}

测试类:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

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

  Page_Objects pageobjects = new Page_Objects();

  @Before
  public void setUp() throws Exception {
    driver1 = new FirefoxDriver();
    baseUrl = "some url for testing/";
    driver1.navigate().to(baseUrl);
    driver1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void Login_Logout() throws Exception {

    pageobjects.Username.sendKeys("someusername");

    // ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
    pageobjects.Password.sendKeys("somepassword");

    pageobjects.Login_Button.click();

    pageobjects.Logout_Button.click();

  }

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

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

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

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

当我运行上面的类时,我得到一个空指针异常。

java.lang.NullPointerException
    at Page_Objects.<init>(Page_Objects.java:20)
    at Salesforce_Test.<init>(Salesforce_Test.java:16)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

【问题讨论】:

    标签: java pointers exception selenium null


    【解决方案1】:

    我在这里看到两个问题。首先是在您启动驱动程序之前调用了Page_Objects 类。这就是为什么当你打电话时
    Page_Objects pageobjects = new Page_Objects(); 你得到一个NullPointerException。 因此,您希望在启动驱动程序后启动Page_Objects。 其次,您需要将您启动的驱动程序的实例传递给它,因为现在它有自己的未启动的私有驱动程序,因此它是空的。 简而言之:

    • 在启动驱动程序后启动Page_Objects(在driver1 = new FirefoxDriver(); 行之后)
    • 使用您启动的驱动程序调用它:Page_Objects pageobjects = new Page_Objects(driver1 );

    【讨论】:

    • 谢谢。这似乎奏效了。我必须先在 Page_Objects 类中创建一些方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多