2017-02-12 13 views
0

POM、BaseTest、およびTestクラスが添付されています。プロジェクトを右クリックしてTestNGテストとして実行しようとすると、下のコードのNullPointerExceptionが表示されます。どうか提案できますか?PageFactoryを使用してスクリプトを実行しようとすると「NullPointerException」が発生する

POMクラス:

package pom; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.PageFactory; 

public class Introduction 
{ 

@FindBy(xpath="//a[text()='Hello. Sign In']") 
WebElement signInLink; 

public Introduction(WebDriver driver) 
{ 
PageFactory.initElements(driver, this); 
} 

public void signIn() 
{ 
    signInLink.click(); 
} 
} 

BaseTestクラス:

package scripts; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.*; 


public class BaseTest 
{ 
public WebDriver driver; 

@BeforeSuite 
public void preCondition() 
{ 
    driver= new FirefoxDriver(); 
    driver.get("https://www.walmart.com/"); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
} 

@AfterSuite 
public void postCondition() 
{ 
    driver.close(); 
} 
} 

テストクラス:

package scripts; 

import org.testng.annotations.Test; 

import pom.Introduction; 

public class SignIn extends BaseTest 
{ 

@Test 

public void validSignIn() 
{ 
    Introduction i= new Introduction(driver); 
    i.signIn(); 
} 
} 
+0

タイムアウトを増やしてみますか?適切に読み込まれたページが表示されますか? – liquide

+0

例外トレースを共有できますか? – Mahipal

答えて

0

あなたのコードは、いくつかの問題があります。

  • ウェブドライブを@BeforeSuiteでインスタンス化しています。これにより、Webdriverインスタンスは<suite>タグごとに1回だけ作成されます。したがって、他のすべての@Testメソッドは、@BeforeSuite注釈付きメソッドが2回目に実行されないため、常にNullPointerExceptionになります。
  • 暗黙のタイムアウトを使用しています。暗黙のタイムアウトを使用しないでください。あなたはthis SOの投稿で暗黙の待ちの悪の詳細を読むことができます。だから、始めるため

、私はそれが

以下

のようなものにテストコードを変更することをお勧めBaseTest.java

package scripts; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.*; 

public class BaseTest { 
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>(); 

    @BeforeMethod 
    public void preCondition() { 
     driver.set(new FirefoxDriver()); 
     driver.get().get("https://www.walmart.com/"); 
    } 

    @AfterMethod 
    public void postCondition() { 
     driver.get().quit(); 
    } 

    public final WebDriver driver() { 
     return driver.get(); 
    } 
} 

SignIn.java

package scripts; 

import org.testng.annotations.Test; 

import pom.Introduction; 

public class SignIn extends BaseTest { 

@Test 
public void validSignIn() { 
    Introduction i = new Introduction(driver()); 
    i.signIn(); 
} 
} 

ここでは、012を使用することにしましたこれらのメソッドはすべて@Testメソッドの前後に実行されることが保証されているため、webdriverのインスタンス化とクリーンアップにはと@AfterMethodが必要です。 ThreadLocalはすべてのスレッドがwebdriverの独自のコピーを取得するので、テストを並行して簡単に開始できるので、WebdriverThreadLocalのバリエーションを使用しました。これは今問題ではありませんが、すぐに実装上の構築を開始するとすぐにこの問題に直面します。並列実行に頼る方法の詳細については、this blogの投稿を読んでTestNGを使用して読むことができます。

関連する問題