Webinar: What is TestOps? A guide to scaling test automation Register now.

Selenium Grid: A Simple Introduction and Complete Tutorial

After publishing many posts about software testing, we're here again to help you further increase your software testing knowledge. Today's…

By Testim,

After publishing many posts about software testing, we’re here again to help you further increase your software testing knowledge. Today’s topic is Selenium Grid. Selenium is a widely used tool for web application testing. Selenium Grid, in turn, is a component of the Selenium suite that specializes in grid testing, which is a form of testing that allows teams to quickly and easily test their web apps across a variety of environments.

This post is an introductory guide to Selenium Grid. You’ll learn some fundamentals about grid testing and how to get started with Selenium Grid. Let’s get to it.

Grid Testing Fundamentals

Grid testing isn’t a hard concept to grasp. In short, it means being able to execute software testing in a variety of different environments simultaneously. The reason why that matters is simple to understand: we have a lot of browsers and operating systems, and you need to ensure your application behaves as intended in all or at least the most used of those environments.

Starting With Selenium Grid

Now you’ll see a hands-on guide on how to get started with Selenium Grid. You’ll start by installing the required components. You’ll then learn how to install and configure a hub, how to install and configure a node, and how to perform your first Selenium Grid automation.

Getting the Requirements Ready

The Selenium components you’ll download in the next step are .jar files. That means you’ll need Java to be able to run them.

If you don’t already have Java installed, go to the Java download page to download and install it. After you’re done, fire up your terminal and run the following command:

If the installation is successful, you’ll see the version of your Java installation.

Additionally, make sure you add the Java path to your PATH environment variable, so you can use the Java command from wherever you are.

Creating Your Selenium Grid Hub

Your next step is to download the Selenium Server.  After the download is complete, move the file to a location where it’s easy to find. In my case, I’ll create a folder on the desktop and move the file there.

The next step is to start your hub. Run the following command:

Make sure to replace <version> with the actual version number of the downloaded file. In my case, the actual command I’ll be running is:

By default, Selenium Grid hubs run on port 4444. If the command ran successfully, you should see messages like the following ones:

To easily verify if the hub is running, open a browser and access http://localhost:4444/grid/console.

Also, do you see the third line the mentions the URL clients should connect to? Write down that URL; you’ll need it later.

Creating Your First Selenium Grid Node

Now it’s time to start a Selenium Grid node. In the “real world,” you’d install your nodes on different machines. For simplicity’s sake, I’ll use a single machine for both the hub and the node.

If you want to follow along using two machines, that’s fine. Just remember that the requirements are the same for the node machine: you need Java, and you need to download the same .jar package.

With all the necessary bits in place, you can open up a terminal, navigate to the location of your .jar file, and run the following command:

Again, replace <version> with the actual version of the .jar file you’ve downloaded. Also, replace <IP-ADDRESS> with the address listed in the output from the command you used to create the hub. If everything worked correctly, you should see a message saying:

If you go back to the terminal at your hub machine, you’ll see a message saying you’ve got a node registered. Also, if you open your browser while in the hub machine and head to the grid console URL, you’ll see an image like this:

The image clearly shows that the node has 11 browsers configured: five Firefox instances, five Chrome instances, and one Internet Explorer instance.

Executing Your First Test Using Selenium Grid

If you’ve been following the tutorial, you’ve successfully configured a hub and a node. You now have a successful Selenium Grid setup. The next logical step is to use that setup to execute some automation. So, let’s learn how to do it!

Downloading Chrome WebDriver

The first thing you have to do is download the adequate WebDriver according to your operating system and the browser that will be automated. For this tutorial, I’ll be driving Chrome on Windows. You can go to this page and download the correct version of the Chrome WebDriver according to the version of Chrome that you have installed.

Which machine should receive the WebDriver, the hub, or the node? That’s an easy question to answer: the hub since there’s where Selenium will actually drive the browser. You have to download the appropriate version of Chrome WebDriver and put it somewhere easy to find.

Assigning the WebDriver to the Node

The next step is executing the Selenium node and assigning it to Chrome WebDriver. To do that, you’ll have to run the same command you did previously, but this time with a new option, –dwebdriver:

You’ll replace <PATH-TO-WEBDRIVER> with the complete path to the location of the Chrome WebDriver file you just downloaded. The rest of the command is identical to how you executed it before.

Defining the Test on the Hub

Woohoo! You now have a node capable of driving a Google Chrome browser. That node is registered to the hub we’ve configured earlier, which means you’ve got all the necessary pieces in place to be able to define and run Selenium automation using your grid.

Creating and running a test is actually quite easy. Though you can use several different programming languages for that, we’re going with C#. Using the free Visual Studio 2019 Community Edition, create a new console application. With the application created, go to Tools, NuGet Package Manager, and then Package Manager Console. Within the console, install the necessary Selenium dependency by running this command:

After the installation is done, it’s just a matter of writing a few lines of code. First, let’s add the using statements so our code can communicate with Selenium:

Inside the main method, let’s create a new instance of the ChromeOptions class:

Now, create a new instance of the RemoteWebDriver class, passing both options and the URL for the hub as parameters:

Remember after you created your hub, I mentioned you should write a URL down because you’d need it later? Well, now’s the time: you’ll replace the URL above with the one you copied earlier.

Now, we’ll add three more simple lines:

The first line deletes all cookies, while the second one maximizes the window. Finally, the third line is where we list the actions we want the browser to perform automatically. In this case, we visit the URL for the Testim website. That’s all I’m doing for now, for simplicity’s sake. Keep in mind that, in a real test, you’ll write the interactions with the application. You can find elements by their names or IDs. You can click on links, buttons, and other elements. Additionally, you can define waits so your script doesn’t fail when you try to interact with an element that hasn’t been rendered yet.

After that, you can run your application by pressing F5, and you should see a new Chrome window opening and visiting the Testim website by itself.

And that’s it! You just ran your first automated test with Selenium Grid. Well done!

Selenium Grid Testing: Where Should You Go Now?

Selenium is one of the most popular tools used in web application testing, and not undeservedly so. Despite having some known shortcomings, Selenium is, in general, a solid tool. It comes in different versions that each address different types of testing needs. In this post, we’ve offered you an introduction to one of these flavors, Selenium Grid.

Selenium Grid is the Selenium suite component that enables grid testing, a form of software testing that is essential in our days of seemingly infinite combinations of operational systems, browsers, screen sizes, and other factors.

What should your next steps be? Now that you have a basic working Selenium setup, you can learn how to write your first Grid test. Later on, you can learn how to improve your existing setup, including using additional tools to spice it all up.

Thanks for reading!

What to read next

How to Fix Selenium’s “Element Is Not Clickable at Point”

What Is Grid Testing? Intro to Selenium Grid and Alternatives

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe