JUnit (Selenium WebDriver) to open www.google.co.uk in Chrome Browser
I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know, Which PATH should I place that? or How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
@Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver
as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it
parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
@BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
@Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "yourpathtoit");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:Seleniumdriverchromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
链接地址: http://www.djcxy.com/p/52326.html