Some important links related to ChromDriver
Desired Capabilities of ChromeDriver
Chrome can be configured in various ways as mentioned by Google developer.
ChromeOptions options = new ChromeOptions();
//using chrome binary from specific location
options.setBinary("/path/to/other/chrome/binary");
//Setting the chrome profile
options.addArguments("user-data-dir=/path/to/your/profile");
//to disable pop up blocking
options.addArguments("disable-popup-blocking");
//To start chrome without security warning
options.addArguments("test-type");
//To start the chrome in Maximized mode
options.addArguments("start-maximized");
//Setting custom options
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
//Adding extensions to chrome
options.addExtensions(new File("/path/to/extension.crx"));
We can directly pass the options to driver as shown below
driver = new ChromeDriver(options);
Or we can also set the capabilities as shown below
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
Mobile emulation using ChromeDriver
You can start the chrome in mobile emulation mode by passing below capabilities.Please note that you can pass any device name as shown in chrome developer tools.
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 7");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
Using RemoteWebDriver and ChromeDriver
You can start the chromedriver as shown below.
$ ./chromedriver
Started ChromeDriver
port=9515
Then you can use below code to instantiate the driver.
WebDriver driver = new RemoteWebDriver("http://127.0.0.1:9515", DesiredCapabilities.chrome());
driver.get("http://www.softpost.org");
ChromeDriver Example
You can use below example/code to start the new chrome session in Java using selenium web driver.
Please note that we need to set the system property webdriver.chrome.driver
- Download chromedriver at http://chromedriver.storage.googleapis.com/index.html
- Release information of ChromeDriver at https://sites.google.com/a/chromium.org/chromedriver/downloads
- https://sites.google.com/a/chromium.org/chromedriver/capabilities
- https://sites.google.com/a/chromium.org/chromedriver/home
- https://sites.google.com/a/chromium.org/chromedriver/capabilities#TOC-Using-a-Chrome-executable-in-a-non-standard-location
Desired Capabilities of ChromeDriver
Chrome can be configured in various ways as mentioned by Google developer.
ChromeOptions options = new ChromeOptions();
//using chrome binary from specific location
options.setBinary("/path/to/other/chrome/binary");
//Setting the chrome profile
options.addArguments("user-data-dir=/path/to/your/profile");
//to disable pop up blocking
options.addArguments("disable-popup-blocking");
//To start chrome without security warning
options.addArguments("test-type");
//To start the chrome in Maximized mode
options.addArguments("start-maximized");
//Setting custom options
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
//Adding extensions to chrome
options.addExtensions(new File("/path/to/extension.crx"));
We can directly pass the options to driver as shown below
driver = new ChromeDriver(options);
Or we can also set the capabilities as shown below
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
Mobile emulation using ChromeDriver
You can start the chrome in mobile emulation mode by passing below capabilities.Please note that you can pass any device name as shown in chrome developer tools.
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 7");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
Using RemoteWebDriver and ChromeDriver
You can start the chromedriver as shown below.
$ ./chromedriver
Started ChromeDriver
port=9515
Then you can use below code to instantiate the driver.
WebDriver driver = new RemoteWebDriver("http://127.0.0.1:9515", DesiredCapabilities.chrome());
driver.get("http://www.softpost.org");
ChromeDriver Example
You can use below example/code to start the new chrome session in Java using selenium web driver.
package seleniumtest;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.Keys;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.*;
import
org.openqa.selenium.interactions.Action;
import
org.openqa.selenium.interactions.Actions;
public class MainTest {
public static void main(String[] args)
{
WebDriver driver =null;
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\csharp\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
try{
driver.get("http://register.rediff.com/register/register.php");
driver.findElement(By.name("name")).sendKeys("ff89");
Thread.sleep(4000);
//driver.navigate();
//driver.navigate("http://www.google.com");
}catch(Exception e){
System.out.println(e.toString());
}
finally{
driver.close();
driver.quit();
}
}
}
We have to give the path of chromedriver exe
Once you have created driver object, you can use its methods to automated the browser tasks.
You may also like to read below topics.
Once you have created driver object, you can use its methods to automated the browser tasks.
You may also like to read below topics.
- Press key in Selenium Webdriver in Java
- Read Table Data in Selenium Webdriver in Java
- Take the screenshot in Selenium Webdriver in Java
- Execute JavaScript in Selenium Webdriver in Java
- Handle multiple browser windows in Selenium Webdriver in Java
- Double-Click on the Element in Selenium Webdriver in Java
- Exceptions in Selenium Webdriver in Java
- Synchronization in Selenium Webdriver in Java
No comments:
Post a Comment