Esta informaciĆ³n en EspaƱol:
One of the Agile principles in software development is TDD -Test Driven Development-, which allows developers to better control the behaviour of their own code.Testing your code is no longer a small part of your projects, and creating automated tests for your developments helps to check the code quickly and efficiently, as well as to maintain the code after later upgrade, so you should always consider it when you plan your estimates.
A really helpful system you can use in Web development to create automated tests for your pages and applications is Selenium, a set of different software tools each with a different approach to supporting automation. This will allow you to carry out tests on your web apps as it helped me with automating the tests on my DOJO apps.
The latest releases of Selenium are the Selenium WebDriver ones, which involves two old projects (Selenium RC and WebDriver) merged into one in order to make things work better. All the references to Selenium in this post are about the Selenium WebDriver version or Selenium 2.
Selenium IDE Interface |
First Steps with Selenium
In order to get familiarised with Selenium, you should first try Selenium IDE, an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run.
Selenium IDE has a very user friendly interface which you can start using as soon as you install the add-on to your Firefox. As you navigate through your page or web app, Selenium IDE will record all the steps you take; you can then save your test cases and suites of tests, so you can reproduce the same processes as many times as you wish.
The tests you create with Selenium IDE can be re-used as part of the integration tests of your web app when you build and package your files. There are several options, depending on the language you are using; but this post will focus using Selenium WebDriver to run the IDE tests in two ways:
- HTML suites directly from Selenium IDE
- Java JUnit classes created as exports from Selenium IDE
Selenium WebDriver
Selenium WebDriver is another module, part of the Selenium family, which you can use to run your automated tests as part of the build process of your web app.
The application example for this post is a DOJO app using a Maven build. We will therefore be using the Selenium Maven dependencies for Selenium Driver and the different browsers we want to cover as part of our tests.
> Adding the Selenium WebDriver dependencies
The following snippet shows the Maven dependencies you need to add to your pom.xml file. The three browsers included in the dependencies will be tested, that is Firefox, Internet Explorer and Google Chrome.
The latest Selenium version at the time of writing this post is 2.26.0.
> Using the HTML test suites from Selenium IDE
You can get the HTML code for your test suites by doing File > Save Test Suite on your Selenium IDE. A suite is a set of one or more test cases; you will be prompted for the name of the test cases and the name of the suite; please make sure that the file representing the suite has an extension of HTML.
The following snippets show a very simple example of a suite with a test case that will open a login page and will fill in the form.
> Snippet of the suite:
> Snippet of the test case:
The files you generate from your Selenium IDE tests should look similar to the ones shown above.
Once you have created your suite files, you can run them as part of your build process. More dependencies are required in your pom.xml to include the Jetty server and the Selenium Maven plugin to call the suite test. The following snippets should be added to your pom file within the build section.
> Including the Jetty dependency so that it starts the app on the Jetty server before the integration test phase and stops it after the tests are completed:
> Including the Selenium Maven plugin dependency, with the different executions of the selenese goal, one execution for each of the browsers to be tested:
You need to make sure that your suite files are in the right path and also remember that your suite file needs to have an extension of HTML, otherwise, the browser will remain open, the test will not finish and the build process will never complete.
Now you are ready to build your app and run your test suite as part of the integration tests; just type your Maven command: mvn clean install
Your build process will start and you will see your tests running and opening the browsers to perform the test cases within your test suite.
NOTE: As part of the test carried out before writing this post, version 2.3 of the Maven Selenium plugin is compatible with Firefox 12, but not with any of the later versions. So if you want to run the selenese goal on Firefox, you need to be running version 12. IE and Chrome worked smoothly on their latest versions.
> Using Java JUnit classes exported from Selenium IDE
You can get the Java code for your test cases by doing File > Export Test Case As... > Java / JUnit 4 / WebDriver on your Selenium IDE. A Java class with JUnit annotations will be generated for each of the test cases you export from Selenium IDE. This code is more a guideline of what the test should do than the actual valid test to be run as part of your JUnit tests, but it is a very good template to use and enhance with your own test code.
The dependency you need to set up for these tests to run is the Maven Surefire plugin (apart from JUnit), as shown next:
According to the previous configuration, your Selenium WebDriver test classes need to be under the *.selenium package and they need to be called *Test.java in order to be run as part of the JUnit integration tests.
The next snippets show the same login page test as before, using JUnit classes to test the three browsers: Firefox, Internet Explorer and Google Chrome:
> The Firefox test in this case is compatible with the latest Firefox version
> For Internet Explorer, you need to download the IE driver server that applies to your Windows OS (32 or 64 bits) and tell your test class to use that driver
> For Google Chrome, you need to download the chromedriver suitable for your platform and tell your test class to use that driver
You can now build your app and run your JUnit tests as part of the integration tests; just type your Maven command: mvn clean install. The results of your tests can be logged by using the Maven Surefire Report plugin.
Many thanks for taking the time to read this post, I hope you found it useful for Automating your web Tests. Please leave any comments or questions you have.
This comment has been removed by the author.
ReplyDeleteOops, I accidentally deleted my question, let's try that again :D
DeleteI use chrome rather than firefox, is the Selenium IDE available for any other browsers?
Thanks for your question Christopher.
DeleteSelenium IDE is a Firefox plugin, therefore it cannot be installed on IE or Google Chrome. But that is where Selenium WebDriver has a good value, because it emulates the behaviour of Selenium IDE and allows you to test all the other browsers by means of the Maven dependencies and build tools.
Hi FernandeZ,
ReplyDeleteUnable to identify the credentials inside the iframe with the help of IDE . Can u plz suggest how to handle inside the iframe crontrols.