Advertisment

Selenium to automate testing process

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: For any software development project, testing is an integral and important process. To assure that all components of an application are working as per plan and producing desired results, comprehensive testing is done for an application.

Advertisment

To aid these efforts, there are tools available that help in automating the process of testing and eliminating repetitive work for a tester. Selenium is a portable software testing framework specifically designed for web applications.

For software testers who need to record and play their tests, Selenium provides the facility to record a web activity process of the application and then generate a test case based on that. He can then run those test cases continuously without having to manually conduct the constituent steps again and again thereby eliminating repetitive work. Even for software developers, Selenium API is available which helps them to author test cases in Java, Ruby, Python, PHP and other languages.

Selenium was developed by a team of programmers and testers at ThoughtWorks, and is available for free under Apache 2.0 license. The Selenium tests run in most popular browsers, such as Internet Explorer, Firefox, Opera and Chrome, and can be deployed on Windows, Linux and Macintosh platforms.

Advertisment

Getting Started

For recording interaction with the web application, Selenium IDE is used, which is a Firefox add-on. With this month's DVD we have distributed this add-on along with Selenium core testing tools which can be used to execute test suites generated by Selenium IDE on any browser.

To install Selenium IDE onto your Firefox browser, right-click on the file, selenium-ide-1.0.2.xpi, located in the Selenium folder in the DVD and then select 'Open with' option to open the file using Firefox. After installation of the add-on, Firefox prompts for a restart. Once you have restarted the browser, you are all set to use Selenium IDE. However, an prerequisite is that JDK 1.5 should be installed on the system.

Selenium IDE is an integrated development environment for generating Selenium tests that can either be written as HTML tables or coded in various languages like Java, PHP or Python. Currently this IDE is supported only by the Firefox browser.

To demonstrate how Selenium can be used to record and then create tests we will use a typical scenario. Web applications contain forms, and it becomes a tedious task to fill a form each time while testing. Using a previously created web application, PcqContacts, we will generate some test cases using Selenium IDE.

Advertisment
 



Generating a test case

Start Selenium IDE in Firefox by selecting Tools > Selenium IDE, a browser pop-up opens with the Selenium IDE. Selenium IDE records interactions with the Web application, with one command per line. By default, the IDE starts with recording switched on, so when you have completed your web activity you just have to click on the red button on the IDE menu to stop the recording.

Advertisment

publive-image

To start Selenium IDE goto Tools > Selenium IDE in Firefox browser. Give the base URL of the web application for which you want to create the test case.

To start recording, enter th e web application's path in the browser's address bar, ie http://localhost:8080/ PcqContacts and then start the web activity. Fill in the Contacts form and submit the form. Next, check the Contacts page to see whether that entry has been enlisted. Note, whatever you're doing within the browser is being recorded by the IDE. During recording, Selenium IDE will automatically insert commands into your test case based on your actions. Typically, these include:

publive-image 

Advertisment

Selenium Core can be used to run test suites on other browsers. Through the control Panel frame you can execute the test suites passed on the Test Suite frame.

Clicking a link - 'click' or 'clickAndWait' commands

Entering values - 'type' command

Advertisment

Selecting options from a drop-down listbox - 'select' command

publive-image

To check whether the entered value exists, use assertTextPresent command. Its presence is revealed in the log window.

Clicking checkboxes or radio buttons - 'click' command

Advertisment

When you're done with our web activity, simply end the recording by clicking on the IDE's red button on the toolbar. You'll notice in the Table tab that for each activity, there has been a command inserted with a target and value. For instance, when we entered the name as Tyra, the command issued was 'type' with target being 'Name' and value being 'Tyra'.

In the Source tab, you can see the HTML code for the test case generated. Through File > Save Test Case option, save the test case as PcqTestCase.html. Now you can use this test case again to run the case scenario of entering the test values to check that the web form acts normally. To run the test, select the test case from the Test Case frame and then click on Run button from the toolbar of the IDE. This will execute the test within the browser and when any error occurs, it gets reported in the log window.

Adding verification

We have already created a test case named PcqTestCase.html. Now if you want to check properties of the web page, use the 'assert' and 'verify' commands. Suppose you want to check that the entered name, Tyra, is present in the Contact list page or not. For that, open the test case in the Selenium IDE and click at the blank line in the Table tab. From the drop-down list, select 'assertTextPresent' command and give its value as Tyra. At test re-run, if the Tyra word is present the assertTextPresent command will return value as true, else it will give an error exception.

Creating test suites



As Selenium is available as a core feature with which tests can be executed in other browsers, to deploy Selenium Core, extract the contents of selenium-core-1.0.1.zip to a base folder. Now copy the 'core' folder from the extracted contents into the web accessible directory of your web server. For instance, if you are using Apache web server, then copy 'core' folder into the 'htdocs' folder of the server so that the Selenium Core is available on the same host and same port as your web application.

To create a test suite from Selenium IDE, select the test cases from the Test Case frame and goto File > Save Test Suite As option and enter the name as PcqTestSuite.html. This will create a table formatted html page having references of all test cases that the test suite contains.

To run a test suite from a browser other than Firefox, open the path to the TestRunner.html file, which in Apache's case would be http://localhost/core/ TestRunner.html. For a sample website hosted on the same web server, run the test suite PcqTestSuite.html for that site. In the Test Suite frame, pass the path for the PcqTestSuite.html and then from the Control Panel frame run the test suite. It executes the test cases from the test suite and displays the error log in a separate browser window.

tech-news