My Thoughts: ChromeDriver
Showing posts with label ChromeDriver. Show all posts
Showing posts with label ChromeDriver. Show all posts

Thursday, September 12

Seleinum Webdriver - Loading chrome browser with default extension

When we start chrome browser using webdriver then by default it will load with out any extensions. But we can also load chrome browser with some default extensions if we need . To do this we need to know .crx file path of chrome extension. Here are the steps to find out the .crx file path :

1.Open chrome browser and type chrome://extensions/ . Now note down the extension id that you would like to be loaded when your browser starts.

2.Check the "Developer Mode" checkbox.Now you will see "Pack Extension" button .

3.Go to Extenstion folder of chrome .You will see the list of other folders with unique id's. Go to folder with the id , which you got in Step 1. Go deep into the folder until you find the manifest.json file and then copy the location .
3.Click on "Pack Extension" button and enter the path of folder in which extension manifest.json file is located.

Click on Pack Extension button.

4.We will get confirmation alert which displays the path of .crx file of extension. Copy that path and use it in your script

Thats it. Now we are done with the .crx file path . Use that path in the below code to load chrome browser with the default extension.
import java.io.File;
	import org.openqa.selenium.WebDriver;
	import org.openqa.selenium.chrome.ChromeDriver;
	import org.openqa.selenium.chrome.ChromeOptions;
    import org.testng.annotations.Test;

	@Test
	public class Chrome {

		WebDriver  driver;  
		
		public void start(){	
			ChromeOptions options = new ChromeOptions();		
			options.addArguments("start-maximized");
			options.addExtensions(new File("CRX FILE PATH"));
			System.setProperty("webdriver.chrome.driver","PATH of chromedriver.exe");
			ChromeDriver driver = new ChromeDriver(options);
                        driver.get("http://mythoughts.co.in");		
		}
}

Referenes:
http://developer.chrome.com/extensions/packaging.html
http://www.chromium.org/user-experience/user-data-directory


Thursday, May 10

Starting Chrome Browser using WebDriver(Selenium2) with example


If we would like to run our scripts on Chrome we need "chromedriver"along with the actual Chrome Browser.If you try to start the Chrome Browser with the below statement alone
WebDriver driver = new ChromeDriver();

Then you will notice below error statements

"You need to have both chromedriver and a version of chrome browser installed
@BeforeTest setUpDriver
java.lang.IllegalStateException: 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. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
at com.google.common.base.Preconditions.checkState(Preconditions.java:172)
atorg.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:114)"

In our scripts if we dont specify where is chromedriver is located then we cant start chrome browser using WebDriver.

Here are steps to setup chromedriver

1.Download chromedriver from
http://code.google.com/p/chromedriver/downloads/list
2.Unzip the file .Say you downloaded it on your desktop folder named "Automation" and unzipped it then location will be like
C:\\Users\\<<User Name>>\\Desktop\\Automation\chromedriver_win_19.0.1068.0\\chromedriver.exe
3.Use the below code to start chrome
System.setProperty("webdriver.chrome.driver","C:\\Users\\<<UserName>>\\Desktop\\Automation\\chromedriver_win_19.0.1068.0\\chromedriver.exe"); 
ChromeDriver driver = new ChromeDriver();

Now you are done. :)

If you are using the TestNG framework then here is the sample code :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class chromeDriver {
 
WebDriver  driver;

@BeforeTest
 public void setUpDriver(){
   System.setProperty("webdriver.chrome.driver",
"C:\\Users\\<>\\Desktop\\Automation\\chromedriver_win_19.0.1068.0\\chromedriver.exe");  
   driver = new ChromeDriver(); 
     }

 @Test
 public void start(){
 driver.get("http://www.google.com/"); 
   }

}