【发布时间】:2016-11-23 19:45:17
【问题描述】:
我是 JAVA 和 Selenium 的新手,我真的很想了解为什么我的代码不起作用并且会抛出 NullPointerException。
基本上,我想要做的是将具有不同类的 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?
【问题讨论】:
标签: java selenium junit nullpointerexception