开始测试时,我在Chrome地址栏中看到“ data:”。 我正在用TestNG创建Maven项目,仅尝试打开google index页面
public class GetBrowser {
protected WebDriver driver;
@Parameters("browser")
@BeforeTest
protected WebDriver getDriver(String browser){
if (browser.equals("chrome")){
System.setProperty("webdriver.chrome.driver", "/home/user/IDEA/drivers/chromedriver");
driver = new ChromeDriver();
}
else if (browser.equals("firefox")){
System.setProperty("webdriver.gecko.driver", "/home/user/IDEA/drivers/geckodriver");
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
public class OpenIndexPageTest extends GetBrowser {
@Test
public void openPage(){
driver.get("https://www.google.com");
String titleActual = driver.getTitle();
String titleExpected = "Google";
Assert.assertEquals(titleActual, titleExpected);
}
}
我的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ChromeTestExample">
<test name="ChromeTest">
<parameter name="browser" value="chrome"/>
<packages>
<package name="com.fozzy.autotest.*"/>
</packages>
</test>
</suite>
此外,我还从maven存储库(最新版本为org.testng和org.seleniumhq.selenium)的pom.xml中添加了依赖项
IDEA返回 java.lang.NullPointerException。而且我不知道问题出在哪里。
跟踪
Starting ChromeDriver 2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5) on port 11290
Only local connections are allowed.
Dec 16, 2018 12:48:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Starting ChromeDriver 2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5) on port 15730
Only local connections are allowed.
Dec 16, 2018 12:48:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
java.lang.NullPointerException
at com.fozzy.autotest.OpenIndexPageTest.openPage(OpenIndexPageTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
信息:
ChromeDriver 2.45(最新版本)
Selenium-java 3.141.59(最新发行版)
testng 6.14.3(最后一次不是beta版本)
java版本“ 1.8.0_191”
IDEA 2018.3.1(CE)
请帮助,谢谢!
答案 0 :(得分:0)
问题出在您的测试代码上。您正在使用@BeforeTest
注解实例化您的浏览器。 TestNG每个@BeforeTest
标签仅运行一次<test>
注释方法。
因此,如果您的程序包包含一个以上的测试类,则不会从第二个类开始到GetBrowser
执行基类NullPointerException
的驱动程序实例化逻辑。
请改用@BeforeClass
或@BeforeMethod
批注。