我的代码是:
package ant;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
public class NewTestNG {
public WebDriver driver;
@BeforeMethod
public void LAunchbrowser() {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void main() {
Actions action = new Actions(driver);
WebElement a= driver.findElement(By.xpath(".//*[@id='gs_htif0']"));
action.moveToElement(a).click().sendKeys("Shirt").build().perform();
driver.findElement(By.xpath("//div[@value='Search']")).click();
}
}
我得到NullPointerException:
FAILED: main
java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
at ant.NewTestNG.main(NewTestNG.java:25)
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)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
我的代码出了什么问题?
答案 0 :(得分:2)
在LaunchBrowser()
中,您再次声明driver
而不是使用类实例。将其更改为
@BeforeMethod
public void LaunchBrowser() {
driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
它应该有效。问题是,当您在driver
中声明LaunchBrowser()
时,该变量的范围仅在方法内部,因此类变量driver
不会被使用。因此,当您在方法之外并尝试引用driver
时,它会null
...因此例外。
您真的需要花些时间学习如何调试自己的程序。如果你在脚本的开头加上一个断点并逐步完成它,你应该能够自己找到它。