Monday, May 30, 2016

Parallel testing using @parameter in testNG_Cross browser testing using webdriver

This is very common, in web testing when we need to test our application in different browsers and webdriver and testNG together has made our life easy to achieve this thing.

We all know testNG is very rich in features and one of best thing is , it allow you to do your test parallel. The parallel testing features to run your test in diff browser in same time without wasting the time. we can run same test same time in diff browsers. Let's see how we can achieve this:
We will create a testNG class  e.g. "parallelTesting.java" and the xml file name say it is again "paralleltesting.xml"

Here the xml:
























in this xml we have two test : 1- Test in Firefox and other is 2- Test in Chrome
and in both test we use same class "parallelTesting" .
what is noticable here is tag parameter which has name as browser and value is Firefox and Chrome for test1 and test2 respectively.

Lets take a look in to jave file.
package practiceTestNG;

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

public class parallelTesting
{
      WebDriver driver= null;
   
      @BeforeTest
      @Parameters({"browser"})
    public void sendbrowser(String browser)
        {
            if(browser.equals("Firefox"))
                {
                driver = new FirefoxDriver();
                }
                else if(browser.equals("Chrome"))
                {
                System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
                driver = new ChromeDriver();
                }
         
        }
   
    @Test
    public void blogTitle()
        {
        driver.get("http://www.vishalsachan.blogspot.in");
        String title = driver.getTitle();
        Assert.assertEquals("Simplifying the test", title);
        driver.quit();
           
        }
    }
 here we have  one class with @Test annotation and another class with
@Beforetest which will take parameter as browser which will have values as Firefox and Chrome (in our scenarios).

Now run ur testsuit and u will observe that your test is run in both browser parallel.   We can code change the code if we want to include more browsers. so its all. enjoy  cross browser testing without much pain. 
Stay tuned for more fruitful post.

No comments :

Post a Comment