Wednesday, May 14, 2014

Testing your web application in parallel with multiple machines against different browsers using Selenium Grid

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.

Architecture:







Setting up Selenium Grid:



Selenium Server is actually a java program, because it was originally build for java in java. So we will need to have JRE (Java Runtime Environment) installed.

If you don't have that installed already, you can go to http://www.java.com/getjava, and you can download java.

Once you have that installed, we can go to http://seleniumhq.org/download





Here we are gonna grab the latest version of selenium server.

You can see it's a jar file. You can copy it to a location where you can use it and run it.
In this example, I will copy it to "C:\Selenium\Server".




Before you can run this, you do need to have java in your path environment variables.

You may already done this, but  if you haven't you can go into your Control Panel under System, then you want to go to Advanced System Settings.

Under here, you wanna go to Environment Variables... and set up your path here by editing the PATH Variable in System variables and adding to Variable value the path to you java bin folder. In my case "C:\Program Files\Java\jre8\bin".





This maybe be a different install location for you depending on what version of Java you've installed. But basically you just have to get to this bin directory.


Once you have done that, go ahead to the directory where you dropped selenium server JAR file using a command prompt window.




On this command prompt, type "java" to make sure you can access java.

Then type "java -jar selenium-server-standalone-2.41.0.jar"

You can also do " -port <port_number>" to specify the port number you want.


And you can see it's launching selenium server as a standalone server.




What it's doing here. It's creating a simple web server that's gonna listen on a port.

You can see that it says "RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub".

And so now, if we point a Remote Web Driver in our test at this address it will communicate with this server in order to execute the test.


This is all we need to do to get server running, and we need to leave this open because server is running in this process.


So now, while writing the different test scenarios, instead of using your local drivers:


var driver = new ChromeDriver(@"C:\Drivers")


We are gonna change this and  use what is called a "Remote Web Driver":


var driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox());


Here we are gonna specify two things:



  1. The address of our remote selenium server (new Uri("http://localhost:4444/wd/hub")
  2. The capabilities we want (DesiredCapabilities.Firefox())

We can put here some specific desired capabilities. We can say we want to run Chrome Web Browser and specify the version number and the operating system. And if the server was able to fulfil that, it would try to run what we are requesting.

In this case we are choosing a basic one (DesiredCapabilities.Firefox()), and this is just going to tell the remote selenium server to run our test in Firefox.

We can use other browsers besides Firefox when we run selenium server.
Remember that we ran the command "java -jar selenium-server-standalone-2.41.0.jar", we also have to specify an option that would say where that driver is.

For example for Chrome, we would have to specify where that chrome driver is, just like we had to do here "var driver = new ChromeDriver(@"C:\Drivers")", when we tried to use ChromeDriver locally. Because Chrome Driver doesn't come built in Selenium.

You can run your tests now, and you can see in selenium server command window that it's executing a new session for selenium server, and that Firefox loaded up and it ran your tests.

So that's really all you have to do to get a hub configured.

The next step is to setting up the grid.


We can also setup selenium server in "The Grid Mode". And it's gonna be very similar to what we have done so far. You can see the diagram, that's what we are gonna do here.

I'm gonna set up a Hub and a Node running on my main PC with Windows. 
On virtual machine, I'm gonna run my second PC with Linux and set up another Node.
And then I'm gonna run a third Node on a Mac.

And so on, what we see here is that each one of this nodes it's just gonna be a separate instance of selenium server and we are just gonna specify what role it's playing. 

So for the Hub, we will just specify it's in the "Hub Mode". And then for the Nodes we are gonna specify that they're in the "Node Mode" and then we are gonna tell them where to connect to the Hub.

And by doing that, we'll have all this nodes connected to the hub and we can actually see that the nodes are connected to the hub.

The purpose of doing this is two-fold:
  • It allows you to distribute your tests so that you can run them in parallel. All through the same coordinator which is the Hub.
  • Allows you to run in different platforms and different configurations and have the hub be smart enough to figure out what node it's connected and which node to use based on what kind of requirement you have for running your test.

No comments:

Post a Comment