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