So you have big page classes in your project, like this one:
public class CheckOutPage {
private static final String URL = "https://www.sell.com/checkout";
//shipping info locators
private static final By ADDRESS_ID = By.id("address");
private static final By CITY_ID = By.id("city");
private static final By PROVINCE_ID = By.id("province");
private static final By POSTAL_CODE_ID = By.id("postalcode");
//contact info locators
private static final By FIRST_NAME_ID = By.id("firstname");
private static final By LAST_NAME_ID = By.id("lastname");
private static final By PHONE_ID = By.id("phone");
private static final By EMAIL_ID = By.id("email");
//order summary locators
private static final By SUB_TOTAL_ID = By.id("subtotal");
private static final By ORDER_DISCOUNT_ID = By.id("orderdiscount");
private static final By ESTIMATED_SHIPPING_ID = By.id("estimatedshipping");
private static final By ESTIMATED_TAX_ID = By.id("estimatedtax");
private static final By ESTIMATED_TOTAL_ID = By.id("estimatedtotal");
//continue locator
private static final By CONTINUE_ID = By.id("continue");
private WebDriver driver;
public CheckOutPage(WebDriver driver) {
this.driver = driver;
}
//shipping info methods
public void typeAddress(String address) {
WebElement field = driver.findElement(ADDRESS_ID);
field.sendKeys(address);
}
public void typeCity(String city) {
WebElement field = driver.findElement(CITY_ID);
field.sendKeys(city);
}
public void typePostalCode(String postalCode) {
WebElement field = driver.findElement(POSTAL_CODE_ID);
field.sendKeys(postalCode);
}
public void selectProvince(String province) {
WebElement field = driver.findElement(PROVINCE_ID);
Select listbox = new Select(field);
listbox.selectByValue(province);
}
//contact info methods
public void typeFirstName(String name) {
WebElement field = driver.findElement(FIRST_NAME_ID);
field.sendKeys(name);
}
public void typeLastName(String name) {
WebElement field = driver.findElement(LAST_NAME_ID);
field.sendKeys(name);
}
public void typePhone(String phone) {
WebElement field = driver.findElement(PHONE_ID);
field.sendKeys(phone);
}
public void typeEmail(String email) {
WebElement field = driver.findElement(EMAIL_ID);
field.sendKeys(email);
}
//order summary methods
public double getProductSubTotal() {
WebElement field = driver.findElement(SUB_TOTAL_ID);
return Double.parseDouble(field.getText());
}
public double getOrderDiscount() {
WebElement field = driver.findElement(ORDER_DISCOUNT_ID);
return Double.parseDouble(field.getText());
}
public double getEstimatedShippingPrice() {
WebElement field = driver.findElement(ESTIMATED_SHIPPING_ID);
return Double.parseDouble(field.getText());
}
public double getEstimatedTax() {
WebElement field = driver.findElement(ESTIMATED_TAX_ID);
return Double.parseDouble(field.getText());
}
public double getEstimatedTotal() {
WebElement field = driver.findElement(ESTIMATED_TOTAL_ID);
return Double.parseDouble(field.getText());
}
//place order methods
public void placeOrder() {
WebElement button = driver.findElement(CONTINUE_ID);
button.click();
}
public boolean isDisplayed() {
String url = getUrl();
return url.contains(URL);
}
private String getUrl() {
return driver.getCurrentUrl();
}
}
Long class, isn’t it? 113 lines of code.
How can we make it smaller?
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.