Friday, January 27, 2012

Using xPath in Selenium

There is no one way to write an xPath statement, but there is good and bad ways.

Things to think about
• Avoid including text in xPath statement as then test won't work in a different language
• Beware of other objects in the page which may use the same attributes
• Always narrow down to a particular code block which can be uniquely identified
Example:



Option1:
//div[@id='list']/table/tbody/tr/td/div[2]/a
Option2:
//div[@id='list']/table/tbody/tr/td/div/a[contains(text(),'Welcome users who')]
Option3:
//div[@id='list']//a[contains(text(),'Welcome users who')]
Option4:
//div[@id='list']//a[contains(@href,'notify.do?user=welcome')]

Option 4 is the best way to identify the link!

Selenium Tutorial to set up a Selenium Grid

This tutorial walks you through the process of setting up Selenium Grid. I am going to first discuss the idea of Selenium Grid.

Selenium Grid is a Selenium derivative that allows the developer/tester to access Selenium Remote Controls without worrying about where the Selenium Remote Controls are. You as a developer/test create your script as you would for Selenium Remote Control and run it as normal. This is good if you want to test a bug with a specific browser but can't have it installed locally like multiple versions of Internet Explorer.

NOTE: Selenium Grid can only be controlled by Selenium Tests in a programming language. You will NOT be able to run Selenium HTML Test suites against it.

1. You will need to download and install the latest Java JRE on your system. You can find this at http://java.sun.com/ .
2. Once you have installed it, open up a command prompt and run java -version. It should look something like the image below.


3. You need to check that you have Apache Ant. If you open up a terminal/command prompt and type ant -version. If it doesnt return an error then you should be fine. If it returns an error you will need to download Apache Ant and expand the archive to a folder on harddrive.
4. You will need to now download Selenium Grid and put that in a folder that is easy to access. Now that we have all the relevant parts installed lets start the grid up.
5. We are going to start up the Selenium Grid Hub first. Open up a command prompt or terminal and navigate to the folder where the Selenium Grid files are stored.
6. From the command prompt run ant launch-hub. This will get the hub running. To see it running put http://localhost:4444/console into a browser and you will be shown a webpage with 3 columns. The first column shows what Environments it can handle, the second column shows what remote controls are available and the third column shows what remote controls are currently running tests. Like the following screenshot



7. Now that we have our hub running, lets start up a remote control. Open up another command prompt or terminal and navigate to the Selenium Grid folder. Run ant launch-remote-control. This will launch a Selenium Remote Control for handling Firefox on your OS on port 5555
8. We now have a Selenium Grid up and running! Lets run some tests against it.
9. To setup an node for Internet Explorer you need to run it as ant -Dport=5556 -Denvironment=*iexplore launch-remote-control. If you Selenium Grid node is not on the same machine as the hub just add -DhubURL= -Dhost=
10. I hope that you find this really useful. I will publish a tutorial on running tests concurrently.
One last thing, if you are running your tests on your Grid and the machine dies and you want to remove it from your grid just run http://:4444/registration-manager/unregister?host=&port=&environment= in a browser and it will unregister it from the hub. This last bit of information will make life a lot easier for you. e.g http://localhost:4444/registration-manager/unregister?host=locahost&port=5556&environment=*iexplore