- Selenium IDE
Integrates into Firefox as an add-on to allow you to generate tests that can then be exported out to other formats for automated testing setups. This post will cover using the Selenium IDE to generate tests to be used with JUnit.
- Selenium Remote Control
Runs the test generated by the IDE on various platforms and browsers allowing for robust automated testing of your web application. Selenium RC will be lightly covered in this post, but will be covered in more depth in a later post.
- Selenium Grid
Builds upon Selenium RC and allows you to distribute your tests across multiple servers for parallel execution. Selenium Grid will be covered in a later post.
The first thing you'll need to do is install the Selenium IDE add on into Firefox by going to http://seleniumhq.org/download/. Once the Selenium IDE has been installed, you'll have to restart Firefox and you'll be ready to begin creating tests.
Start Firefox and click on Tools > Selenium IDE to open the IDE so you can begin creating a new test. Click the record button inside of the Selenium IDE dialog, the red circle button, then you can begin performing the actions that will make up your test. This process is pretty much just browsing to the application that will be testing and being specifying what should be tested.
For our first example, we're going to use the Selenium IDE to build a basic test that asserts the title is correctly set for http://javaconfessions.com. We want to verify not only that the title is set correctly in the head of the document, but we want to make sure the title is showing properly at the top of the page.
Ensure that we have clicked the record button, the red circle button, then in the browser go to http://javaconfessions.com. Once the page loads, right click anywhere on the page and hover your mouse over the last item in the context menu which will be "Show all available commands."Once the flyout menu appears, click the "assertTitle Confessions of a Java Programmer" menu item. You will see this command populate into the Selenium IDE. This command will assert that the title inside the head of the main HTML page reads "Confessions of a Java Programmer."
Now that we've tested to ensure the title in the head tag is correct, we're going assert the title also shows up on the top of the page. This time, select the header text at the top of the page that reads "Confessions of a Java Programmer." Right click and once again hover over the "Show all available commands" menu item, then click the "assertText //div[@id='header-inner']/div[1]/h1 Confessions of a Java Programmer" menu item. This command will assert that the top of the page reads "Confessions of a Java Programmer."
Running the Test in the Selenium IDE
Now that we've gone through the process to setup a basic test, we're going to run it in the Selenium IDE in order to show that it works the way we think it should. To execute the test in the Selenium IDE, click the button that has the green triangle with three bars to execute the entire test.The browser will go through all of the actions defined in the test and it will pass. If there is a failure, you will see a red message in the output at the bottom and then you know you need to rework your test to ensure you are getting accurate results.
Exporting the Test for JUnit
Now that we have built the basic test in the Selenium IDE, we can export our test for use in a JUnit Test Case. In the Selenium IDE go to File > Export Test Case As > Java ( JUnit ) - Selenium RC. Name the file JavaConfessionsTestCase.java.
Open up the newly generated test file and change the package for the class if you desire. By default the test class will be created with a package of com.example.tests. After you are done editing the file close it and copy it into the appropriate package in your project's testing directory.
Running the Test
Now we need to download Selenium RC from http://seleniumhq.org/download/. Unzip the newly downloaded file and copy out the file named selenium-java-client-driver.jar out of the selenium-java-client-driver-1.0.1 directory to your testing environment's class path.
The next step is to copy out the selenium-server.jar out of the selenium-server-1.0.3 directory to a permanent spot on your testing environment server. This file can be put anywhere, as it will be ran as a service in order to allow Selenium testing from JUnit classes.
Before executing our JUnit test we need to start the Selenium RC server. Open a command prompt and change into the directory where you stored the selenium-server.jar file. Then start the Selenium RC server by typing java -jar selenium-server.jar.
With the Selenium RC server now running , you can execute your Selenium JUnit test just like you normally run JUnit tests. In the screenshot, you can see the results from executing the Selenium test in IntelliJ IDEA. In the next post, I will demonstrate how to create more complex Selenium tests.
0 comments:
Post a Comment