java lang NullPointerException

时间:2016-03-09 03:05:09

标签: java selenium-webdriver nullpointerexception pageobjects findby

使用Selenium Wedriver实用指南中的示例(登录并在Wordpress上创建测试帖),我遇到了上述错误。

这是我正在使用的PageObject:

package com.PageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by JTester on 3/9/2016.
 */
public class AdminLoginPage {
    WebDriver driver;
    @FindBy(how=How.ID, id="user_login")
    WebElement email;
    @FindBy(how=How.ID, id="user_pass")
    WebElement pwd;
    @FindBy(how=How.ID, id="wp-submit")
    WebElement submit;

    public AdminLoginPage(WebDriver driver){
        this.driver = driver;
        driver.get("http://mysite.wordpress.com/wp-admin/");
        }

    public void login(){
        email.sendKeys("myEmailAddress@yahoo.com");
        pwd.sendKeys("myPasswd");
        submit.click();
    }

}

以下是测试类:

package com.features.trial;

import com.PageObjects.AdminLoginPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Created by JTester on 3/9/2016.
 */
public class TestAddNewPost {
    public static void main(String... args) {
        WebDriver driver = new FirefoxDriver();
        //login to wordpress admin
        AdminLoginPage admLoginPage = new AdminLoginPage(driver);
        admLoginPage.login();

        //go to AllPosts page
        driver.get("http://mysite.wordpress.com/wp-admin/edit.php");

        //add a new post
        WebElement addNewPost = driver.findElement(By.linkText("Add New"));
        addNewPost.click();

        //add new post's content
        driver.switchTo().frame("content_ifr");
        WebElement postBody = driver.findElement(By.id("tinymce"));
        postBody.sendKeys("This is my description.");
        driver.switchTo().defaultContent();
        WebElement title = driver.findElement(By.id("title"));
        title.sendKeys("This is my first post");

        //publish my post
        WebElement publish = driver.findElement(By.id("publish"));
        publish.click();
        }
}

由于我是Selenium和Page Objects的新手,有人可以解释为什么会抛出以下错误:

Exception in thread "main" java.lang.NullPointerException
    at com.PageObjects.AdminLoginPage.login(AdminLoginPage.java:29)
    at com.features.trial.TestAddNewPost.main(TestAddNewPost.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

非常感谢..

ps:在网上查看了很多代码示例之后,我想我需要使用PageFactory来实例化我的元素,但不确定这是否属实,如果是,我该如何使用我的示例来解决它代码。

调试器截图: enter image description here

2 个答案:

答案 0 :(得分:0)

AdminLoginPage应该已使用PageFactory初始化。

import org.openqa.selenium.support.PageFactory;

public AdminLoginPage(WebDriver driver){
    PageFactory.initElements(driver, this);  //add this
    this.driver = driver;
    driver.get("http://mysite.wordpress.com/wp-admin/");
}

答案 1 :(得分:0)

添加以下代码:

AdminLoginPage loginPage = PageFactory.initElements(driver,AdminLoginPage.class);
        loginPage.login();

到TestAddNewPost类,而不是使用现有代码:

AdminLoginPage admLoginPage = new AdminLoginPage(driver);
        admLoginPage.login();