Saturday, September 28, 2019

Post#127.Parallel Execution of test In multiple browsers using testNG


Parallel Execution of test In multiple browsers using testNG
Selenium WebDriver does not have any feature which will allow us to perform parallel execution in multiple browsers. So with the help of TestNG we can perform parallel execution in selenium. 
This can be done by using @parameters annotation.

Let’s consider the below example:

package Testng_Pack;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Test_Parallel {

             private  WebDriver driver=null;
            @BeforeClass
            //parameter value will retrieved from testng.xml file's <parameter> tag.
             @Parameters ({"browser"})
             public void setupBrowser(String browser){//Method will pass value of parameter.
             
                         if (browser.equals("FFX")) {//If value Is FFX then webdriver will open Firefox Browser.
               System.out.println("Test Starts Running In Firefox Browser.");
               driver = new FirefoxDriver();  
              }
                else if (browser.equals("CRM")){//If value Is CRM then webdriver will open chrome Browser.
               System.out.println("Test Starts Running In Google chrome.");
               System.setProperty("webdriver.chrome.driver", "C:\\Users\\sujyonta.giri\\Downloads\\chromedriver.exe");
               driver = new ChromeDriver();  
              }
                else if (browser.equals("IE")){//If value Is ie then webdriver will open IE Browser.
                           System.out.println("Test Starts Running In IE.");
                           System.setProperty("webdriver.ie.driver", "C:\\Users\\sujyonta.giri\\Downloads\\IEDriverServer.exe");
                           driver = new InternetExplorerDriver();  
                          }
            driver.manage().window().maximize();
              driver.get("http://newtours.demoaut.com/"); 
             }
             //Both bellow given tests will be executed In all the browsers.
             @Test
             public void verify_title(){  
               String title = driver.getTitle();
               Assert.assertEquals("Welcome: Mercury Tours", title);
               System.out.println("Title Is Fine.");  
             }
             @Test
             public void verify_message(){ 
              driver.findElement(By.name("userName")).sendKeys("UID1");
              driver.findElement(By.name("password")).sendKeys("pass1");
              driver.findElement(By.name("login")).click();
              driver.findElement(By.linkText("Home")).click();
              }
             @AfterClass
             public void closebrowser(){
               driver.quit(); 
             }
            }

In the above code, we have setupBrowser method with @Parameters annotation along with parameter 'browser'. In the xml we will define three tests tags to run each test with different browser. We will compare the browser value with the parameter value and based on that we will create the driver instance. We have now defined three tests with three browsers (Firefox, Google Chrome and Internet Explorer).

Testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="webDriver" parallel="tests">
   <test name="Test In FireFox" >
    <parameter name="browser" value="FFX" />
    <classes>
      <class name="Testng_Pack.Test_Parallel" />
    </classes>
  </test>
   <test name="Test In Google Chrome" >
    <parameter name="browser" value="CRM"></parameter>
    <classes>
      <class name="Testng_Pack.Test_Parallel"></class>
    </classes>
  </test>
  <test name="Test In IE" >
    <parameter name="browser" value="IE"></parameter>
    <classes>
      <class name="Testng_Pack.Test_Parallel"></class>
    </classes>
  </test>
</suite>

In the above testng.xml we have defined parallel="tests" in suite tag. So all the browsers will run in parallel.
If you want to run the browsers one after another then simply remove parallel="tests" from testng.xml file.


No comments:

Post a Comment