Thursday, March 6, 2014

Internet Explorer WebDriver and Desired capabilities

Internet Explorer Webdriver 

Selenium webdriver provides cross browser support thus comes with driver package to run tests on Internet Explorer. Internet explorer webdriver is very complex to use and sometimes can be big pain area. However, we can improve Internet explorer performance by using appropriate capabilities for webdriver.

 Before we proceed further to Internet explorer capabilities we must know how to initialize Internet Explorer webdriver?

First Step would be to download the IEDriverServer for using Internet Explorer driver. without IEDriverServer the Internet explorer driver will not work properly. We can download the IEDriverServer file from this location for both 32 bit and 64 bit OS version. Next is to place this file either in your project path or you can place file at other location and use below code to use IEDriverServer file. Finally, use below code to initialize the Internet Explorer driver.

File file =null;
if (new File("C:\\Program Files (x86)").exists())
{
file = new File( "C:\\IEDriverServer_X64.exe");
}else
{
file = new File( "C:\\IEDriverServer_X86.exe");
}
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());

WebDriver driver = new InternetExplorerDriver(dc);


Internet Explorer Capabilities 


Internet explorer driver supports some important capabilities which can be used to smooth execution of test on Internet Explorer. Some of these capabilities help us to unable java scripts, ignore the security domain setting for IE , Persistent hovering , require window focus etc... These capabilities ease the way the for automation using selenium webdriver on Internet explorer. More details on the Internet explorer can be found here. Below code will guide on how to use the desired capabilities for Internet Explorer driver.

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING,false);
dc.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, false);
dc.setCapability(InternetExplorerDriver.UNEXPECTED_ALERT_BEHAVIOR, true);
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 
dc.setJavascriptEnabled(true); 
WebDriver driver = new InternetExplorerDriver(dc);

Above code will initiate Internet Explorer Driver with the capabilities.This will solve most of the problems with Internet explorer driver. It will be easier to handle to the Internet explorer issues. Some important capabilities of Internet Explorer webdriver are as follows:

ignoreProtectedModeSettings : Whether to skip the protected mode check. If set, tests may become flaky, unresponsive, or browsers may hang. If not set, and protected mode settings are not the same for all zones, an exception will be thrown on driver construction. Only "best effort" support is provided when using this capability.
ignoreZoomSetting : Indicates whether to skip the check that the browser's zoom level is set to 100%. Value is set to false by default.
enablePersistentHover : Determines whether persistent hovering is enabled (true by default). Persistent hovering is achieved by continuously firing mouse over events at the last location the mouse cursor has been moved to.
requireWindowFocus : Determines whether to require that the IE window have focus before performing any user interaction operations (mouse or keyboard events). This capability is false by default, but delivers much more accurate native events interactions.


Friday, January 24, 2014

Firefox Webdriver Profile & Desired Capabilities Setting

What is Desired Capabilities ?


Basically, DesiredCapabilities help to set properties for the WebDriver. A typical usecase would be to set the path for the FirefoxDriver if your local installation doesn't correspond to the default settings. capabilities describes a series of key/value pairs that encapsulate aspects of a browser.

FireFox WebDriver and Desired capabilities


Usually we use below code to initiate FireFox driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

Webdriver driver =  new FirefoxDriver();

Now, we need to learn about how to setup custom profile or capabilities of Firefox. So that we can have desired preference for browser. Someone may want to have Firefox accept site certificates and may want to file download to specific location without and popup for save etc..Selenium webdriver helps in setting desired capabilities for FireFox browser.

For setting desired capabilities we need to use below code.

import org.openqa.selenium.remote.DesiredCapabilities; 

DesiredCapabilities dc=DesiredCapabilities.firefox();
Webdriver driver =  new FirefoxDriver(dc);


FireFox Profile 


We can create custom FireFox profile and use it with desired capabilities for example :

import org.openqa.selenium.firefox.FirefoxProfile;

FirefoxProfile profile = new FirefoxProfile();

Example to use Firefox profile with desired capabilities

DesiredCapabilities dc=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver =  new FirefoxDriver(dc);

Thus the webdriver will get initilized with desired capabilities & profile set.

What preferences to use in firefox profile?


Here is the interesting question, now we know that how to set Firefox profile? What is the use of it? or which preferences can be used with it?  The answer is simple but we need to understand what does the preferences mean here and how we can get to know them.

Preferences can be some setting of Firefox browser specific to the download or any other type of preffred settings. To get the preferences you can open Firefox browser and type "about:config" in url and hit enter. you will get the list of all the Firefox prerences list and Settings.

Now we need to use them in our code for setting Firefox profile for example :

FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.alwaysAsk.force", false); 



Finally you can combile all the things above and initiate FireFox driver as shown in below example:


FirefoxProfile profile = new FirefoxProfile();
DesiredCapabilities dc=DesiredCapabilities.firefox();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false); profile.setPreference("browser.download.dir", "C:\Downloads"); profile.setPreference("browser.download.downloadDir","C:\Downloads"); profile.setPreference("browser.download.defaultFolder","C:\Downloads"); profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/anytext ,text/plain,text/html,application/plain" );
dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver =  new FirefoxDriver(dc);



Above code will initilise firefox driver which will never ask for file download for file MIME type mentioned. We can add more file MIME types in above code as per requirenment. File MIME type can be found here.

For more information on desired capabilities please refere this link.

Selenium WebDriver 2.0

What is selenium ?

Selenium is a set of different software tools each with a different approach to supporting test automation.The entire suite of tools results in a rich set of testing functions specifically geared to the needs of testing of web applications of all types. These operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior. One of Selenium’s key features is the support for executing one’s tests on multiple browser platforms.


History:


Selenium first came to life in 2004 when Jason Huggins was testing an internal application at ThoughtWorks. Being a smart guy, he realized there were better uses of his time than manually stepping through the same tests with every change he made. He developed a Javascript library that could drive interactions with the page, allowing him to automatically rerun tests against multiple browsers. That library eventually became Selenium Core, which underlies all the functionality of Selenium Remote Control (RC) and Selenium IDE. Selenium RC was ground-breaking because no other product allowed you to control a browser from a language of your choice.


Webdriver 2.0 :

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand, which will help make your tests easier to read and maintain.Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.

Selenium webdriver can be used with diffrent browser and how it drives the browser means make native call to browser is entirely depends on the browser in use. Webdriver provides very easy to use APIs and can be configured with variety of programming language to write test cases. 

Webdriver can supports the following web browsers:
Internet Explorer,Firefox,chrome,safari, opera.

Languages you can use with webdriver are:
.Net,Java,Perl,PHP,Python,Ruby.

References:  Selenium