I recommend to download Download Google_Search.zip file.
I was refactoring code of an ASP.NET application and had to repeatedly test some web pages in three different browsers Mozilla, Chrome and Internet Explorer (IE). The testing was becoming very tedious job and as a lazy programmer I was looking for an easy and free of cost solution to automate web page testing. I searched online and hit gold in form of Selenium. In this article I am giving a short introduction to Selenium-WebDriver which is small part of Selenium. Selenium provides a full scale solution to do continuous integration testing for you web applications and manage test cases efficiently.
Is a tool suite used to automate web browsers by allowing browsers to be controlled by many programming languages and testing frameworks. It's supported in many browsers and platforms. The primary purpose of Selenium is to automate testing of web applications and can also be used for automating repeated web-based administration tasks. The diagram below shows Selenium components.
It's a Firefox plugin used to record user interactions with Firefox and can also play back allowing to create simple scripts or assist in exploratory testing.
The Selenium Server was previously known as Selenium RC(Remote Control).
The WebDriver was introduced in Selenium 2.0 version it provides a simpler programming interface with well-designed object-oriented API. It directly calls to browser using browser’s native support for automation and features they support depend on browser you are using. If your browser and tests will all run on same machine and your tests only use WebDriver API, then you do not need to run Selenium-Server.
Selenium-Server is used with Selenium-WebDriver in following scenarios.
- For using Selenium-Grid to distribute your tests over multiple machines or virtual machines.
- Connect to a remote machine that has a particular browser version that is not on your current machine.
- When you use only HtmlUnit Driver and not using any JAVA bindings (i.e. Python, C#, JAVA or Ruby).
Except for Firefox all other browsers have separate WebDriver executable file which communicates with browser on behalf WebDriver API.
For more details refer Selenium Documentation.
Using the code
To demonstrate Selenium-WebDriver capabilities I have written a simple console application that takes two command line parameters. The first parameter is browser type you want to test your web page on and search keyword to search in Google search page. The application will open chosen browser navigate to Google search page and type the search keyword given and hit on search button.
Running sample program is a two-step process.
Step 1: Setup Selenium WebDriver in your machine
By default Firefox browser supports WebDriver, for Chrome and Internet Explorer 32 and 64 bit WebDriver has been provided as EXE file which is provided along with source code given above for easy setup, to get latest version you may need to visit Selenium web site at any given point in time you will be able to use only Internet Explorer 32 or 64 bit as driver names are same.
To setup the driver create a folder in your drive say C:\WebDriver copy respective driver EXE(s) to this folder. Now add this folder path to PATH environment variable in your machine. This is required as WebDriver API will search for the respective EXE to open web browser.
Step 2: Run the application by providing arguments
Now you have two option to run the application one is to run from Visual Studio go to Project>Properties>Debug tab provide command line arguments as shown below or you can go to command prompt, navigate to debug folder and provide the arguments. The screen shot below shows command line arguments to search for Selenium keyword in google on Firefox browser.
How application works
First application parses command line arguments and identifies browser to use and search keyword. Then it creates the instance of respective web browser by calling constructor of respective WebDriver's API as shown below.
IWebDriver browser = null;
string uCaseBrowser = browserType.ToUpperInvariant();
browser = new FirefoxDriver();
browser = new ChromeDriver();
InternetExplorerOptions opt = new InternetExplorerOptions();
opt.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
browser = new InternetExplorerDriver(opt);
Now application will navigate to google search website using instance of WebDriver.
string link = @"http://www.google.com";
Once page is loaded application makes attempt to search for input text box by name "q" from DOM of web page. The Selenium provides multiple options to search DOM for required objects.
IWebElement query = browser.FindElement(By.Name("q"));
Then it hits the submit button of respective search form. The WebDriver is intelligent enough to search for respective form submit button automatically.
Points of Interest
- Selenium is a free tool and is documented well enough but you will still find automating certain things is easy and some are difficult. If you're stuck with a difficult problem then you may need to search a lot and post questions and wait patiently for an answer.
- Based on which language API your using you will note that certain feature provided in one language API will not be available in others.
- Selenium-WebDriver is closely tied with browser features so you may need to consider these differences carefully while writing your test cases if they are used to test web pages on different browsers.