Sunday, 27 October 2013

How to launch chrome browser using selenium webdriver in Java?

Some important links related to ChromDriver

  1. Download chromedriver at http://chromedriver.storage.googleapis.com/index.html
  2. Release information of ChromeDriver at https://sites.google.com/a/chromium.org/chromedriver/downloads 
  3. https://sites.google.com/a/chromium.org/chromedriver/capabilities
  4. https://sites.google.com/a/chromium.org/chromedriver/home
  5. 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();
              }     
       }   
}

Please note that we need to set the system property webdriver.chrome.driver
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.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java
What do you think on above selenium topic. Please provide your inputs and comments. Thanks

No comments:

Post a Comment

Buy Best Selenium Books