Tuesday 8 January 2013

Selenium IDE

Selenium is a test tool for web applications. Selenium tests run directly in a browser, just as real users do. Selenium is a JavaScript framework that runs in your web browser. Selenium IDE is a FireFox extension made by the OpenQA.org team, that records mouse and keyboard actions as Selenium commands. It is very easy to use and powerful tool for controlling, automating or testing web sites. You can record how an application is being used and then play back those recordings followed by asserts. After recording, you can export test case in many languages like java, Ruby, Perl, python etc.

So, let’s get started with it.

Installing Selenium:  IDE is only available for Firefox as a add-on. In Firefox browser, go to Tools, add-ons, click on get Add-ons, search for Selenium IDE, Click on Install IDE .After installing, restart FireFox.

Selenium IDE explained
After installing Selenium IDE, go to Tools->Selenium IDE, you’ll get a screen which contains

Base URL:The root of web application you want to test. It can be any website URL under test. For ex, If you are test Google’s homepage, then Base URL will be automatically filled by www.google.co.in.

Fast and Slow: We can specify the speed of the test case using Fast and Slow. Default is fast. Sometimes test fails due to the fast execution. Selenium IDE gives the facility to adjust the speed.

Run Test Suite: If there are many test cases, we can put all together in a suite. This button helps us to run the entire suite.

Run a single Test: After recording a test case, we can run the test case by clicking on this button.

Pause: We can pause a test case using this button.

Step: We can step through the test case.

Apply RoleUp Roles:
Record By default when the IDE comes up it has recording turned on.  You might want to turn it off by clicking on the red button, until you are ready to begin recording.

Command ,Target, Value: Specify commands, including asserts.

Log, Reference, UI-Element, RollUp: Reference of the currently selected command.

Recording a Simple Test:
  1.  Start Selenium IDE  in Firefox: Tools->Selenium IDE.
  1. Browse to Google.com and enter ’selenium’ in the search box and click enter.
  1. Click on the first result, which is that of selenium.openqa.org.
  1. Stop the recording by clicking on the record button.
  1. If you click on the ‘Source’ tab you can see the test html generated by selenium.
  1. The ‘table’ tab shows the commands recorded by Selenium.  Open a new tab in Firefox and click on the Selenium IDE’s play button to run the recorded test
                         
There are several commands that can be used  .To use the available commands ,follow the step below.
a. Click on the blank line below after the last ‘clickAndWait’ command and insert ‘assertTextPresent’ command from the drop down box as shown below. You should see something like this.

This test checks to see if the text ‘Selenium News’ is present in the last rendered page. Run the test again and you should see that the test has run successfully. No try replacing the text with something that is not present on the page, take for example ‘mpower’ and run the test again. Now the test fails and you should get the screen below.

We can record and play on any website.
    
Second  example: Now we are  going to use few more commands in our test case. Follow the Step by Step procedure.
  1. Open Firefox browser
  1. In the navigation bar, type in http://www.mpowerglobal.com/
  1. Go to Tools->Selenium IDE. By default it starts recording.
  1. Now go back to firefox, on the http://www.mpowerglobal.com/ home page, On right clicking anywhere on the page ,you can find show all commands with an arrow.On following the arrow,you can find assertTitle  IT Business Consulting | Liferay Portal | … command. select that command.
  1. Now highlight “Who we Are” and right click , select  verifyTextPresent.   
  1. Now go to Selenium IDE , you can find all the actions you performed in command table tab.      
  1. Now go to File->Save TestCase.Browse to the folder you want to save and then save test case  in .html format.
We have successfully created another test case.Now if we want to run these two test cases one after the other, then we can put them in a suite.

Writing a Test Suite: A Test Suite in Selenium is just an HTML file that contains a table of links to tests.

<html>
<table border="1" style="font-size:25">
    <tr><td><a href="./F:/Tests in html/testcase1">Google test</a></td></tr>
                        <tr><td><a href="./F:/Tests in html/testcase2">mPower test</a></td></tr>
</table>
</html>

In order to create a test suite, all you have to do is create an HTML document that is composed of a table of links to your individual unit tests. Obviously you’ll need to adjust the href’s to wherever you have your test cases stored, but essentially that is all there is to creating a test suite.

Running the Test Suite:   After creating the suite, go to Selenium IDE ->File-> Open test suite->navigate to your main suite position.

After opening suite , click on run test suite and see your tests working. To view the test case running, minimize the FireFox window and keep selenium IDE next to it.
  
After recording, we can export test case in many languages .Java developers can export test case in Java. Just have to go File->Export test case as->Java (JUnit)
                           
After exporting, we need Selenium RC to run our exported test case.

Extensions to Selenium IDE: You can extend functionalities of Selenium IDE with JavaScript. There are 2 types of scripts in Selenium IDE - formats and extensions.
User-extensions are very easy to use with the selenium IDE.
  1. Create your user extension and save it as user-extensions.js.
  1. Open Firefox and open Selenium-IDE.Click on Tools, Options
  1. Write the user extensions.js file.
  1. Your user-extension will not yet be loaded, you must close and restart Selenium-IDE.
  1. Now you can use the values present in user-extensions.js file.
XPath: One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.
There are also a couple of very useful Firefox Add-ons that can assist in discovering the XPath of an element:
  • XPath Checker - suggests XPath and can be used to test XPath results.
  • Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.