user3325340 user3325340 - 14 days ago 6
Java Question

Using JAVA array or list to fill text boxes on a page designed by Selenium Page Object Model

package com.objects;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import com.tests.BaseClass;

public class LinkedInHomePage extends BaseClass {

public LinkedInHomePage(WebDriver driver) {
PageFactory.initElements(driver, this);
}

@FindBy(how = How.ID, using = "firstName-coldRegistrationForm")
public WebElement newFirstNameTexthBox;

@FindBy(how = How.NAME, using = "lastName")
public WebElement newLastNameTexthBox;

@FindBy(how = How.ID, using = "email-coldRegistrationForm")
public WebElement newEmailTexthBox;

@FindBy(how = How.ID, using = "password-coldRegistrationForm")
public WebElement newPasswordTexthBox;

@FindBy(how = How.ID, using = "btn-submit")
public WebElement signUpButton;

public void EnterNewFirstName(String inComingNewFirstName) {
newFirstNameTexthBox.clear();
newFirstNameTexthBox.sendKeys(inComingNewFirstName);
}

public void EnterNewLastName(String inComingNewLastName) {
newLastNameTexthBox.clear();
newLastNameTexthBox.sendKeys(inComingNewLastName);
}

public void EnterNewEmail(String inComingNewEmail) {
newEmailTexthBox.clear();
newEmailTexthBox.sendKeys(inComingNewEmail);
}

public void EnterNewPassword(String inComingNewPassword) {
newPasswordTexthBox.clear();
newPasswordTexthBox.sendKeys(inComingNewPassword);
}

public void ClickSignUp() {
signUpButton.click();
}

public void JoinNow(String FName, String LName, String Email,
String Password) {
EnterNewFirstName(FName);
EnterNewLastName(LName);
EnterNewEmail(Email);
EnterNewPassword(Password);
ClickSignUp();
}


}

The JoinNow() function above as you can see takes multiple parameters and it works perfectly fine. I would like use array or list to reduce
the number of arguments and then use loop to fill the text boxes. I want to accomplish something similar as below, but since I am using Page Object Model Design, can't use findElement.

@SuppressWarnings("unchecked")
protected void JoinNow(String... var) {
List<MyElements> inputElements = new ArrayList<MyElements>();
inputElements.add((MyElements) driver.findElement(By
.id("firstName-coldRegistrationForm")));
inputElements.add((MyElements) driver.findElement(By.id("lastName")));
inputElements.add((MyElements) driver.findElement(By
.id("email-coldRegistrationForm")));
inputElements.add((MyElements) driver.findElement(By
.id("password-coldRegistrationForm")));

for (int i = 0; i < var.length; i++) {
((WebElement) inputElements.get(i)).sendKeys(var[i]);
}
}

Answer

Why not send a Map to JoinNow method. I suggest you keep your PageObject pattern as is. I believe this is more readable and more maintainable long term

Map<String,String> data  = new HashMap<String,String>();
data.put("firstname", "George");
data.put("lastname", "Clooney");
data.put("email", "George@xyz.com");
data.put("password", "Dontguess");
JoinNow(data);

protected void JoinNow(Map<String,String> data) {
    firstNameElement.sendKeys(data.get("firstname"));
    lastNameElement.sendKeys(data.get("lastname"));
    emailElement.sendKeys(data.get("email"));
    passwordElement.sendKeys(data.get("password"));
}