2.50/Automated Testing/Selenium
Contents |
Selenium
Selenium is a portable software testing framework for web applications.
It has a suite of tools that are useful for testing automation. Single test scripts can be recorded and played back using Selenium IDE. More complex tests can be written on various programming languages and executed using Selenium Remote Control. In addition, tests executions can be distributed using Selenium Grid.
Currently, we use four of Selenium programs: The code we write communicates with the Remove Control Server, that might be in another computer. And the Remote Server makes Selenium Core instances to execute the specific actions on a web browser. We use Selenium Grid to parallelize the execution through a hub that communicates multiple Remote Controllers with a Selenium Server. Finally the IDE is used to get a clue of how user actions can be expressed in terms that selenium can interpret.
Selenium IDE
Selenium IDE is a Firefox extension that allows to record, edit, debug and play back tests [1]. It can be downloaded at their website. Once installed, open it from the menu Tools->Selenium IDE of your Firefox browser. The red button is used to record the actions performed on the browser. While you press buttons, select options and enter text, those actions are recorded and are stored as a table. You can run the pre-recorded actions from the same IDE with the green play button. |
Scripts are written as a three column HTML table; and they can be extended using Javascript files. For example, the following source code performs a login on Openbravo ERP.
<tr> <td>open</td> <td>/openbravo/security/Login_FS.html</td> <td></td> </tr> <tr> <td>type</td> <td>user</td> <td>Openbravo</td> </tr> <tr> <td>type</td> <td>password</td> <td>openbravo</td> </tr> <tr> <td>clickAndWait</td> <td>buttonOK</td> <td></td> </tr>
In addition, Selenium IDE can be used to translate from HTML to one of the languages supported by the Remote Control.
However this is not the same code that we will use in our testing repository because:
- Recorded code is hard to maintain and is failure prone.
- There is no chance to reuse code because all tests are done from scratch, and the IDE doesn't factorize common actions so there would be a lot of duplicated code.
- All the parameters are hardcoded.
Even though, this IDE is useful because it gives us a clear understanding of what we will have to write.
Selenium Remote Control
Selenium Remote Control (RC) allows to write tests in any programming language that can make HTTP requests; using any mainstream JavaScript-enabled browser.
It can be downloaded from their website . This file includes the server and client libraries for Java, C#, Perl, PHP, Python and Ruby.
With Openbravo ERP we use the Java client driver to automate our integration tests, because it's the language of the ERP and most developers are confortable with it. Previous HTML example of the login process will look like this using Java:
selenium.open("openbravo/security/Login_FS.html"); selenium.type("user", "Openbravo"); selenium.type("password", "openbravo"); selenium.click("buttonOK"); selenium.waitForPageToLoad("30000");
In order to execute the test, we need to start selenium server first:
java -jar /path/to/selenium-server.jar -multiWindow
Note: -multiWindow option is required to run the test in a separate browser window. If this parameter is not used, selenium runs in a subframe; that doesn't work with Openbravo ERP |
Then, we use Junit4 to run the tests. For example, to execute this simple login scenario we write on the command line something like this:
java -classpath .:/path/to/junit-4.5.jar:/path/to/selenium-java-client-driver.jar org.junit.runner.JUnitCore Login
Note: Even though Selenium tests can be run without a testing framework, we have found some advantages while using Junit |
Selenium Core
Selenium Core is the javascript execution framework that will finally interact with the browser. It is included on Selenium Remote Control.
Selenium Grid
Selenium Grid allows the parallel execution of Selenium tests.
First the Selenium hub has to be started. And then one or more Selenium remote controllers can be registered as executors with that hub.
Of course, something will have to run tests in parallel. We use the Ant parallel tasks for this.
And when the Hub receives the requests it will send them to a idle Remote Controller (if any) or queue them.