Getting Started with Selenium Grid

Exclusive offer: get 50% off this eBook here
Selenium 1.0 Testing Tools: Beginner’s Guide

Selenium 1.0 Testing Tools: Beginner’s Guide — Save 50%

Test your web applications with multiple browsers using the Selenium Framework to ensure the quality of web applications

$23.99    $12.00
by David Burns | November 2010 | Beginner's Guides RAW Open Source

In this article, by David Burns, author of Selenium 1.0 Testing Tools, we are going to have a look at what Selenium Grid is and how we can set it up in different environments, including how to set it up on Amazon EC2. This will abstract the topography of where the tests are located so that your tests only have to worry about one address.

In this article we shall cover the following topics:

  • Setting up the Selenium Grid Hub
  • Setting up the Selenium Grid Remote Controls
  • Creating tests for the Grid

So let's get on with it...

 

Selenium 1.0 Testing Tools: Beginner’s Guide

Selenium 1.0 Testing Tools: Beginner’s Guide

Test your web applications with multiple browsers using the Selenium Framework to ensure the quality of web applications

  • Save your valuable time by using Selenium to record, tweak and replay your test scripts
  • Get rid of any bugs deteriorating the quality of your web applications
  • Take your web applications one step closer to perfection using Selenium tests
  • Packed with detailed working examples that illustrate the techniques and tools for debugging
        Read more about this book      

(For more resources on this subject, see here.)

Important preliminary points

For this section you will need to have Apache Ant on the machine that you are going to have running Grid instances. You can get this from http://ant.apache.org/bindownload.cgi for Windows and Mac. If you have Ubuntu you can simply do sudo apt-get install ant1.8, which will install all the relevant items that are needed onto your Linux machine. You will also have to download the latest Selenium Grid from http://selenium-grid.seleniumhq.org/download.html.

Understanding Selenium Grid

Selenium Grid is a version of Selenium that allows teams to set up a number of Selenium instances and then have one central point to send your Selenium commands to. This differs from what we saw in Selenium Remote Control (RC) where we always had to explicitly say where the Selenium RC is as well as know what browsers that Remote Control can handle.

With Selenium Grid, we just ask for a specific browser, and then the hub that is part of Selenium Grid will route all the Selenium commands through to the Remote Control you want.

Selenium Grid also allows us to, with the help of the configuration file, assign friendly names to the Selenium RC instances so that when the tests want to run against Firefox on Linux, the hub will find a free instance and then route all the Selenium Commands from your test through to the instance that is registered with that environment. We can see an example of this in the next diagram.

We will see how to create tests for this later in the chapter, but for now let's have a look at making sure we have all the necessary items ready for the grid.

Checking that we have the necessary items for Selenium Grid

Now that you have downloaded Selenium Grid and Ant, it is always good to run a sanity check on Selenium Grid to make sure that we are ready to go. To do this we run a simple command in a console or Command Prompt.

Let's see this in action.

Time for action – doing a sanity check on Selenium Grid

  1. Open a Command Prompt or console window.
  2. Run the command ant sanity-check. When it is complete you should see something similar to the next screenshot:

What just happened?

We have just checked whether we have all the necessary items to run Selenium Grid. If there was something that Selenium relied on, the sanity check script would output what was needed so that you could easily correct this. Now that everything is ready, let us start setting up the Grid.

Selenium Grid Hub

Selenium Grid works by having a central point that tests can connect to, and commands are then pushed to the Selenium Remote Control instances connected to that hub. The hub has a web interface that tells you about the Selenium Remote Control instances that are connected to the Hub, and whether they are currently in use.

Time for action – launching the hub

Now that we are ready to start working with Selenium Grid we need to set up the Grid. This is a simple command that we run in the console or Command Prompt.

  1. Open a Command Prompt or console window.
  2. Run the command ant launch-hub. When that happens you should see something similar to the following screenshot:

We can see that this is running in the command prompt or console. We can also see the hub running from within a browser.

If we put http://nameofmachine:4444/console where nameofmachine is the name of the machine with the hub. If it is on your machine then you can place http://localhost:4444/console. We can see that in the next screenshot:

What just happened?

We have successfully started Selenium Grid Hub. This is the central point of our tests and Selenium Grid instances. We saw that when we start Selenium Grid it showed us what items were available according to the configuration file that is with the normal install.

We then had a look at how we can see what the Grid is doing by having a look at the hub in a browser. We did this by putting the URL http://nameofmachine:4444/console where nameofmachine is the name of the machine that we would like to access with the hub. It shows what configured environments the hub can handle, what grid instances are available and which instances are currently active.

Now that we have the hub ready we can have a look at starting up instances.

Adding instances to the hub

Now that we have successfully started the Selenium Grid Hub, we will need to have a look at how we can start adding Selenium Remote Controls to the hub so that it starts forming the grid of computers that we are expecting. As with everything in Selenium Grid, we need Ant to start the instances that connect. In the next few Time for action sections we will see the different arguments needed to start instances to join the grid.

Time for action – adding a remote control with the defaults

In this section we are going to launch Selenium Remote Control and get it to register with the hub. We are going to assume that the browser you would like it to register for is Firefox, and the hub is on the same machine as the Remote Control. We will pass in only one required argument, which is the port that we wish it to run on. However, when starting instances, we will always need to pass in the port since Selenium cannot work out if there are any free ports on the host machine.

  1. Open a Command Prompt or console window.
  2. Enter the command ant –Dport=5555 launch-remote-control and press Return. You should see the following in your Command Prompt or console:

  3. And this in the Selenium Grid Hub site:

What just happened?

We have added the first machine to our own Selenium Grid. It has used all the defaults that are in the Ant build script and it has created a Selenium Remote Control that will take any Firefox requests, located on the same machine as the host of Selenium Remote Control Grid. This is a useful way to set up the grid if you just want a large number of Firefox-controlling Selenium Remote Controls.

Selenium 1.0 Testing Tools: Beginner’s Guide Test your web applications with multiple browsers using the Selenium Framework to ensure the quality of web applications
Published: November 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources on this subject, see here.)

Adding Selenium Remote Controls for different machines

Selenium Grid is most powerful when you can add it to multiple operating systems. This allows us to check, for instance, whether Firefox on Windows and Firefox on Linux is doing the same thing during a test. To register new remote controls to the grid from a machine other than the one hosting the hub, we need to tell it where the hub is. We do this by passing in the –DhubURL argument when calling the Ant script. We also need to pass in the –Dhost argument with the name of the machine so that we can see where it is being hosted.

Let's see this in action.

Time for action – adding Selenium Remote Controls for different machines

For this Time for action you will need to have another machine available for you to use. This could be the Ubuntu machine that you needed for the previous chapter. I suggest giving the –Dhost argument the name of the machine that it is running on. If you have a small Grid then you can name them according to the operating system that it is run on.

  1. Open a Command Prompt or console.
  2. Run the command ant –Dport=9999 –DhubURL=http://nameofmachine:port –Dhost=nameofcurrentmachine launch-remote-control.
  3. When you have run this, your Grid site should appear as follows:

What just happened?

We have added a new remote control to the grid from a machine other than where the Selenium Grid Hub is running. This is the first time that we have been able to set up our remote control instances in a grid. We learnt about the –DhubURL argument and the –Dhost argument that is needed when launching the remote control. We then saw that it has updated the Grid site that is running on the hub.

Now that we have this working as we expect, let us have a look at setting up browsers other than Firefox.

Adding Selenium Remote Control for different browsers

Selenium Grid is extremely powerful when we start using different browsers on the grid, since we can't run all the different browsers on a single machine due to operating systems and browser combinations. There are currently up to nine different combinations that are used by most people, so getting Selenium Grid to help with this can give you the test coverage that you need. To do this we pass in the –Denvironment argument in our Ant call. The value that we assign to this has to be Selenium Grid configuration. The Selenium Grid configuration comes with a number of preset items. This is visible from the Selenium Grid Hub page that we have seen already. Let us now see how we can set the items.

Time for action – setting the Environment when starting Selenium Remote Control

Now that we need to get Internet Explorer Selenium Remote controls added to our grid. We have to add the –Denvironment argument to our call with the target on the configured environments. Since we want an Internet Explorer remote control we can use the IE on Windows targets.

  1. Open a console or Command Prompt window.
  2. Run the command ant –Dport=9998 –DhubURL=addressofhub –Dhost=nameofremotehost –Denvironment="IE on Windows" launch-remote-control.
  3. When it is running, your hub page should appear as follows:

What just happened?

We have just seen how we can create more verbose environment names such as "IE on Windows". We also saw how we can start a remote control for different browsers. This is quite useful when we need to test a large amount of browser and operating system combinations.

Updating the Selenium Grid Configuration

The Configured Environments come with a standard installation but there are times where it would be useful to set up your own targets. This could be when browsers that used to work on a specific operating system now work on multiple operating systems. We have already seen this happen with Google Chrome.

To update the configuration we need to have a look at the grid_configuration.yml. This is a YAML file that contains all the configurations that Selenium grid has. We need to add a new name and browser for new items. For example:

-name: "Google Chrome on Windows"
Browser: "*googlechrome"

Time for action – adding new items to the Grid Configuration

As we saw from the default configuration on that is distributed, it doesn't have the different flavors of Google Chrome. We can add this to the file this time so we can extend the coverage that we need.

  1. Open grid_configuration.yml in a text editor.
    Add the following to the file:

    -name: "Google Chrome on Windows"
    Browser: "*googlechrome"

  2. Start the hub.
  3. In another console or Command Prompt, run the newly created item. While running, your Grid should appear as follows:
Selenium 1.0 Testing Tools: Beginner’s Guide Test your web applications with multiple browsers using the Selenium Framework to ensure the quality of web applications
Published: November 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources on this subject, see here.)

What just happened?

We have just added our first item to the Grid Configuration. This could be renaming an item so that it makes a lot more sense, or if there is something missing that is needed. We can give each of the items meaningful names that then point to a specific browser. We can also use this to set up custom browsers if need be.

Pop quiz – doing the thing

  • What is the command required to start the Hub?
  • What is the URL where one can see what is happening on the Grid?
  • How do you specify the port that the Remote Control is running on?
  • How do you specify which browser you would like the Remote Control to be registered with?

Running tests against the Grid

Now that we have set up the Grid with different instances, we should have a look at how we can write tests against these Remote Controls on the Grid. We can pass in the value of the target that we can see in the grid and then run the tests. So instead of passing in *firefox you can use "firefox on linux" and then run the tests as usual.

Let's see this in action.

Time for action – writing tests against the grid

  1. Create a new test file.
  2. Populate it with a test script that accesses an item on the grid and then works against http://book.theautomatedtester.co.uk/. Your script should look similar to the following:

    import org.junit.*;
    import com.thoughtworks.selenium.*;

    public class TestExamples2 {

    Selenium selenium;

    @Before
    public void setUp(){
    selenium = new DefaultSelenium("192.168.157.153",4444,
    "Google Chrome on Linux",
    "http://book.theautomatedtester.co.uk");//
    selenium.start();
    }

    @After
    public void tearDown(){
    selenium.stop();
    }

    @Test
    public void ShouldRunTestsAgainstGoogleChromeOnLinux(){
    selenium.open("/");
    selenium.click("link=chapter2");
    }
    }

What just happened?

We have just seen how we can write tests that can run against the Grid and then run them. When the tests are running the grid will show which Remote Control is currently in use and which grid items are currently free. We can see this in the following screenshot:

Summary

We learned a lot in this article about how to set up Selenium Grid and all the different arguments needed, as well as running our tests against the Grid.

Specifically, we covered:

  • Starting Selenium Grid Hub: In this section we had a look at how we can start up Selenium Grid Hub that is the central point for Selenium Grid.
  • Setting up Selenium Grid Remote Controls: We had a look at all the arguments that are needed to add a Remote Control to the Grid so that we can use it. This gives us a more manageable view of our grid so that we can work with it.

We also discussed how we can create tests that use the grid.


Further resources on this subject:


About the Author :


David Burns

David Burns is a Senior Developer in Test having worked with Selenium for quite a few years. He is a Selenium Core Committer and so he knows and understands what users and developers want from the framework.

Books From Packt


Python Testing: Beginner's Guide
Python Testing: Beginner's Guide

Software Testing with Visual Studio Team System 2008
Software Testing with Visual Studio Team System 2008

JavaScript Testing Beginner's Guide
JavaScript Testing Beginner's Guide

Django 1.1 Testing and Debugging
Django 1.1 Testing and Debugging

Software Testing using Visual Studio 2010
Software Testing using Visual Studio 2010

Moodle 1.9 Testing and Assessment
Moodle 1.9 Testing and Assessment

iReport 3.7
iReport 3.7

JasperReports 3.6 Development Cookbook
JasperReports 3.6 Development Cookbook


Your rating: None Average: 5 (1 vote)
Excellent Article by
This is an excellent article. Really helped me to introduce to selenium grid concepts. Thank a ton. Keep posting....

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
j
8
f
k
z
f
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software