在Chrome中通过Selenium webDriver运行测试用例时出现java.lang.NullPointerException
我的类使用SeleniumWebDriver并抛出错误在Chrome中通过Selenium webDriver运行测试用例时出现java.lang.NullPointerException,java,selenium-webdriver,Java,Selenium Webdriver,我的类使用SeleniumWebDriver并抛出错误 public class Test_Chromedriver { public static WebDriver driver; public static void main(String[] args) { try { System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe"); WebDr
public class Test_Chromedriver {
public static WebDriver driver;
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).sendKeys("surya");
driver.findElement(By.name("pass")).sendKeys("hello");
} catch (Exception e) {
e.printStackTrace();
}
finally{
driver.close();
driver.quit();
}
}
}
一旦我运行了上面的代码,下面的错误消息就会出现:
Started ChromeDriver
port=12877
version=23.0.1240.0
log=D:\Eclipse Juno Workspace\AS Selenium workspace\WebDriver Applications\chromedriver.log
Exception in thread "main" java.lang.NullPointerException
at Test_Chromedriver.main(Test_Chromedriver.java:25)
注意:webdriver打开chrome浏览器,然后在字段中输入值,但上面的错误一直显示在控制台中,最后阻止也没有执行。您的问题来自于您的
try/catch
,特别是您的finally
因为您的驱动程序
仅位于中的本地,所以请尝试{}
以便。。。您的最终
无法对您的驱动程序执行操作。我将重命名你的驱动程序,以帮助你了解这里发生了什么
public class Test_Chromedriver {
public static WebDriver driverNULL; // <- never instantiate
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
WebDriver driverOK=new ChromeDriver(); // < - local variable
driverOK.get("http://www.facebook.com");
driverOK.findElement(By.id("email")).sendKeys("surya");
driverOK.findElement(By.name("pass")).sendKeys("hello");
} catch (Exception e) {
e.printStackTrace();
}
finally{
driverNULL.close(); // <- want to close a object never instantiate so null pointer exception
driverNULL.quit();
}
}
}
公共类测试\u色度驱动程序{
public static WebDriver driverNULL;//您的麻烦来自您的try/catch
,特别是您的finally
因为您的驱动程序
仅位于中的本地,请尝试{}
,因此…您的最终
无法处理您的驱动程序。我将重命名您的驱动程序,以帮助您查看此处发生的情况
public class Test_Chromedriver {
public static WebDriver driverNULL; // <- never instantiate
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe");
WebDriver driverOK=new ChromeDriver(); // < - local variable
driverOK.get("http://www.facebook.com");
driverOK.findElement(By.id("email")).sendKeys("surya");
driverOK.findElement(By.name("pass")).sendKeys("hello");
} catch (Exception e) {
e.printStackTrace();
}
finally{
driverNULL.close(); // <- want to close a object never instantiate so null pointer exception
driverNULL.quit();
}
}
}
公共类测试\u色度驱动程序{
公共静态WebDriver driverNULL;//是否可以尝试使用TestNG和注释
他们的工作做得相当不错
使用这种类型的结构将为测试和执行的维护提供良好的准备
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Chromedriver {
public static WebDriver driver;
@BeforeClass
public void setUp(){
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe"");
driver=new ChromeDriver();
}
@Test
public static void TestFacebook() {
try {
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).sendKeys("surya");
driver.findElement(By.name("pass")).sendKeys("hello");
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown(){
driver.close();
driver.quit();
}
}
也许尝试使用TestNG和注释
他们的工作做得相当不错
使用这种类型的结构将为测试和执行的维护提供良好的准备
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test_Chromedriver {
public static WebDriver driver;
@BeforeClass
public void setUp(){
System.setProperty("webdriver.chrome.driver","E:\\jars\\chromedriver.exe"");
driver=new ChromeDriver();
}
@Test
public static void TestFacebook() {
try {
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).sendKeys("surya");
driver.findElement(By.name("pass")).sendKeys("hello");
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown(){
driver.close();
driver.quit();
}
}
这不是错误,这是异常。请注意,异常告诉您它发生在第25行。请编辑您的问题,指出哪一行是第25行。这不是错误,这是异常。请注意,异常告诉您它发生在第25行。请编辑您的问题,指出哪一行是第25行。感谢您的回复和指导。现在我可以在没有“NullPointerException”的情况下运行代码命名的好主意-我也有类似的问题,并且知道你的意思。谢谢你的回复和指南。现在我可以在没有“NullPointerException”的情况下运行代码命名的好主意-我也有类似的问题,我知道你的意思。谢谢你的回复。只是我安装了TestNG并使用TestNG运行了上面的测试用例。现在我可以在没有“NullPointerException”的情况下运行测试用例了。谢谢你的回复。我刚刚安装了TestNG并使用TestNG运行了上面的测试用例。现在我可以在没有“NullPointerException”的情况下运行测试用例了。