Selenium is a free, open source, automated testing suite for web applications across different browsers and platforms.
The Selenium suite consists of a few core components. Several add-ons are also available. The primary components of Selenium are:
- Selenium IDE - for recording browser actions driving the web application through a specific scenario
- Selenium Grid - a tool used to run parallel tests across multiple machines and browsers.
Recently, Selenium 2 was released which combines Selenium RC and Web Driver.
Like most record and playback testing tools, SeleniumIDE allows the user to quickly record a browser session and play it back. While many of the Smoke Tests run in CI were initially created using SeleniumIDE they have been heavily modified to be less brittle as well as to be made up of reusable components. SeleniumIDE is useful for recording a test initially. It can then be saved as a unit test and then refined to be more stable and reliable. For writing Smoke Tests from scratch see Deprecated Creating Rice Smoke Tests.
Guidelines to Design/Record a Selenese IDE Test
Documentation guide provided by SeleniumHQ is the best for a beginner/professional to get going with Selenium IDE/Selenium RC/Selenium WebDriver backed Selenese Test.
Selensese is commonly used as a reference for a set of Selenium commands used to run a test.
It’s pretty simple to create a Selenese Test:
- Install a Selenium IDE to your Firefox browser (Download here).
- Record the script as desired.
- Export the recorded script to desired format and save it.
- Run an exported JUnit test
Designing (Recording) a Selenium Test is as simple as it looks, but it also involves a bit of tweaking in order to get the best out of it.
Since we already have integrated Selenium Automated testing to the Rice Continuous Integration environment, let's concentrate on how to design/create selenium test and add it to an existing test package.
To have the basic Idea about the usage of SeleniumIDE one must go through the Documentation Guide provided on SeleniumHQ. Try a few tutorials and become well aware about different Selenese commands (and their versions if any) offered by Selenium.
Running a selenium unit test
After you have created a test and successfully played it back using the Selenium IDE, then next step is to save the recorded test as a unit test.
- From the selenium Firefox add on menu (File > Export Test Case As ...), export the record script as a JUnit 3 or 4 remote control test
- Attempt to run the test without starting the selenium server, if it fails start the selenium server on the console using
- OR Download the selenium server
- Start the selenium server on the console using
- Add the following system property to the default JUnit test configuration. This can also be done per test in an IDE specific way. The URL points to the rice instance being tested.
- Add the following system property to prevent Firefox from closing after the test is run.
- Start the rice instance if not running yet.
- Initiate the test
Referring to above mentioned documentations would almost get you well going through recording a simple test case. Below are some simple steps to record your first Test.
- Bring IDE to Recording mode by activating the Red button. Once its active IDE starts recording your events on the browser for the window that holds content for Base URL, mentioned at the top bar of IDE.
- Get accustomed to all the available commands and know each specifically referring to the documentation available. Command List box on IDE displays list of all the commands supported by IDE. These commands either reflect different browser events or different desirable actions like verifying, asserting element etc. For example:
- Generated from click(locator)
- Arguments : locator - an element locator
- Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.
- Target field is the field which takes input to help locate different elements which are under test. IDE supports different locating techniques. CSS, DOM, Xpath are some of the supported methods one can employ to locate element. For example a link with value Documentation can be expressed in target field as:
- Value field of IDE takes input for the Test Data that is to be used.
- All the selenium Tests are recorded in HTML format by default. You can have HTML view of the Test in the Source Tab.
- Another feature IDE comes with is its flexibility to allow the user to export the recorded Test to different formats compatible to various existing popular programming languages and Test framework like JUnit and TestNG. You can directly export or save the Test to desired format. If your project employs JUnit Test framework this will well be a handy feature as the user would want to record Test with IDE, export or save the same to JUnit Test and integrate within your test package as JUnit test.
- Lastly your IDE knows to play the same Test. It allows you to load the previously recorded test and play the same. This may help you to quickly verify your Test behavior.
- Overriding default configuration would be required in some situations. For example user may want to set desired timeout value for command to timeout throughout Test. One can set default values for default package attribute from the Menu-> Option for JUnit Test format. This and many other such options can be configured as desired.
Things to taken care of while designing/Recording the script.
- Test should be such that it should free from the static identifiers for the html elements as much as possible.
- There are some components which are automatically generated and have auto generated Ids. Such elements should be located with the help of different feasible locating techniques like xPath, css, html as mentioned in bullet 3 of IDE overview section.
- If you are not sure about the later modifications to the page under Test you should avoid using static Identifiers like mentioning ids, name, CSS class Name. This could well prevent breaking of your test sequence.
for ex. If the input field is
- Sometimes it's good to tweak the Test rather than completely accepting the one exported or saved from IDE. Developers more competent with JUnit coding could put in their desired piece of code wherever feasible along with the exported code. You might want to tweak the test for some conditional situations like executions of particular piece of Test Code or Assertions/verification of elements etc.
- More and more complex Test you work on, higher the Expertise curve goes.
Configuring Selenium with Maven Applications
This section more or less deal with how to automate and bind your Test's execution with the integration phase of life cycle.
Selenium Maven plug-in along with Maven Surefire Plug-in and web-container plug-ins like tomcat/jetty enables you to run your Test Plans in continuous integrated environment.
Below are some of the plug-in configurations used which may be added to pom.xml to enable binding of Selenium Tests to integration phase.
- Selenium Maven Plugin allows binding of Selenium Server’s start and stop goal with integration phase. You may want to add some configurations too along with this.
- Tomcat Maven Plugin can be configured to bind the start and stop of container with pre-integration as well as post-integration-test phase. Similar things can be achieved using Jetty Maven Plugin with respective configuration changes.
- Maven Surefire Plugin to make sure the execution of Unit Tests during the integration-test phase of lifecycle
- Along with this plugin we need to add selenium-java/JUnit dependency Setting up all this with your application and running maven goals would ensure execution of Tests during Integration-test phase of application life-cycle.