I need to combine two methods into one method(two methods only xpath values are changing, rest of the functions are same). Two methods are performing same function except xpath value. How I can use this two xpath value in one method and make it two method into one method.
NAV_MENU_ITEMS_LIST= driver.findElement(By.xpath(mat-input-0))
NAV_SUB_MENU_ITEMS_LIST= driver.findElement(By.xpath(//span[text()=' Quick Login ']))
private void methodOne(String menuItemName) {
NAV_MENU_ITEMS_LIST.findBy(text(menuItemName))
.click();
log.info("Navigating to " + menuItemName);
}
private void methodTwo(String subMenuItemName) {
NAV_SUB_MENU_ITEMS_LIST.findBy(text(subMenuItemName))
.click();
log.info("Navigating to " + subMenuItemName);
}
Main Calling method:
public DashboardPage goDashboardManagement() {
methodOne("People");
methodTwo("Salary");
return new PplPage();
}
If you have two or more methods with identical method body and only internal value changes, you could simply pass the variable data as parameters to the method. In your case,
private void commonizedMethod(String menuItemName, WebElement element) {
element.findBy(text(menuItemName)).click();
log.info("Navigating to: " + menuItemName);
}
Alternatively, you could resolve for the element internally by passing the Xpath to the method
private void commonizedMethod(String menuItemName, WebDriver driver, String xpath) {
WebElement element = driver.findElement(By.xpath(xpath));
element.findBy(text(menuItemName)).click();
log.info("Navigating to: " + menuItemName);
}
If the driver is a global parameter, you don't need to pass it in the method.
UPDATE: Based on the update the OP just posted, I am adding additional information to address the issue.
One "trick" I have done over the years is take advantage of overriding methods to support legacy code and expand functionality at the same time. For example, in this case, legacy code has two similar methods that new functionality merged into one.
public DashboardPage goDashboardManagement() {
methodOne("People");
methodTwo("Salary");
return new PplPage(); // not sure what this does or relevancy to the two method calls that preceded
}
public DashboardPage goDashboardManagement(String[] args, WebElement[] elements) {
// check to make sure both arrays are of the same length
for (for int i = 0; i < arr.length, i++) {
commonizedMethod(args[i], elements[i]);
}
return new PplPage(); // not sure what this does or relevancy to the two method calls that preceded
}
Since the old legacy goDashboardManagement()
method have hard-coded values AND follows this pattern, and assuming that
NAV_MENU_ITEMS_LIST
and NAV_SUB_MENU_ITEMS_LIST
are global constants, I could simply update the method body as follows:
public DashboardPage goDashboardManagement() {
String[] arr = {"People", "Salary"};
WebElement elements = {NAV_MENU_ITEMS_LIST, NAV_SUB_MENU_ITEMS_LIST};
commonizedMethod(arr, elements);
return new PplPage(); // not sure what this does or relevancy to the two method calls that preceded
}
This will allow you to remove methodOne(String)
and methodTwo(String)
safely and still keep goDashboardManagement()
to support legacy code. Your unit tests for that method should continue to work because the new version of goDashboardManagement()
and the old version are basically the same even though it has changed internally.
Lastly, you obviously have the burden of creating the needed arrays.