Selenium WebDriver Parallel Tests Execution Using TestNG - @Parameters

Browser compatibility software testing Is most Important thing for any software web application and generally you have to perform browser compatibility testing before 1 or 2 days of final release of software web application. In such a sort time period, you have to verify each Important functionality In every browsers suggested by client. If you will go for running your all webdriver tests In each browsers one by one then It will take too much time to complete your software tests and you may not complete It before release. In such situation, Running your tests In all required browsers at same time will helps you to save your time efforts. So question Is - Can we run our tests parallel In all required browsers using webdriver software automation testing tool? Answer Is yes.

Before learning how to run webdriver test parallel in multiple browsers, You must have knowledge about how to run test In webdriver using TestNg framework. You will find links of TestNG tutorial post with examples on THIS PAGE. I have described testng configuration with detailed explanation on those links so read them carefully one by one. You can read webdriver related more tutorials on THIS LINK.

Parallelism In TestNG
You can configure your testng.xml file In such a way to run your test suite or tests or methods In separate browsers Is known as parallelism In TestNG. Interviewer can ask you this question. Now let us look at example of parallel test execution In webdriver using testng. Bellow given example will run the test In Mozilla Firefox and Google chrome browser parallel.

Created Test_Parallel.java class file for testing application and configured testng.xml file to run tests parallel as shown In bellow given example. In testng.xml file, parallel="tests" Inside <suite> tag will Instruct TestNG to consider bunch of methods of each <test> tag as separate thread. Means If you wants to run your software test In 2 different browsers then you need to create two <test> blocks for each browser Inside testng.xml file and Inside each <test> tag block, define one more tag "parameter" with same name(In both <test> block) but with different values. In bellow given testng.xml file, each <test> block has "parameter" tag with same name = browser but different values(FFX and CRM).

In test case, I have used @Parameters annotation to pass parameter In method. Values of this parameter will be feed by testng.xml file. and then If condition will check that value to decide which driver to use for test. This way, example testng.xml file will feed two values(FFX and CRM) In parameter so It will open Firefox and Google chrome browsers and run test In both browsers. (For running test In google chrome, You need to download chromedriver.exe. VIEW THIS POST to know more how to run webdriver test In google chrome browser).

Run bellow given test In your eclipse with testng to see how It runs your test In 2 browsers at same time.

Test_Parallel.java
package Testng_Pack;
import org.junit.Assert;

public class Test_Parallel {

 private  WebDriver driver=null;
 @BeforeClass
//parameter value will retrieved from testng.xml file's <parameter> tag.
 @Parameters ({"browser"})
 public void setup(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.");
   System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
   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", 
     "D:\\chromedriver_win32_2.3\\chromedriver.exe");
   driver = new ChromeDriver();   
  }
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.com/2014/05/login.html");  
 } 
 //Both bellow given tests will be executed In both browsers.
 @Test
 public void verify_title(){   
   String title = driver.getTitle();
   Assert.assertEquals("Only Testing: LogIn", title);
   System.out.println("Title Is Fine.");   
 } 
 @Test
 public void verify_message(){  
  driver.findElement(By.xpath("//input[@name='userid']")).sendKeys("UID1");
  driver.findElement(By.xpath("//input[@type='password']")).sendKeys("pass1");
  driver.findElement(By.xpath("//input[@value='Login']")).click();
  String alert = driver.switchTo().alert().getText();
  driver.switchTo().alert().accept();
  Assert.assertEquals("UserId Is : UID1  Password Is : pass1", alert);
  System.out.println("Alert Is Fine.");  
 } 
 @AfterClass 
 public void closebrowser(){
   driver.quit();  
 }
}

testng.xml
<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> 
</suite>

If you wants to run your software test In one more browser then you need to create new <test> tag block in testng.xml file and set another parameter value and then put one more if else condition in class file to check and run your test.

This way you can your test parallel In multiple browsers at same time to reduce your time efforts.

9 comments:

  1. how to create and why to create tstng.xml? what is actually this testng.xml file

    ReplyDelete
  2. please read testng.xml ( start of this tutoril)

    ReplyDelete
  3. How to automate If Webpage is designed in .net ?

    ReplyDelete
  4. do you have a tutorial of creating a user profile in Chrome?
    I saw the sample in your site using the FIREFOX.
    I wanted also to hide the authentication required dialog in Chrome.
    Thanks for the tutorial.

    ReplyDelete
    Replies
    1. You can handle it using autoit in any browser-> http://software-testing-tutorials-automation.blogspot.in/2014/12/how-to-handle-http-proxy-authentication.html

      Delete
  5. How to run this via maven using pom.xml??
    I found an article which provides a way to pass parameter to testNG @Parameter annotated test methods but it passes only 1 value.
    So if we want to run the above program how do we pass 2 values for the variable browser i.e CRM and FFX ?


    ReplyDelete
  6. Great tutorial Aravind G

    There are classes that not are imported in Test_Parallel class, they are necessary to execute the code:

    import org.testng.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;


    Thanks for your work! I am learning a lot.

    ReplyDelete
  7. Hi Aravind,
    I m getting below error.Please help
    org.testng.TestNGException:
    Parameter 'browser' is required by @Configuration on method setup but has not been marked @Optional or defined

    Thanks in advance,
    Ranjani

    ReplyDelete