I am very beginner with Selenium and I'd like to get an advice how to optimize the code. I have 3 tooltips on the website and want to do the same actions for all of them.
I have written so far:
public void validateTooltipIsPresent() {
Actions builder = new Actions(SeleniumDriver.getDriver());
WebElement usernameTooltip = SeleniumDriver.getDriver().findElement(By.xpath("xpath1"));
builder.moveToElement(usernameTooltip).perform();
String tooltipMsg = usernameTooltip.getText();
System.out.println(tooltipMsg);
WebElement usernameTooltip2 = SeleniumDriver.getDriver().findElement(By.xpath("xpath2"));
builder.moveToElement(usernameTooltip2).perform();
String tooltipMsg2 = usernameTooltip2.getText();
System.out.println(tooltipMsg2);
WebElement usernameTooltip3 = SeleniumDriver.getDriver().findElement(By.xpath("xpath3"));
builder.moveToElement(usernameTooltip3).perform();
String tooltipMsg3 = usernameTooltip3.getText();
System.out.println(tooltipMsg3);
My idea is to add Webelements to an ArrayList and then iterate though it. Is that good solution?
The method public void validateTooltipIsPresent() does not have any parameters, but if I add (Webelement element), then in step definitions, the arguments will be required.
This is the method in step definition:
@Then("^In pricing section ensure tool tip display for List price, Net Price, and packaging conversion$")
public void in_pricing_section_ensure_tool_tip_display_for_list_price_net_price_and_packaging_conversion() throws Throwable {
pdpActions.validateTooltipIsPresent();
}