Tuesday, June 7, 2016

Test Execution Using Selenium Grid

So as I mentioned last time, I have been at work trying to figure out how to get tests to run on Selenium Grid, which wasn't that hard to figure out. The set up just needs a few manual steps, buts its really easy.

First, there is only a few lines of code that we need change in order for it to work. If you remember my last post on Selenium, I will be using the same code I did then and just update it.

The new code looks something like this:

using System;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest
{
    class GenerateTokens
    {
        private RemoteWebDriver driver;

        public static void Main()
        {
            Generator();
            Environment.Exit(1);
        }

        private static void Generator()
        {
            var capabilities = new DesiredCapabilities();
            capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
            capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
            driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
            driver.Manage().Window.Maximize();

            driver.Url = "some-site.com";

            for (int counter = 0; counter < 10; counter++)
            {
                // Do something 
            }

            driver.Quit();
        }
    }
}

As you can see, we use a RemoteWebDriver object instead of just a IWebdriver like we did before. This basically is saying that the test will not be running locally (but it can, more on that later) and that we will be using Selenium Grid to run the test on a different machine. We specify that we want to run the test on a machine that has Chrome as the browser and that its also a Windows machine. After that, we don't really care what computer is running those test, all we care is that it meets those two requirements for the test.

But if you were to run this test now, it would fail, because it wouldn't be able to run the test locally. The test runner is looking for an instance of Selenium Grid at the address that you provided "http://localhost:4444/wd/hub" and it can't find it. Now you have to set up the grid with only one hub and at least one node to run the test. Here are the steps for the set up:
  1. First you have to have Java installed in your machine and make sure that it was added to your PATH. 
  2. Next download "Selenium Standalone Server" from the Selenium downloads page. And put it in a folder that you want, I created a folder in my C driver called Selenium for it. 
  3. You will need to open a command window and navigate to the folder and enter: "java -jar selenium-server-standalone-2.53.0.jar -role hub"
  4. You will then need to open a new command window and navigate to the folder again and enter: "java -jar selenium-server-standalone-2.53.0.jar -role node  -hub http://localhost:4444/grid/register"
At this point you have now set up a grid, comprised of just your computer but it it will get the job done for now. If you open a browser window and go to "http://localhost:4444/grid/console", you will see that your grid has one node available to run tests and which browsers are available on the node.

Now if you run the test that we wrote before, it will work. The test runner will send a test to the server and the server will then decide which "node" to send the test to. Then the server will return the test result to the test runner. That's a really simplistic way of explaining the process, but at least that's how I understand it.

We are still running into issues at work where we can't get the tests to run in parallel, but hopefully we will figure it out soon. That's all for now, thanks for reading!

No comments:

Post a Comment