我编写了下面的代码来在 Chrome 浏览器中打开一个网站并验证其标题。但是当使用 System.setProperty() 来设置 ChromeDriver 路径时,它会给我语法错误,当我评论该行时,我得到:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property..
我的代码:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class FirsttestNGFile {
String BaseURL = "http://newtours.demoaut.com/";
System.setProperty("webdriver.chrome.driver", "E:\\Automation Jars\\chromedriver_win32\\chromedriver.exe"); -- If I comment this line, I get Illegal state Exception for chromedriver path; if not commented , I get syntax error
WebDriver driver = new ChromeDriver();
@Test
public void verifyHomePageTitle() {
driver.get(BaseURL);
String ExpectedTitle = "Welcome: Mercury Tours";
String ActualTitle = driver.getTitle();
Assert.assertEquals(ExpectedTitle, ActualTitle);
driver.quit();
}
}
最佳答案
您无法全局定义System.setProperty
。
使用下面的代码并尝试:
WebDriver driver;
@Before
public void browser(){
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
}
@Test
public void verifyHomePageTitle() {
String BaseURL = "http://newtours.demoaut.com/";
driver.get(BaseURL);
String ExpectedTitle = "Welcome: Mercury Tours";
String ActualTitle = driver.getTitle();
Assert.assertEquals(ExpectedTitle, ActualTitle);
}
@Test
public void a() {
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=6PDbV-qTAZHT8gecr4qQBA");
}
@After
public void close(){
driver.quit();
}
}
如果您使用的是 Junit
,则使用 @Before
,如果您使用的是 TestNG
,则使用 @BeforeTest
。
回复我以进一步询问。 快乐学习。 :-)
关于java - 无法使用 ChromeDriver 在 Eclipse 中运行 TestNG 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39529373/