2016-11-23 8 views
0

私はJAVAとSeleniumが初めてで、コードが機能せず、NullPointerExceptionがスローされる理由を本当に理解したいと思います。オブジェクトを使用してSelenium WebDriverコードを持つメソッドを呼び出すとき

基本的に、私はJUnitテストとして実行される "Master Test"クラスとは異なるクラスのWebDriver実装を持つメソッドを呼び出したいと考えています。

しかし、私がマスターテストを実行するたびに、NullPointerExceptionがスローされます。ここでは、拡張されている構成クラスである

package common_methods; 

import org.junit.*; 

public class Master_Test extends Configurations { 

    @Before 
    public void setUp(){ 
     try{ 
     testConfiguration(); 
     driver.get("http://only-testing-blog.blogspot.ro/"); 
     } catch (Exception e){ 
      System.out.println("setUp Exception: "+e); 
     } 
    } 

    @After 
    public void tearDown(){ 
     driver.close(); 
    } 

    @Test 
    public void masterTest(){ 
     try{ 
     TestCase1 testy1 = new TestCase1(); 
     testy1.test1(); 
     }catch (Exception master_e){ 
      System.out.println("Test Exception: "+master_e); 
     } 
    } 
} 

今よりよく理解するために:

は、ここで実行されます、私のマスターテストだ

package common_methods; 

import org.openqa.selenium.*; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class Configurations { 

    public WebDriver driver; 

    public void testConfiguration() throws Exception{ 
     System.setProperty("webdriver.chrome.driver", "D:\\Browser_drivers\\chromedriver_win32\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } 
} 

は、そして、ここでTestCase1クラスであるから、I私のメソッドを得る:

package common_methods; 

import org.openqa.selenium.*; 

public class TestCase1 extends Configurations{ 

    public void test1() throws Exception{ 
     driver.findElement(By.id("tooltip-1")).sendKeys("Test Case 1 action"); 
     Thread.sleep(5000); 
    } 
} 

なぜNullPointerExceptionを取得するのですか?あなたはそれがNullPointerExceptionが得られないように、ドライバの参照を渡す必要があり

TestCase1 testy1 = new TestCase1(); 

を呼び出しているMaster_Test、で

答えて

0

。また、ドライバを処理するために、TestCase1クラスにコンストラクタを追加する必要があります。

以下のコードを確認してください。

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Configurations { 

    public WebDriver driver; 

    public void testConfiguration() { 
     driver = new FirefoxDriver(); 
    } 
} 

Master_Test

import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.WebDriver; 



public class Master_Test extends Configurations { 

    @Before 
    public void setUP() { 
     testConfiguration(); 
     driver.get("http://www.google.com"); 
    } 

    @Test 
    public void masterTest() { 
     TestCase1 test = new TestCase1(driver); 
     test.test1(); 
    } 
} 

TestCase1

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 

public class TestCase1 extends Configurations { 

    public TestCase1(WebDriver driver) { 
     this.driver = driver; 
    } 

    public void test1() { 
     driver.findElement(By.id("lst-ib")).sendKeys("test"); 
    } 
} 
+0

おかげアニッシュを - 私はFirefoxDriver &グーグルでそれを使用しています!それは動作し、私はそれが動作しなかった理由を理解する:) –

関連する問題