Tuesday, April 4, 2017

Parallel testing on Selenium Grid C# MStest

This is to explain how to use parallel test execution on Selenium grid using MStest.

As on the previous Post you have to create some remote web browsers  
So i have 5 free FireFox, 5 Chrome and a 1 IE  instances available on a server.

192.168.134.67 is my remote server.
5557 is the connection port to the Selenium Grid Host server.

So my 1st test method will looks like this.
I have used one and only IE instance which was available on my selenium grid.
I dont have any asserts ,Only navigating to Yahoo.


[TestMethod]
        public void SeleniumGridYahoo()
        {
            Uri hub = new Uri("http://192.168.134.67:5557/wd/hub");
            DesiredCapabilities capabilities = DesiredCapabilities.InternetExplorer();
            Environment.SetEnvironmentVariable("webdriver.IE.driver", @"C:\Documents\SeleniumGrid\IEDriverServer.exe");

            RemoteWebDriver driver3 = new RemoteWebDriver(hub, capabilities);
            driver3.Navigate().GoToUrl("http://www.yahoo.com");
        }

And then going to utilizing one of my FireFox instance this time,

[TestMethod]
        public void SeleniumGridFacebook()
        {
            Uri hub = new Uri("http://192.168.134.67:5557/wd/hub");
            DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
          Environment.SetEnvironmentVariable("webdriver.geko.driver", @"C:\Users\ugljenkins\Documents\SeleniumGrid\geckodriver.exe");
            RemoteWebDriver driver3 = new RemoteWebDriver(hub, capabilities);
            driver3.Navigate().GoToUrl("http://www.facebook.com");

        }

So now i have two test cases.

If i run these test cases individually by the test explorer it will open a IE or FireFox browser at the  192.168.134.67 server and execute the test.

If i run these test cases as a group by the test explorer it will open a IE and FireFox browser one after another at the  192.168.134.67 server and execute the test.






But if i do the Test execution in a Parallel way ,
Both browsers will open same time at the server and start navigating the relevant web URL.

So it will cut down the half of my execution time.


  1. Simply add a new XML file to the project.
  2. Add parallelTestCount attribute to the excel file.
  3. Set it as 2 (two means you can run up to two test cases parallelly)
  4. Save and rename it to testsettings.



Sample XML file.


<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Local" id="953cfca7-2c66-4909-bc08-b8ce43651474" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment enabled="false" />
  <Execution parallelTestCount="2">
    <TestTypeSpecific />
    <AgentRule name="Execution Agents">
    </AgentRule>
  </Execution>

</TestSettings>

DONE.

Monday, April 3, 2017

Testing on Multiple web browsers - Selenium Grid - C#

Most easiest way to create list of web browsers in a one single PC/Server is having a selenium GRID.

1. Download the selenium server from SeleniumHQ.
https://goo.gl/uTXEJ1

2. We need to execute the downloaded selenium server.
As it is a jar file we need to execute it via CMD.

3. Execute the jar file with below code.
java -jar selenium-server-standalone-3.3.1.jar -role hub

If the selenium grid working fine you have to get this INFO on your CMD.
INFO - Selenium Grid hub is up and running

4. Two double check it ,Navigate to http://localhost:4444/grid/console.
You should see something like this.



5. Then we have to add Web browsers to this grid.
If we add chrome driver by default it will give us  5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.
Below is how we add the chrome driver in order to get 11 default web browsers.

java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.3.1.jar -role node -hub http://localhost:4444/grid/register

highlighted EXE should be replace with the EXE file path to the latest chrome web driver which you can download from Selenium HQ.

2nd highlighted are is the URL where we have already host our Selenium grid.


6. If you wont any other browsers then navigate to Selenium HQ and download any browser's WEB DRIVER.
Then execute the below code.

java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.3.1.jar -port 5556 -role node -hub http://localhost:4444/grid/register -browser "browserName=chrome, version=ANY, maxInstances=10, platform=WINDOWS

First line you need to mention the exact driver name and the driver EXE name.
Then the jar file name where we use to create the selenium GRID at the beginning.

7. Then again navigate to http://localhost:4444/grid/console. 

8. You should see 10 chrome icons at the Selenium grid server.
We called it as Chrome instances.




9. Now the Grid is up and ruining,Just access the browsers at the Grid by below code.

 Uri hub = new Uri("http://localhost:4444/wd/hub");
 DesiredCapabilities capabilities = DesiredCapabilities.Chrome(); <You can change the driver to IE or FireFox.>
 IWebDriver driver2 = new RemoteWebDriver(hub, capabilities);

 driver2.Navigate().GoToUrl("http://www.yahoo.com");