2016-05-29 7 views
1

私のセレンオートメーションにpageObjectモデルを使用しています。JUnitで実行中のSeleniumスクリプトでNullPointerExceptionが発生する

次のブラウザ設定クラスを検討してください。

package BrowserConfig; 

import org.junit.After; 
import org.junit.Before; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class crossBrowserConfiguration { 
    By logInPanel = By.xpath("//div[@id='logInPanelHeading']"); 

    public static WebDriver driver = null; 
    WebDriverWait wait = new WebDriverWait(driver,30); 

    @Before 
    public void initBrowser(){ 
     driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
     driver.get("Website that contains Login Page"); 
     wait.until(ExpectedConditions.visibilityOfElementLocated(logInPanel)); 
     } 

    @After 
    public void closeBrowser(){ 
     driver.quit(); 
    } 
} 

私はログイン画面のページオブジェクトを持っています。

package PageObjects; 

import org.openqa.selenium.By; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

import BrowserConfig.crossBrowserConfiguration; 

public class LoginScreen extends crossBrowserConfiguration { 

    WebDriverWait wait = new WebDriverWait(driver,30); 
    By userName = By.xpath("//input[@id='txtUsername']"); 
    By password = By.xpath("//input[@id='txtPassword']"); 
    By loginButton = By.xpath("//input[@id='btnLogin']"); 
    By welcomeNote = By.xpath("//a[@id='welcome']"); 
    By empListVerify = By.xpath("//div[@id='employee-information']/a"); 

    public void logIN(String UserName, String Password){ 
     driver.findElement(userName).sendKeys(UserName); 
     driver.findElement(password).sendKeys(Password); 
     driver.findElement(loginButton).click(); 
     wait.until(ExpectedConditions.visibilityOfElementLocated(welcomeNote)); 

     //Employee List Verification 
     String empListBtnText = driver.findElement(empListVerify).getText(); 
     System.out.println(empListBtnText); 
    } 

} 

そして最後に、私は次のテストケースのスクリプトがあります。

package TestCases; 

import org.junit.Test; 

import BrowserConfig.crossBrowserConfiguration; 
import PageObjects.LoginScreen; 

public class initiateBrows extends crossBrowserConfiguration{ 

    LoginScreen Obj1 = new LoginScreen(); 

    @Test 
    public void runThis() throws Exception{ 
     Obj1.logIN("admin", "123456"); 
    } 

} 

私はJUnitのテストとして、私のテストを実行すると、それがすべてで実行しなくても、NullPointerExceptionができます。例外のスタックトレースは次のとおりです。

java.lang.NullPointerException 
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212) 
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102) 
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71) 
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45) 
    at BrowserConfig.crossBrowserConfiguration.<init>(crossBrowserConfiguration.java:15) 
    at TestCases.initiateBrows.<init>(initiateBrows.java:8) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) 
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) 
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

at BrowserConfig.crossBrowserConfiguration.<init>(crossBrowserConfiguration.java:15) WebDriverWait wait = new WebDriverWait(driver,30);

に私はこの例外を直面していますなぜに任意の洞察力を指し?

+0

この問題は解決しましたか?同じ例外があります – Gemasoft

答えて

0

waitフィールドは、クラスのオブジェクトが作成されるときに初期化されます。これは、JUnitが@Beforeメソッドを呼び出す前に発生します。したがって、driverオブジェクトはnullです。問題を解決するにはさまざまな方法があります。 1つは、ドライバオブジェクトを単純なフィールドにして、それを直ちに初期化することです。

public class crossBrowserConfiguration { 
    By logInPanel = By.xpath("//div[@id='logInPanelHeading']"); 

    public final WebDriver driver = new FirefoxDriver(); 
    WebDriverWait wait = new WebDriverWait(driver,30); 

    @Before 
    public void initBrowser(){ 
    driver.manage().window().maximize(); 
    driver.get("Website that contains Login Page"); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(logInPanel)); 
    } 

    @After 
    public void closeBrowser(){ 
    driver.quit(); 
    } 
} 
関連する問題