christo
christo

Reputation: 93

Selenium TestNG java - too many parameters

I'm doing Selenium Data Driven framework with using TestNG in java language

I have pageObject Login_Page.java to store all locators available on the page.

Then I have appModules Login_Action.java as common function for repeated Login flow.

As you noticed, step in ExecuteLoginAction() are same but have different number of parameters due to test case input needed. How can I optimize the code in such situation?

As in my test script, I'll call Login_Action.ExecuteLoginAction(many...parameters)

How can I avoid this long parameter list in MyTestScript_001Test() and Login_Action.ExecuteLoginAction()

pageObjects Login_Page.java

package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

    public class Login_Page extends BaseClass {

        private static WebElement element = null;

        public Login_Page(WebDriver driver){
                super(driver);
        }

        public static WebElement txt_enterUsername() throws Exception{
            try{
                element = driver.findElement(By.name("username"));
            }catch (Exception e){
                throw(e);
                }
            return element;
        }

        public static WebElement txt_enterPassword() throws Exception{
            try{
                element = driver.findElement(By.name("password"));
            }catch (Exception e){
                throw(e);
                }
            return element;
        }

        public static WebElement btn_clickLoginBtn() throws Exception{
            try{
                element = driver.findElement(By.name("loginBtn"));
            }catch (Exception e){               
                throw(e);
                }
            return element;
        }                                           
    }

appModules Login_Action.java

package appModules;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;

import pageObjects.Login_Page;

    public class Login_Action {     

        public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress, String ColCountry, String ColGender) throws Exception{          

            // Click Login link
            Home_Page.lnk_clickLoginBtn().click();

            // Enter text for Username       
            Login_Page.txt_enterUsername().sendKeys(ColUsername);

            // Enter text for Password 
            Login_Page.txt_enterPassword().sendKeys(ColPassword);

            // Click Login submit button
            Login_Page.btn_clickLoginSubmitBtn().click();

        }

        public static void ExecuteLoginAction(WebDriver driver, String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress) throws Exception{           

            // Click Login link
            Home_Page.lnk_clickLoginBtn().click();

            // Enter text for Username       
            Login_Page.txt_enterUsername().sendKeys(ColUsername);

            // Enter text for Password 
            Login_Page.txt_enterPassword().sendKeys(ColPassword);

            // Click Login submit button
            Login_Page.btn_clickLoginSubmitBtn().click();

        }

    }

Main Test Script (MyTestScript_001)

@Test(dataProvider="MyTestScript_001Data")
    public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
                String ColFirstName, String ColLastName, String ColAddress) throws Exception{


            // Login to web application
            Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
                 ColFirstName,  ColLastName,  ColAddress,  ColCountry,  ColGender);                                             

            // Enter First Name
            UpdateProfile_Page.txtbx_enterFirstName().sendKeys(ColFirstName);

            // Enter Last Name
            UpdateProfile_Page.txtbx_enterLastName().sendKeys(ColLastName);

            Search_Action.ExecuteSearchAction(driver, ColTestCaseName, ColUsername, ColPassword, 
                 ColFirstName,  ColLastName,  ColAddress,  ColCountry,  ColGender);

Main Test Script (MyTestScript_002)

Main Test Script (MyTestScript_002)

@Test(dataProvider="MyTestScript_002Data")
public void MyTestScript_001Test(String ColTestCaseName, String ColUsername, String ColPassword, 
            String ColFirstName, String ColLastName, String ColAddress) throws Exception{


        // Login to web application
        Login_Action.ExecuteLoginAction(driver, ColTestCaseName, ColUsername, ColPassword, 
             ColFirstName, ColLastName, ColAddress);                                                                    

        // Enter text for Address
        UpdateProfile_Page.txtbx_enterAddress().sendKeys(ColAddress);

Your suggestion are greatly appreciate!

Upvotes: 0

Views: 760

Answers (1)

Maikel Arbizu
Maikel Arbizu

Reputation: 11

Makes no sense to send so many parameters, I would send an object :

public class UserInfo {
    private String colTestCaseName;
    private String coldUserName;
    private String colPassword;
    public String getColTestCaseName() {
        return colTestCaseName;
    }
    public void setColTestCaseName(String colTestCaseName) {
        this.colTestCaseName = colTestCaseName;
    }
    public String getColdUserName() {
        return coldUserName;
    }
    public void setColdUserName(String coldUserName) {
        this.coldUserName = coldUserName;
    }
    public String getColPassword() {
        return ColPassword;
    }
    public void setColPassword(String colPassword) {
        this.colPassword = colPassword;
    }
}

Call the method:

UserInfo userInfo = new UserInfo();
//Add credentials
userInfo.setColUsername("user1");
....
Login_Action.ExecuteLoginAction(driver, userInfo);

Upvotes: 1

Related Questions