2016-12-07 102 views
0

我是新的硒,我使用IntelliJ,Selenium WebDriver,Junit。我的問題是 java.lang.NullPointerException。這些是我在項目中的頁面。IntelliJ錯誤:java.lang.NullPointerException

這是主頁頁面:

package PageObjectPage; 


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.CLASS_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); 
    } 

這是我的帳戶頁:

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頁:

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; 
import org.openqa.selenium.support.PageFactory; 


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(); 
     } 
    } 
} 

錯誤:

"C:\Program Files\Java\jdk1.8.0_101\bin\java" -ea -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;C:\Users\Dragana\workspace\proba\target\test-classes;C:\Users\Dragana\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Dragana\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-java\3.0.0-beta3\selenium-java-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\3.0.0-beta3\selenium-chrome-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\3.0.0-beta3\selenium-remote-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-api\3.0.0-beta3\selenium-api-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\cglib\cglib-nodep\3.2.4\cglib-nodep-3.2.4.jar;C:\Users\Dragana\.m2\repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C:\Users\Dragana\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;C:\Users\Dragana\.m2\repository\com\google\guava\guava\19.0\guava-19.0.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;C:\Users\Dragana\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Dragana\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpmime\4.5.2\httpmime-4.5.2.jar;C:\Users\Dragana\.m2\repository\net\java\dev\jna\jna\4.1.0\jna-4.1.0.jar;C:\Users\Dragana\.m2\repository\net\java\dev\jna\jna-platform\4.1.0\jna-platform-4.1.0.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-edge-driver\3.0.0-beta3\selenium-edge-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver\3.0.0-beta3\selenium-firefox-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-ie-driver\3.0.0-beta3\selenium-ie-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-opera-driver\3.0.0-beta3\selenium-opera-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-safari-driver\3.0.0-beta3\selenium-safari-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\io\netty\netty\3.5.7.Final\netty-3.5.7.Final.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-support\3.0.0-beta3\selenium-support-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\com\codeborne\phantomjsdriver\1.3.0\phantomjsdriver-1.3.0.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 test.AccountTest,shouldLogIn 
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 33399 
Only local connections are allowed. 
Dec 07, 2016 6:54:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end 
Dec 07, 2016 6:54:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Detected dialect: OSS 
Step 1 

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:25) 
    at test.AccountTest.shouldLogIn(AccountTest.java:28) 
    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) 
+0

你好,我想這是因爲你沒有初始化變量「button_my_accout」。你知道該怎麼做,或者你需要更多的解釋嗎? –

+0

我不知道如何初始化值「button_my_accout」。你能向我解釋一下,如果你能寫我的代碼嗎? –

+0

當然。你可以嘗試像這樣:'WebElement button_my_accout = driver.findElement(By.xpath(「your_xpath」));'。確保在此代碼之前聲明您的驅動程序。您也可以創建一個By對象而不是Webelement。然後,代替'WebElement button_my_accout = driver.findElement(By.xpath(「your_xpath」));',你寫'By button_my_accout = By.xpath(「your_xpath」);'然後,在你的測試方法中:'driver .findElement(button_my_accout).click();' –

回答

0

在您聲明WebDrive驅動你的AccountTest類;再次設置爲driver = null,這會導致HomePage驅動程序爲空;你也不需要webDriver的getter。這應該工作得很好

public class AccountTest extends TestBase { 

@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(); 
     } 
    } 
} 
+0

此代碼不起作用。走出同樣的錯誤。你知道一些其他的解決方案嗎? –

相關問題