在硒中创建PageObject Module时出错对于简单的Tesng测试用例

时间:2019-03-10 07:37:42

标签: java selenium selenium-webdriver automated-tests testng

我试图使用PageObjectModel在Selenium中创建一个简单程序。在运行程序时,它将引发Null指针异常。不知道我在做什么错。是我的Variable初始化错误。我知道我在初始化By定位器时犯了错误,但不知道我在做什么错。

    public class main extends Base{

    private static final int TIMEOUT = 5;
    private static final int POLLING = 100;

    protected WebDriverWait wait;
    protected static WebElement ele;
    protected By locator;

    public void Base() {
        wait = new WebDriverWait(driver, TIMEOUT, POLLING);
    }

    public WebElement waitForElementToAppear(By locator) {
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));//Line which Throws Null
        return ele;
    }

    protected void waitForElementToDisappear(By locator) {
        wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    protected void waitForTextToDisappear(By locator, String text) {
        wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(locator, text)));
    }
@Test()
   public void getURL() {
            driver.get("https://www.google.com");
            waitForElementToAppear(By.name("q")).sendKeys("Pom");// Line Which Throws Null.

}

还有保存驱动程序属性的“我的基础类代码”。

 public class Base {
    protected WebDriver driver;

    public WebDriver getDriver() {
        return driver;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }
    @BeforeSuite
    public void beforeSuite() {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe"); // You can set this property elsewhere
        driver=new ChromeDriver();
        driver.manage().window().maximize();

    }
}

2 个答案:

答案 0 :(得分:2)

问题出在初始化WebDriverWait对象的方式上。

仅当@BeforeSuite类中运行Base方法时,您的WebDriver对象才会实例化。

初始化WebDriverWait的逻辑是public void Base()类中方法main的一部分。

但是您的@Test注释的getURL()方法不会调用Base()方法。因此,您的wait对象始终为空。

要解决此问题,请在您的Base()方法中调用@Test或用Base()注释您的@BeforeClass方法,以便TestNG自动调用它。

答案 1 :(得分:0)

您的代码中存在多个问题

  • 可能不需要全局变量声明。当然,您可以这样做,但是请确保将其初始化。
  • 请勿在页面对象中放置测试方法
  • ele将始终为空
  • 调用构造函数

可能您需要以下内容:

 public class MyPageObject extends Base{

    private static final int TIMEOUT = 5;
    private static final int POLLING = 100;

    protected WebDriverWait wait;

    public void MyPageObject() {
        super();                                             //Call constructor of Base if needed
        wait = new WebDriverWait(driver, TIMEOUT, POLLING);  //init wait
    }

    public WebElement waitForElementToAppear(By locator) {
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        return driver.findElement(locator);                  //return WebElement
    }

    protected void waitForElementToDisappear(By locator) {
        wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    protected void waitForTextToDisappear(By locator, String text) {
        wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(locator, text)));
    }
}

public class MyTestClass {
   @Test()
   public void getURL() {
       MyPageObject myPageObject = new MyPageObject();       //Initialize your page object
       driver.get("https://www.google.com");
       myPageObject.waitForElementToAppear(By.name("q")).sendKeys("Pom");
}