0
我是JAVA和Selenium的新手,我真的很想理解爲什麼我的代碼不起作用並拋出NullPointerException。NullPointerException當使用一個對象來調用具有Selenium WebDriver代碼的方法
基本上,我想要做的是調用WebDriver實現從不同類到「主測試」類的方法,該類將作爲JUnit測試執行。
但每次我執行我的主測試,然後引發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類從中我得到我的方法:
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?
感謝阿尼什!它的工作原理,我明白爲什麼它沒有工作:) –