Friday, February 15, 2013

Passing parameters to TestCase using testng

Passing parameter using TestNg

  • Some times there is a need to send parameters (like browser name, browser version ..etc).
  • May be you want to run the same test case with different values for same attribute.
You can achieve above cases by using @parameter annotation in testng

Below is the example for running same test cases in different browsers (firefox, chrome) by passing different parameters (browser name, version, profile)

TestNg Class
import org.openqa.selenium.WebDriver;  
   import org.testng.annotations.Parameters;  
   import org.testng.annotations.Test;  
   import org.testng.annotations.BeforeMethod;  
   import org.testng.annotations.AfterMethod;  
   import org.testng.annotations.BeforeClass;  
   import org.testng.annotations.AfterClass;  
   import org.testng.annotations.BeforeTest;  
   import org.testng.annotations.AfterTest;  
   public class ExampleTestCase   
   {  
     private static WebDriver driver;
     @Parameters({"browser,version"})
     @BeforeClass
     public void beforeClass(String browser,String version,String profile)
     {
          driver=getDriverInstance(browser,version,profile);
     }
     @BeforeTest
     public void beforeTest()
     {
     }  
     @Test
     public void f()
     {
          //your test code here
     }
   @AfterTest
    public void afterTest()
    {
    }
    @AfterClass
    public void afterClass()
    {
        driver.quit();
    }
}

getDriverInstance method implimentation
public static WebDriver getDriverInstance(String browser,String version,String profile)  
  {  
     WebDriver driver=null;  
     if(browser.equals("firefox"))  
     {  
       DesiredCapabilities capability = DesiredCapabilities.firefox();  
       capability.setVersion(version);
       capability.setCapability(FirefoxDriver.PROFILE, profile);
       driver = new FirefoxDriver(capability);  
     }  
     else if(browser.equals("chrome"))  
     {  
         DesiredCapabilities capability = DesiredCapabilities.chrome();  
         capability.setVersion(version);  
         driver = new ChromeDriver(capability);  
     }  
     return driver;  
  }  

TestNg Suite
<?xml version="1.0" encoding="UTF-8"?>  
   <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
   <suite thread-count="2" name=MyTestSuite" parallel="tests">  
       <test name="RunInFirefox" preserve-order="false">
          <parameter name="browser" value="firefox">
          <parameter name="version" value="8"/>
          <parameter name="profile" value="default">
                <classes preserve-order="true">  
                      <class name="com.test.TestCase1"/>  
                      <class name="com.test.TestCase2"/>  
                      <class name="com.test.TestCase3"/>  
                </classes>  
       </test>  
       <test name="RunInChrome" preserve-order="false">
          <parameter name="browser" value="chrome">
          <parameter name="version" value="21"/>  
               <classes preserve-order="true">  
                    <class name="com.test.TestCase1"/>  
                    <class name="com.test.TestCase2"/>  
                    <class name="com.test.TestCase3"/>  
               </classes>  
        </test>  
</suite>  



Related Topics
Running Junit Cases from command prompt
Upload photo in facebook using webdriver

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.