我正在尝试执行以下程序但得到空指针错误。其中@test无法访问对象 - 驱动程序。可能是我正在犯的错误。错误 -
JavaScript警告:https://www.google.com/xjs/_/js/k=xjs.s.en_US.MHBUsB8Me90.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,d,csi/am=wCJGjhccAPk_IRQStxAWyAImDiA/rt=j/d=1/t=zcms/rs=ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug,第7行:改变对象的[[Prototype]]会导致代码运行得非常慢;而是使用Object.create创建具有正确的初始[[Prototype]]值的对象 https://www.google.com/?gws_rd=ssl 失败:websiteTitle
java.lang.NullPointerException
at myPackage.TestNGforHDFC.websiteTitle(TestNGforHDFC.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
package myPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNGforHDFC {
public WebDriver driver;
public String urlUnderTest = "http://www.google.com";
public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe";
@BeforeTest
public void login()
{
System.setProperty("webdriver.gecko.driver", projLocation);
WebDriver driver = new FirefoxDriver();
driver.get(urlUnderTest);
System.out.println(driver.getCurrentUrl());
}
@Test
public void websiteTitle()
{
System.out.println(driver.getTitle());
}
}
答案 0 :(得分:2)
类变量driver
永远不会被初始化,您在driver
方法中定义了一个名为login
的新变量。
使用:
public void login(){
driver = new FirefoxDriver();
....
}
而不是:
public void login(){
WebDriver driver = new FirefoxDriver();
....
}