如何在Selenium中的页面对象模型中解决java.lang.NullPointerException?

时间:2018-08-27 06:28:07

标签: selenium testng

TestBase类:-

public class TestBase {
    public static WebDriver driver;
    public static Properties prop;

    // TestBase class constructor is used to initialize the properties object to
    // fetch config variables from config.properties file.
    public TestBase() {

        try {

            // File src = new File("./src/main/java/com/config/config.properties");
            File src = new File(".\\src\\main\\java\\com\\config\\config.properties");
            FileInputStream fs = new FileInputStream(src);
            prop = new Properties();
            prop.load(fs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initialization() throws InterruptedException {
        String browserName = prop.getProperty("Browser");
        if (browserName.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
        } else {
            System.out.println("Oops! Exception has Caught");
        }

        driver.manage().window().maximize();
        driver.get(prop.getProperty("URL"));
    }
}

首页类:-

public class HomePage extends TestBase {

    TestUtil testUtil;

    @FindBy(xpath = "//a[@title='Update your profile, personal settings, and more']")
    WebElement updateProfile;


    @FindBy(xpath = "//a[@title='Create a New Post']")
    WebElement createNewPost;

    @FindBy(xpath="(//span[@class='count'])[2]")
    WebElement drafts;

    @FindBy(xpath="//a[@class='button masterbar__recent-drafts-see-all is-compact is-borderless']")
    WebElement seeAll;

    public HomePage() {
        PageFactory.initElements(driver, this);
    }

    public ProfilePage updateYourProfile() throws AWTException, InterruptedException {
        updateProfile.click();
        Thread.sleep(3000);
        return new ProfilePage();
    }

    public NewPostPage createNewPost() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        return new NewPostPage();
    }

    public DraftsPage draftsPage() throws InterruptedException {
        createNewPost.click();
        Thread.sleep(3000);
        drafts.click();
        Thread.sleep(3000);
        seeAll.click();
        Thread.sleep(3000);
        return new DraftsPage();
    }
}

DraftsPage类:-

public class DraftsPage extends TestBase {

    @FindBy(xpath="(//button[@title='Toggle menu'])[1]")
    WebElement toggleMenu;

    @FindBy(xpath="(//button[@role='menuitem'])[2]")
    WebElement trash;

    public void toggleMenu() throws InterruptedException {
        toggleMenu.click();
        Thread.sleep(3000);
        trash.click();
        Thread.sleep(3000);
    }
}

DraftsPageTest类:-

public class DraftsPageTest extends TestBase {

    ProfilePage myProfile;
    LoginPage loginpage;
    HomePage homepage;
    NewPostPage newPostPage;
    DraftsPage draftspage;

    public DraftsPageTest() {
        super();
    }

    @BeforeMethod
    public void setUp() throws InterruptedException {
        initialization();
        loginpage = new LoginPage();
        loginpage.login();

        Thread.sleep(3000);
        loginpage.userName(prop.getProperty("username"));
        Thread.sleep(3000);
        homepage = loginpage.password(prop.getProperty("password"));
        Thread.sleep(3000);
        myProfile = new ProfilePage();
        Thread.sleep(3000);
        newPostPage = new NewPostPage();
        Thread.sleep(3000);
        draftspage = new DraftsPage();
        Thread.sleep(3000);
    }

    @Test
    public void DraftsPageTest() throws InterruptedException {
        homepage.draftsPage();
        Thread.sleep(3000);
        draftspage.toggleMenu();
        Thread.sleep(3000);
    }

    @AfterMethod
    public void close() {
        driver.close();
    }
}

错误消息:-

FAILED: DraftsPageTest
java.lang.NullPointerException
    at com.pages.DraftsPage.toggleMenu(DraftsPage.java:18)
    at com.testCases.DraftsPageTest.DraftsPageTest(DraftsPageTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

说明:-

我正在尝试借助Selenium开发TestNG Framework。 我已经开发了HomePage,LoginPage,ProfilePage,NewPostPage,DraftsPage。当我运行DraftsPageTest.java时, @BeforeMethod 工作正常 并且当控制权转到 @Test 时,homepage.draftsPage();也可以正常工作。但是,当控制权来到draftspage.toggleMenu();时,它就会抛出

  

java.lang.NullPointerException。

我们将不胜感激任何帮助。

提前感谢:)

1 个答案:

答案 0 :(得分:1)

您的代码中有一个错误。

请向您的PageFactory.initElements(driver, this);类添加一个调用com.pages.DraftsPage的构造函数[类似于共享代码中的HomePage构造函数]。那应该可以解决问题。

NullPointerException背后的原因是,没有初始化PageFactory.initElements() WebElement的电话toggleMenu