Trabla: Selenium + Java + Chrome Driver ( WebDriver ) : quick setup
Solving:
OS: Windows
ChromeDriver is a separate executable that WebDriver uses to control Chrome.
This tutorial shows how to quickly set up selenium environment to use ChromeDriver.
1. Install Java Development Kit ( JDK )
2. Install Eclipse
3. Run Eclipse and create new Java Project, name it "ChromeDriverQuickStart"
4. Download selenium standalone .jar file
- page - http://www.seleniumhq.org/download/
5. Drag and drop selenium-server-standalone-2.47.1.jar into eclipse project "src" folder
6. Add selenium-server-standalone-2.47.1.jar into build path
7. Download chromedriver from
http://chromedriver.storage.googleapis.com/index.html?path=2.18/
and unzip file.
Copy full path to file chromedriver.exe after unzipping ( will need in step 8 )
e.g. on my PC :
C:\Users\SamuraiKit\Downloads\chromedriver_win32\chromedriver.exe
8. Create new class "ChromeDriverTest" and copy following code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ChromeDriverTest {
public static WebDriver getChromeWebDriver(){
// Path to chromedriver.exe:
// C:\Users\SamuraiKit\Downloads\chromedriver_win32\chromedriver.exe
// Replace to yours
System.setProperty(
"webdriver.chrome.driver",
"/Users/SamuraiKit/Downloads/chromedriver_win32/chromedriver.exe"
);
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1024,768");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
return driver;
}
public static void main(String[] args) {
WebDriver chrome = ChromeDriverTest.getChromeWebDriver();
String url = "https://google.com";
chrome.get(url);
System.out.println("Page title is: " + chrome.getTitle());
}
}
9. Run
No comments:
Post a Comment