Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IntelliJ错误:java.lang.NullPointerException_Java_Selenium_Intellij Idea_Selenium Webdriver_Pageobjects - Fatal编程技术网

IntelliJ错误:java.lang.NullPointerException

IntelliJ错误:java.lang.NullPointerException,java,selenium,intellij-idea,selenium-webdriver,pageobjects,Java,Selenium,Intellij Idea,Selenium Webdriver,Pageobjects,我使用selenium、WebDriver、Intellij、Junit4、ChromeDriver、PageObject、PageFactory 我按照这里的指示: 当您观看此视频时,此项目将正常工作。 视频播放后,我使用了PageFactory。 我不是写BasePage类的抽象类,而是创建了一个新类,它包含webdriver的@Before、@After和方法。 当我运行测试时,错误是:java.lang.NullPointerEkception。 我不知道如何解决这个问题。 请帮忙 这是

我使用selenium、WebDriver、Intellij、Junit4、ChromeDriver、PageObject、PageFactory

我按照这里的指示: 当您观看此视频时,此项目将正常工作。 视频播放后,我使用了PageFactory。 我不是写BasePage类的抽象类,而是创建了一个新类,它包含webdriver的@Before、@After和方法。 当我运行测试时,错误是: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);
    }
}
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;
    }

    }
这是登录结果页面:

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.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);
    }
}
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.删除:

public WebDriver getDriver() {

    return driver;
}

WebDriver driver;
来自AccountTest,因为它覆盖了来自TestBase的WebDriver

  • 在TestBase中保护WebDriver驱动程序
  • 受保护的WebDriver

    或者在AccountTest中使用getDriver(),例如主页上的主页=PageFactory.initElements(getDriver(),HomePage.class)

    1.删除:

    public WebDriver getDriver() {
    
        return driver;
    }
    
    WebDriver driver;
    
    来自AccountTest,因为它覆盖了来自TestBase的WebDriver

  • 在TestBase中保护WebDriver驱动程序
  • 受保护的WebDriver


    或者在AccountTest中使用getDriver(),例如主页上的主页=PageFactory.initElements(getDriver(),HomePage.class)

    请发布错误的完整堆栈跟踪,或至少发布发生错误的文件和行。这使得调试匹配更容易。可能是我添加的错误的重复项。请发布错误的完整堆栈跟踪,或至少发布错误发生的文件和行。它使调试匹配更容易。可能重复的我添加了什么出来的错误。当删除WebDriver;在AccountTest中,无法识别代码中的驱动程序:HomePage onHomePage=PageFactory.initElements(驱动程序,HomePage.class);您是否在TestBase中创建了受保护的WebDriver驱动程序?从AccountTest中删除驱动程序后,您需要在TestBase中创建受保护的WebDriver驱动程序或使用getDriver()。删除WebDriver驱动程序后出现的错误;在AccountTest中,我在TestBase中添加了protected。org.openqa.selenium.NoSuchElementException:没有这样的元素:找不到元素:{“方法”:“名称”,“选择器”:“帐户图标”}(会话信息:chrome=54.0.2840.99)(驱动程序信息:chromedriver=2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),平台=Windows NT 6.3.9600 x8664)(警告:服务器未提供任何stacktrace信息)命令持续时间或超时:55毫秒有关此错误的文档,请访问:这是另一个问题。因此,NullPointerException的问题已解决。现在您的选择器不正确。请使用HTML和您尝试获取元素的代码问另一个问题。删除WebDriver driver;in AccountTest时,无法识别中的驱动程序代码:HomePage onHomePage=PageFactory.initElements(driver,HomePage.class);是否在TestBase中创建受保护的WebDriver驱动程序?从AccountTest中删除驱动程序后,需要在TestBase中创建受保护的WebDriver驱动程序或使用getDriver().My error delete WebDriver driver;在AccountTest中我添加了受保护的TestBase.org.openqa.selenium.NoSuchElementException:没有这样的元素:找不到元素:{“方法”:“名称”,“选择器”:“帐户图标”}(会话信息:chrome=54.0.2840.99)(驱动信息:chromedriver=2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:55毫秒有关此错误的文档,请访问:这是另一个问题。因此,NullPointerException的问题已解决。现在您的选择器不正确。请在尝试获取元素的HTML和代码中询问另一个问题。