IntelliJ错误:java.lang.NullPointerException

时间:2016-12-08 10:41:22

标签: java selenium intellij-idea selenium-webdriver pageobjects

我使用selenium,W​​ebDriver,Intellij,Junit4,ChromeDriver,PageObject,PageFactory。

我按照这里的说明操作: https://www.youtube.com/watch?v=pO5Ry_GLC60&t=582s 您关注此视频时,项目有效。 在视频之后我使用了PageFactory。 我代替抽象类我写了BasePage类,我创建了一个包含@ Before,@ After和webdriver方法的新类。 当我运行测试错误时是:java.lang.NullPointerEkception。 我不知道如何解决这个问题。 请帮助。

这是我的主页:

package PageObjectPage;


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

public class HomePage extends BasePage {

    @FindBy(how = How.NAME, using = "account_icon")
    @CacheLookup
    WebElement button_my_accout;


    public HomePage(WebDriver driver){

        super(driver);
    }

   public MyAccount clickOnMyAccount(){
        //Click on My Account
        button_my_accout.click();

       return PageFactory.initElements(getDriver(), MyAccount.class);
    }

    }

This is MyAccount page:

package PageObjectPage;

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


public class MyAccount extends BasePage {

    @FindBy(id = "log")
    @CacheLookup
    WebElement username;

    @FindBy(how = How.ID, using = "pwd")
    @CacheLookup
    WebElement password;

    @FindBy(how = How.ID, using = "login")
    @CacheLookup
    WebElement login_button;


    public MyAccount(WebDriver driver){

        super(driver);
    }

    public MyAccount LogIn(){
        //Fill in the text box username
        username.sendKeys("Dragana");
        //Fill in the text box password
        password.sendKeys("123456");

        return new MyAccount(driver);
    }
    public LogInResultPage submitForm() {
        //Click on button Log in
        login_button.click();

        return new LogInResultPage(driver);
    }
}

这是LogInResultPage:

package PageObjectPage;

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


public class LogInResultPage extends BasePage{

    public LogInResultPage(WebDriver driver){

        super(driver);
    }
    public String getMessage(){
        //Printing message
        return driver.findElement(By.tagName("p")).getText();

    }

}

这是BasePage页面:

package PageObjectPage;


import org.openqa.selenium.WebDriver;

public class BasePage {

    protected WebDriver driver;

    public BasePage (WebDriver driver){
        this.driver = driver;
    }

    public WebDriver getDriver() {
        return this.driver;
    }

    }

这是TestBase页面:

package TestBaseSetup;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class TestBase {

    WebDriver driver;

    public WebDriver getDriver() {

        return driver;
    }

    @Before
    public void testSetUp(){
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dragana\\Desktop\\chromedriver.exe ");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        driver = new ChromeDriver(options);

        driver.navigate().to("http://store.demoqa.com/");
    }

    @After
    public void testTearDown(){

        driver.close();
    }

}

这是我的测试页面:

package test;

import PageObjectPage.HomePage;
import PageObjectPage.LogInResultPage;
import PageObjectPage.MyAccount;
import TestBaseSetup.TestBase;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;


public class AccountTest extends TestBase {

    public WebDriver getDriver() {

        return driver;
    }

    WebDriver driver;

    @Test
    public void shouldLogIn() {

        HomePage onHomePage = PageFactory.initElements(driver, HomePage.class);
        System.out.println("Step 1 ");
        MyAccount onMyAccount = onHomePage.clickOnMyAccount();
        System.out.println("Step 2");
        LogInResultPage onResultPage = onMyAccount.LogIn().submitForm();
        System.out.println("Step 3");
        wait(2000);
        Assert.assertTrue(onResultPage.getMessage().contains("ERROR"));
    }

    public void wait(int seconds){
        try {
            Thread.sleep(2000);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }
}

错误:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy8.click(Unknown Source)
    at PageObjectPage.HomePage.clickOnMyAccount(HomePage.java:26)
    at test.AccountTest.shouldLogIn(AccountTest.java:31)
    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:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

1 个答案:

答案 0 :(得分:0)

1.Delete:

public WebDriver getDriver() {

    return driver;
}

WebDriver driver;
来自AccountTest的

因为它覆盖了TestBase中的WebDriver。

  1. 在TestBase中保护WebDriver驱动程序
  2. protected WebDriver driver;

    或者在AccountTest中使用getDriver(),例如HomePage onHomePage = PageFactory.initElements(getDriver(), HomePage.class);