2017-12-21 26 views
0

抽象クラスとテストクラスを拡張してテストを作成しようとすると、NullPointerExceptionが発生します。エラー:TestNG SeleniumとPageオブジェクトパターンを使用したNullPointerException

java.lang.NullPointerException at pages.UserRegistrationPage.fillName(UserRegistrationPage.java:61) at UserRegistrationpageTest.fillName(UserRegistrationpageTest.java:26) 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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:571) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979) 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:1187) at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) at org.testng.TestNG.runSuites(TestNG.java:1028) at org.testng.TestNG.run(TestNG.java:996) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

ドライバが初期化され、テストページが開き、フィールドに入力しようとすると例外がスローされます。 私のクラス:

public abstract class AbstractPage { 
     private WebDriver driver; 
     public AbstractPage(WebDriver driver){ 
      this.driver = driver; 
     } 
    } 

    public class UserRegistrationPage extends AbstractPage{ 

     @FindBy(id = "input-firstname") 
     private WebElement firstName; 

     @FindBy(id = "input-lastname") 
     private WebElement lastName; 

     @FindBy(id = "input-email") 
     private WebElement email; 

     @FindBy(id = "input-telephone") 
     private WebElement telephone; 

     @FindBy(xpath = "//*[@id=\"input-fax\"]") 
     private WebElement fax; 

     @FindBy(id = "input-address-1") 
     private WebElement address; 

     @FindBy(id = "input-city") 
     private WebElement city; 

     @FindBy(id = "input-postcode") 
     private WebElement postcode; 

     @FindBy(id = "input-country") 
     private WebElement country; 

     @FindBy(id = "input-zone") 
     private WebElement zone; 

     @FindBy(id = "input-password") 
     private WebElement password; 

     @FindBy(id = "input-confirm") 
     private WebElement passwordConfirm; 

     @FindBy(css = "#content input[type=\"checkbox\"]:nth-child(2)") 
     private WebElement agreeCheckbox; 

     @FindBy (xpath = "//*[@id=\"content\"]/form/div/div/input[2]") 
     private WebElement submitButton; 

     public UserRegistrationPage(WebDriver driver) { 
      super(driver); 
     } 


     public void fillName(String name){ 
      this.firstName.sendKeys(name); 
     } 
    } 

public abstract class AbstractTest { 

    protected WebDriver driver; 
    @BeforeSuite 
    public void setUpDriver(){ 
     driver = new FirefoxDriver(); 
    } 
} 

public class UserRegistrationpageTest extends AbstractTest{ 

    private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver); 


    @BeforeTest 
    void openURL(){ 
     driver.get("http://88.119.151.54/opencartone/index.php?route=account/register"); 
    } 

    @AfterTest 
    void closeBrowser(){ 
     driver.quit(); 
    } 

    @Test 
    void fillName(){ 
     userRegistrationPage.fillName("John"); 
    } 

} 

私はここで何が間違っていますか?

答えて

1

理由ビーイング:あなたは「userRegistrationPage.fillName( 『ジョン』)」行に達したときに「UserRegistration]ページの[ドライバのメンバーは、ヌルのまま。

この

enter image description here

これは@BeforeSuiteされているので

のコンストラクタ(メソッドが呼び出されますUserRegistrationPageのコンストラクタの後に呼び出される内部の 'AbstractTest' テストを横たわっている。だから、順序は今

1である)を参照してくださいAbstractPage。 (ここであなたのドライバはnullです)

2)UserRegistrationPageのコンストラクタです。 (ここであなたのドライバはnullです)

3)@BeforeSuite of AbstractTest。

private UserRegistrationPage userRegistrationPage = new UserRegistrationPage(driver); 

「UserRegistrationPage」オブジェクトのこの、変更初期設定の場所を解決するために(ここでは、あなたのドライバはnullになりませんが、この時間までに、あなたはすでに「UserRegistrationpageTest」クラスのラインを使用して「UserRegistrationPage」のオブジェクトを初期化しています

enter image description here

+0

ありがとう!それは私の問題を解決しました。 – Sig

0

'PageFactory'を使用する場合は、WebElementsを初期化する必要があります。 AbstractPageを拡張UserRegistrationPageWebDriverインスタンスdriverを使用しています 'AbstractPage'

public AbstractPage(WebDriver driver) { 
    this.driver = driver; 
    PageFactory.initElements(driver, this); 
} 
+0

ストレンジ以下のように 'OpenURLの' 内部で、今NullPointerExceptionが取得し、少し異なる: 'java.lang.NullPointerExceptionが \t org.openqa.selenium.support.pagefactory.DefaultElemeでcom.sun.proxyでntLocator.findElement(DefaultElementLocator.java:69) org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invokeで\t(LocatingElementHandler.java:38) \t。$ Proxy9.sendKeys(不明出所)pages.UserRegistrationPage.fillNameで \t(sun.reflect.NativeMethodAccessorImpl.invoke0でUserRegistrationPage.java:61) UserRegistrationpageTest.fillNameで\t(UserRegistrationpageTest.java:26) \t(ネイティブメソッド) ' – Sig

+0

@Sigをしてみてくださいそれを 'UserRegistrationPage'コンストラクタに移動します。 – Guy

+0

ない運が、私はJUnitを用いたTestNGのを置き換える場合いただきまし奇妙なすべてが正常に動作します。 – Sig

0

に追加します。したがって、あなたは、コンストラクタを定義する必要がありますよう:

WebDriver driver; 

//constructor 
public UserRegistrationPage(WebDriver driver) 
{ 
    this.driver=driver; 
} 
+0

OPのコードはすでに 'super(driver)'を呼び出すことでこれを行っていると思います。したがって、このコンストラクタは必要ありません。 –

関連する問題