我在使用TestNG在Selenium和Java中运行代码时遇到以下错误。在多个博客/网站上提到清理项目,所以我做了Project-> Clean但是它仍然给我这个错误。有人可以指出我这个代码有什么问题吗?感谢。
package firsttestngpackage;
//import org.testng.annotations.Test;
//import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
//import org.testng.asserts.*;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestNGFile {
@BeforeSuite
public void SetBrowser(){
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
}
public WebDriver driver1 = new ChromeDriver();
public String baseurl = "http://newtours.demoaut.com/";
public String ExpTitle = "Welcome: Mercury Tours";
@Test
public void CheckPageTitle() {
driver1.get(baseurl);
String ActTitle = driver1.getTitle();
Assert.assertEquals(ActTitle, ExpTitle);
driver1.quit();
}
}
异常:
org.testng.TestNGException:
Cannot instantiate class firsttestngpackage.FirstTestNGFile
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:89)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
at firsttestngpackage.FirstTestNGFile.<init>(FirstTestNGFile.java:21)
... 26 more
答案 0 :(得分:1)
我已经对上面的代码进行了修改,现在工作正常......
问题是由于驱动程序实例范围是在方法中定义的。
package firsttestngpackage;
//import org.testng.annotations.Test;
//import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
//import org.testng.asserts.*;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestNGFile {
public WebDriver driver1 ;
@BeforeSuite
public void SetBrowser(){
System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");
driver1= new ChromeDriver();
}
public String baseurl = "http://newtours.demoaut.com/";
public String ExpTitle = "Welcome: Mercury Tours";
@Test
public void CheckPageTitle() {
driver1.get(baseurl);
String ActTitle = driver1.getTitle();
Assert.assertEquals(ActTitle, ExpTitle);
driver1.quit();
}
}