Sunday, 12 October 2014

Selenium Webdriver commands to Initialize Browser like Firefox, Safari, IE, Opera and Run

Command for Firefox/Safari driver

In my earlier blogs we have already used some commands to initialize the firefox driver.
Here is the command to initialize Firefox driver.
WebDriver driver = new FirefoxDriver();

For Safari also, we don't need to do much things. There also single line coding will work. But will work good with Mac Osx. Here is the command for safari browser.
WebDriver driver = new SafariDriver();

Like we did for Firefox/Safari driver, same will not work directly for other browsers like Chrome, Opera, Internet Explorer etc. We need few additional stuffs there.

We need to download the driver files first for other browsers. Lets start with IE browser first.

Steps and command for IE driver

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “The Internet Explorer Driver Server” section.


Step 3: Now you can download the IE driver zip file of 32 or 64 bit as per requirement.
Step 4: Extract the zip file and save the 'IEDriverServer.exe' in a proper place (say here: C:\\Users\\debasisa\\Desktop\\IEDriverServer.exe )


Now, here is the code to initialize the IE driver:

public static void main(String[] args){

File file = new File("C:\\Users\\debasisa\\Desktop\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}


Explanation:
1. In first line, we created file object which holds the path for the IE driver.
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the IE driver.
4. Now Gmail instance is opened in IE browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the IE browser.

*Note:
It will not work on all versions on IE. Supported versions are from IE 6 to IE 11, where as for Version 11, we need some additional settings in Registry file.


Steps for Additional setting to run in IE11:

Step 1: Go to Start menu of Windows system, click Run and type 'REGEDIT'
Step 2: It will open registry edit window.
Step 3: There go to the below folder path.

For 64 bit machine HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\InternetExplorer\Main\FeatureControl\
FEATURE_BFCACHE

For 32 bit machine
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InternetExplorer\Main\FeatureControl\FEATURE_BFCACHE

The last folder mentioned 'FEATURE_BFCACHE' might not present there. So, create a new Key there with this name.



Step 4: Now in that 'FEATURE_BFCACHE' folder, create a 'DWORD' with name: 'iexplore.exe' & value 0 .



Step 5: Now we are all set. We can run our same piece of code in Eclipse, it will work in IE 11 as well.


*Note:
But running code in IE 11 is bit slower. Through sendkeys when we pass values like Email/Password etc., we can see that it will type very slowly. And it is because of 64bit driver.

Try using 32 bit IE driver file. Though it is not much compatible with Win 64 bit system, but this slow responsiveness issue can be resolved by this 32 bit IE driver server.

We need all these additional settings, because IE driver server doesn't support directly for IE 11.
But currently IE 11 driver server is available to download, but that is for Win8.
If you are on Win8, it is worth trying.
You can download IE 11 driver server from here.


Steps and command for Chrome driver

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “Third Party Drivers, Bindings, and Plugins” section.



Step 3: Click on the version no. and then choose the link as per need like Win/Mac/Linux etc. And then download the zip file.
Step4: Extract and save the “chromedriver.exe” file in a proper place (say here: C:\\Users\\debasisa\\Desktop\\chromedriver.exe )


Now, here is the code to initialize the Chrome driver:

public static void main(String[] args){

File file = new File("C:\\Users\\debasisa\\Desktop\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}

Explanation:
1. In first line, we created file object which holds the path for the Chrome driver. 
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the Chrome driver.
4. Now Gmail instance is opened in Chrome browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the chrome browser.




Steps and command for Opera driver
Like we downloaded the driver file for Chrome, we can download the opera driver file in the same way.

But here after extract, we will get a .jar file. We need to add that jar file to build path.
Here are the steps below.

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “Third Party Drivers, Bindings, and Plugins” section.

Step 3: Click on the version no. to download. Here, it will download the .jar file, rather than .exe file.
Step 4: Now, we need to add that jar file to the build path of our project in Eclipse.
Here are the steps mentioned below to add the jar file.

Open Eclipse → Go to Project → Right click → Build Path → Configure Build Path → Add External JARs → Then select the downloaded Opera driver jar file → Click Ok to save that

After adding jar file in build path, we can view the same.

                             


Step 5: Now copy the path of 'opera.exe' file. It is the launcher file, what we got after installation of opera browser, mostly the location is in program files. 

Say the path is: “C:\\Program Files (x86)\\Opera\\opera.exe” .

Now here is the code to initialize opera driver and run the program in opera browser.



public static void main(String[] args){

File file= new File("C:\\Program Files (x86)\\Opera\\opera.exe");
System.setProperty("webdriver.opera.driver", file.getAbsolutePath());
WebDriver driver = new OperaDriver();

driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}

Explanation:

1. In first line, we created file object which holds the path for the Opera browser. Here it is the path of 'opera.exe' which we saved in step 5 above.
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the Opera driver.
4. Now Gmail instance is opened in Opera browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the opera browser.


*Note:
It will work only for opera versions 12.x or lower than that. For higher versions of opera, it won't work.


No comments :

Post a Comment