Execution Of Test Cases In Sequential And Parallel Using Selenium Webdriver

Selenium is an open source tool used to execute the test scripts or the test cases on the Web Application. As we know Selenium is an open source tool, which supports various programming languages such as Java, C#, PHP, JavaScript, Ruby etc.

Selenium supports various operating systems to execute the test scripts on web applications like Windows, Linux, MacOS etc. Selenium is written using one of the famous programming languages; i.e. Java, and it's cross-platform.

Selenium supports various programming languages and various platforms. For these reasons, most of the top companies prefer Selenium rather than using Quick Test Professional (QTP) and CodedUI, which are both licensed versions.

It supports various browsers, such as Google Chrome, Microsoft Edge, Mozilla Firefox, Internet Explorer, Safari etc. Each browser has its own driver for executing the test scripts, such as -  for executing the Selenium scripts on Chrome, we have Chromedrivers;
for IE, we have Internet Explorer drivers; for Firefox, we have geckodrivers; for Safari, we have Safari drivers; and for Opera, we have Opera drivers.

Internet Explorer(IE) and Google Chrome support both, 32-bit and 64-bit, versions which you can download and use based on your system requirements.

Selenium supports both web applications as well as mobile based applications for testing, which is done using Appium. As we know, to execute test scripts or test cases using Selenium, we need to configure hub and node, where hub acts as a centralized location.

We can have only one hub but can have multiple nodes. Each node can be configured to the hub by using its IP Address. We have multiple nodes configured to a single hub.

When we configure multiple nodes to a single hub, hub responsibility is used to distribute the test cases among the configured nodes. We can have multiple nodes of either of the browsers.

If we want to open multiple browser instances, we don't need to configure multiple nodes. In order to open multiple browser instances when executing test scripts or test cases, we just need to use maxInstances attribute while configuring the nodes.

If there are five test cases to execute and only one of the browsers is configured to a hub, then the hub will execute all the five nodes on the same connected or configured node.

If there are same five test cases to execute and two different browsers are configured to a hub, the hub will divide or distribute the five test cases between the two configured nodes.

Suppose, we have 10 test cases and 5 nodes are configured to a single hub. The hub distributes these ten test cases to the configured 5 nodes, like 2 test cases for each node.

Syntax to configure hub
  • java -jar selenium-server-standalone.3.4.0.jar -role hub
Here, selenium-server-standalone.3.4.0.jar is a Java jar file. To execute the above command, Java Runtime Environment (JRE) is required to be downloaded.
  • Role hub is used to configure as an hub.
Syntax for configuring multiple nodes

java -jar selenium-server-standalone.3.4.0.jar -role -node- hub http://192.168.122.1:4444/grid/register -browser browsername="firefox" , version=ANY, platform=ANY ,
maxInstances=10 -Dwebdriver.firefor.driver="D:/drivers/Geckodriver/geckodriver.exe" -browser browsername="chrome" , version=ANY , platform=ANY , maxInstances=10 -
Dwebdriver.chrome.driver="D:/drivers/chromedriver/chromedriver.exe" -browser browsername="internet explorer" , version=ANY , platform=ANY , maxInstances=10 -
Dwebdriver.ie.driver="D:/drivers/InternetExplorerDriver/iedriver.exe"
-role -node- hub http://192.168.122.1:4444/grid/register : It is nothing but we are configuring an node either firefox , chrome , IE to the hub which has an IP address of
192.168.122.1 and with port no. 4444

maxInstances defines the number of instances of browsers we want to open. We need to define the path of the drivers which we installed into the drivers folder. Wemust provide the .exe path of the nodes based on which we are configuring.

In the above code, we configured 3 nodes that is Firefox, Chrome, & IE with their paths like,
  • Dwebdriver.chrome.driver="path of chrome .exe"
  • Dwebdriver.ie.driver="path of IE .exe"
  • Dwebdriver.firefox.driver="path of Firefox .exe"
In the above commands, we configured 3 different nodes to a single hub. If there are multiple test cases to execute, then it is hub's responsibililty to divide or distribute the test cases among the 3 given nodes.

Using Selenium webdriver, we can execute test scripts or test cases either sequentially or parallely using gallio bundle which is a tool of selenium.

To execute test cases parallely, we need to use [TestFixture] & [parallelizable] attributes and maxinstances attribute. By default, the maxInstance attribute has a default value of 1, that means when we execute the testcase, hub opens only single browser at a time. If we define maxInstances=5 while configuring a node, then based on multiple test cases to execute, it opens 5 different browser instances to execute the given test cases.

If we set maxinstances=10 then we can open 10 different instances of chrome or Internet explorer or firefox etc simultaneously which helps us to execute the test cases in multiple instance browsers which reduces the manpower as well as we can reduce the time.

In selenium webdriver , we can execute the testcases either parallely or sequentially. In certain scenarios we were unable to automate the application based on the sequential order of the testcases. Consider certain scenario like below, If i want to automate the facebook application with the following steps.
  1. First i want to register to an facebook application.
  2. Then i Need to login to facebook.
  3. Then i want to post something.
  4. In the fourth step i want to Update the content of the post.
  5. In this step i want to delete the post.
From the above given scenarios , let us consider the above steps as TestMethods like RegisterToFacebook(), LoginToFB(), PostToFB() , UpdatePost(), & DeletePost() etc ,then when we execute them , it is not executing in the given order rather it executing in jumble order like
first it is trying to execute the DeletePost() method , but there is no post to delete it and hence we are getting failed result.

To execute testcases in sequential mode or sequential order then we need to make use of [ProcessTestFixture] and [TestSequence] attribute.

To Execute test cases in sequentially in the given selected order, we need to add Mbunit.Framework.dll in projects references.
  1. Using system;  
  2. Using Mbunit.Framework;  
  3. [ProcessTestFixture]  
  4. public class TestClass {  
  5.     public TestClass {}  
  6.     [Test]  
  7.     [Mbunit.Framework.TestSequence(1)]  
  8.     public void TestMethod1() {  
  9.             //code to execute testmethod1  
  10.         }  
  11.         [Test]  
  12.         [Mbunit.Framework.TestSequence(2)]  
  13.     public void TestMethod2() {  
  14.             // code to execute testmethod2  
  15.         }  
  16.         [Test]  
  17.         [Mbunit.Framework.TestSequence(3)]  
  18.     public void TestMethod3() {  
  19.             // code to execute testmethod3  
  20.         }  
  21.         [Test]  
  22.         [Mbunit.Framework.TestSequence(4)]  
  23.     public void TestMethod4() {  
  24.             // code to execute testmethod4  
  25.         }  
  26.         [Test]  
  27.         [Mbunit.Framework.TestSequence(5)]  
  28.     public void TestMethod5() {  
  29.         // code to execute testmethod5  
  30.     }  
  31. }  
From the above code, for sequential order we need to replace the [TestFixture] attribute to [ProcessTestFixture] . If we don't replace it, then we will be getting same failed result because the above code will not be executing in the given order which we have provided.

When we execute the above project, we can execute it in the provided order because of TestSequence() method. This TestSequence() method accepts a parameter of type (int order). Here, we need to provide the order which we want them to execute. In this case, we get the pass result.

To Execute test cases parallely

In order to execute the test cases parallely, we need to add two different dll's to its project references that is,
  1. Mbunit.dll
  2. Gallio.dll
which we can get from NuGet.
 
Given below is the program to understand the execution of test cases in parallel.
  1. using system;  
  2. using mbunit;  
  3. [assembly: DegreeOfParallelism(20)]  
  4. [assembly: Parallelizable(TestScope.All)]  
  5. [TestFixture][Parallelizable]  
  6. public class Testclass {  
  7.     public Testclass() {}  
  8.         [Test][Parallelizable]  
  9.     public void TestMethod1() {  
  10.             // code for TestMethod1  
  11.         }  
  12.         [Test][Parallelizable]  
  13.     public void TestMethod2() {  
  14.             //code for TestMethod2  
  15.         }  
  16.         [Test][Parallelizable]  
  17.     public void TestMethod3() {  
  18.             //code for TestMethod3  
  19.         }  
  20.         [Test][Parallelizable]  
  21.     public void TestMethod4() {  
  22.             //code for TestMethod4  
  23.         }  
  24.         [Test][Parallelizable]  
  25.     public void TestMethod5() {  
  26.         //code for TestMethod5  
  27.     }  
  28. }  
Thanks! I hope this helps you.


Similar Articles