Thursday, July 3, 2014

Function Parameters

It is always a good practice to pass parameters when calling the method, rather than providing parameters inside the method. We can pass parameters through methods, just as in normal programming code. The code below will show us how we can login with parameterized username and password.
Steps to follow:
1. First let’s have a look over our previous example of SignIn_Action class.
package appModule;
import org.openqa.selenium.WebDriver;
import pageObject.Login_Page;

public class SignIn_Action{
    public static void Execute(WebDriver driver){
Login_Page.txtbx_UserName(driver).sendKeys("testuser@gmail.com");
Login_Page.txtbx_Password(driver).sendKeys("test@1234");
Login_Page.btn_Login(driver).click();
}
}
2. Modify the above Execute method of class SignIn_Action to accept string Arguments (Username & Password).
package appModule;
import org.openqa.selenium.WebDriver;
import pageObject.Login_Page;

public class SignIn_Action{
    public static void Execute(WebDriver driver, String sUserName, String sPassword){
Login_Page.txtbx_UserName(driver).sendKeys(sUserName);
Login_Page.txtbx_Password(driver).sendKeys(sPassword);
Login_Page.btn_Login(driver).click();
}
}
3. Create a New Class and name it as Param_TC by right click on the automationFramework Package and select New > Class. We will be creating all our test cases under this package. Now convert your old Module_TC in to the new passing parameters based test case.
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import appModule.SignIn_Action;
import pageObject.Home_Page;
public class Param_TC {
       private static WebDriver driver = null;
       public static void main(String[] args) {
       driver = new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get("Application URL");
       driver.manage().window().maximize();
       // Use your Module SignIn now
       SignIn_Action.Execute(driver,"testuser@gmail.com","test@123");
       System.out.println(" Login Successfully.");
       Home_Page.lnk_LogOut(driver).click();
       driver.quit();
       }
}
You see how easy it is to pass parameters to your methods. It increases your code readability.
Note: It is still not the best idea to hard-code your input anywhere in your test script, as it will still impact the bulk of your test scripts if there is any change in user data. I used this example just to give you an idea to how we use parameters in a method.

No comments:

Post a Comment