[英]java.lang.NullPointerException - selenium java testng
yesterday my test works ok, bust i want to use testng and then appeared a problem.昨天我的测试工作正常,但我想使用 testng 然后出现了问题。 My main class leadTest:
我的主课leadTest:
public class leadTest {
WebDriver driver;
@Test
public void f() {
..
...
...
/*------------------------Go to leads page-------------------------------*/
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
}
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.get("http://getbase.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
and my class LeadsPage:和我的类 LeadsPage:
public class LeadsPage {
WebDriver driver;
String expStatus = "new";
@FindBy(id="leads-new")
WebElement addLead;
@FindBy(className = "lead-status")
WebElement UserStatus;
public void addNewLead(){
addLead.click();
}
public void checkUsrStat(){
String stat = UserStatus.getText().toLowerCase();
Assert.assertEquals(stat, expStatus.toLowerCase());
}
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
public LeadsPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
I have problem with this part from above:我对上面的这部分有问题:
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
I have error我有错误
AILED: f
java.lang.NullPointerException
at leadTest.LeadsPage.findLeadByName(LeadsPage.java:38)
at leadTest.leadTest.f(leadTest.java:101)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
When i use当我使用
@FindBy(partialLinkText="nameToFind")
WebElement ntf;
and和
ntf.click();
its works, but i cant do that, becouse in place nameToFind will be variable.它的工作原理,但我不能这样做,因为在地 nameToFind 将是可变的。
And i dont know what is wrong... Please help me我不知道出了什么问题...请帮帮我
Driver variable not initialized: you try to call findElement() method on a null object.驱动程序变量未初始化:您尝试对空对象调用 findElement() 方法。
One way to confirm that is to print the driver value:确认这一点的一种方法是打印驱动程序值:
System.out.println("driver=" + driver);
just before the exception is thrown就在抛出异常之前
public void findLeadByName(String leadName){
System.out.println("driver=" + driver);
driver.findElement(By.partialLinkText(leadName)).click();
}
also i am noticing ,我也注意到,
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
gotoLeadsPage(); gotoLeadsPage(); and findLeadByName();
和 findLeadByName(); both functions are in same class LeadsPage but using different objects dbp and ldsp
两个函数都在同一个类 LeadsPage 中,但使用不同的对象 dbp 和 ldsp
May be below changes will work.可能下面的更改会起作用。
in LeadsPage class change your findLeadByName();在 LeadsPage 类中更改您的 findLeadByName(); function as follows,
功能如下,
public void findLeadByName(WebDriver driver, String leadName){
this.driver.findElement(By.partialLinkText(leadName)).click();
}
and in your class leadTest call function findLeadByName() as follows,并在您的类 LeadTest 调用函数 findLeadByName() 中,如下所示,
LeadsPage ldsP = new LeadsPage(driver);
ldsP.findLeadByName(driver ,"nameToFind");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.