Friday, June 20, 2014

TestNG Parameters

Everybody knows the importance of Parameterization in automation testing. It allows us to automatically run a test case multiple times with different input and validation values. As Selenium Webdriver is more an automated testing framework than a ready-to-use tool, you will have to put in some effort to support data driven testing in your automated tests. TestNG again gives us another interesting feature called TestNG Parameters. TestNG lets you pass parameters directly to your test methods with your testng.xml.

Steps to follow:
Let me take a very simple example of LogIn application, where the username and password is required to clear the authentication.
1) Create a test to perform LogIn which takes the two string argument as username & password.
2) Provide Username & Password as parameter using TestNG Annotation.

package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;

public class TestngParameters {
            private static WebDriver driver;
  @Test
  @Parameters({ "sUsername", "sPassword" })
  public void testLogin(String sUsername, String sPassword) {
              driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://www.gmail.com");
      driver.findElement(By.id("txtUsername")).sendKeys(sUsername);
      driver.findElement(By.id("txtPassword")).sendKeys(sPassword);
      driver.findElement(By.id("btnLogin")).click();
      driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();
      driver.quit();
  }
}

3. The parameter would be passed values from testng.xml as shown below.
<suite name="Suite">
    <test name="TestParameter">
            <parameter name="sUsername" value="testuser_1"/>
            <parameter name="sPassword" value="Test@123"/>
                        <classes>
                            <class name="automationFramework.TestngParameters" />
                        </classes>
    </test>
</suite>

Now, run the testng.xml, which will run the parameter testLogin method. TestNG will try to find a parameter named sUsername & sPassword.

No comments:

Post a Comment