Issue:
The target webpage has many buttons in the page and adding separate abstraction for each method like , click button1 , button2 etc feels like unnecessary and just creates additional lines of code.
So to address this the below approaches where suggested:
Approach 1:
Create a map of element and pass the element name as key
Page Object:
package page_objects;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import driversetup.TestBaseClass;
public class homepage extends TestBaseClass {
private Map<String, WebElement> elements = new HashMap<String, WebElement>();
public homepage() {
driver.get().get("https://courses.ultimateqa.com/users/sign_in");
}
public void initializeElements() {
elements.put("password_field", driver.findElement(By.xpath("//input[@type=\"password\"]")));
elements.put("email_field", driver.findElement(By.xpath("//input[@type=\"email\"]")));
}
public homepage inputText(String field, String value) {
elements.get(field).sendKeys(value);
return this;
}
}
Test case:
package test_suites;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Utils.excelParcer;
import driversetup.TestBaseClass;
import page_objects.homepage;
public class Testcase1 extends TestBaseClass {
homepage homepage;
@BeforeMethod
public void setUp() {
initializeTestBase();
homepage = new homepage();
homepage.initializeElements();
}
@Test(description = "Test case 1 assert True")
public void loginPageTitleTest2() {
homepage.inputText("email_field", "test").inputText("password_field", "text2").click(homepage.signin);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Approach 2:
Make elements public and pass the element to the method
Page Object:
package page_objects;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import driversetup.TestBaseClass;
public class homepage extends TestBaseClass {
public WebElement email;
public WebElement password;
public WebElement signin;
public homepage() {
driver.get("https://courses.ultimateqa.com/users/sign_in");
}
public void initializeElements() {
email= driver.findElement(By.xpath("//input[@type=\"password\"]"));
password = driver.findElement(By.xpath("//input[@type=\"email\"]"));
submit= driver.findElement(By.xpath("//input[@type=\"submit\"]"));
}
public homepage inputText(WebElement field, String value) {
field.sendKeys(value);
return this;
}
public homepage click(WebElement field) {
field.click();
return this;
}
}
Test case:
package test_suites;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Utils.excelParcer;
import driversetup.TestBaseClass;
import page_objects.homepage;
public class Testcase1 extends TestBaseClass {
homepage homepage;
@BeforeMethod
public void setUp() {
initializeTestBase();
homepage = new homepage();
homepage.initializeElements();
}
@Test(description = "Test case 1 assert True")
public void loginPageTitleTest2() {
homepage.inputText(homepage.email, "test").inputText(homepage.password, "text2").click(homepage.signin);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Question
Which of the two is better , and if both are not suggested why would making webelement private and create separate methods for each element recommended