How to click an element which is selected from List <WebElement> in PageObjectClass with PageFactory?

85 views Asked by At

I am using Page Object Model using PageFactory.

Condition: If my search item name matches from the List items, I want to click that item name (WebElement). It works fine in a normal java class but in PageObjectModel I think I am missing something here.

Element declaration in page object class

@FindBy(xpath="....") List <WebElement> searchIteams;
public String selectItem(String searchItemName)
    {
        String  itemInfo="";
        for(WebElement listItem:searchIteams)
        {
            itemInfo=listItem.getText();
            if(itemInfo.equals(searchItemName))
            {
                listItem.click();
                
            break;
            }
        }
        return itemInfo;
    
    }

Return string successfully matches with the item name that I pass in the argument. I dont know why I am not able to click the Matched element.

Here is my testCase method:

@Test (priority=2)
    public void checkItemInResult()
    {
        try {
            
            SearchResult sr= new SearchResult(driver);
            String actualItem=  sr.selectItem("Apple iPhone 14 Plus (256 GB) - Yellow");
            assertEquals(actualItem.equals("Apple iPhone 14 Plus (256 GB) - Yellow"), true);
            
            } catch (Exception e) {
           Assert.fail();
        }
        
        
    }


Please help.

Thank you!
2

There are 2 answers

0
JeffC On

I use POM on a daily basis but not PageFactory (see NOTE below). If it were me, I would rewrite your selectItem() method as below.

public void selectItem(String searchItemName) {
    new WebDriverWait(driver, Duration.ofSeconds(10))
        .until(ExpectedConditions.elementToBeClickable(By.xpath(MessageFormat.format("//span[normalize-space()='{0}']", searchItemName)))).click();
}

With this method, you don't need to assert the returned string because if you successfully click the element, you know it exists... no assert needed.

Now if you want to assert when searchItemName does not appear on the page, I would do this... same thing as above but wrapped in try-catch.

public void selectItem(String searchItemName) {
    try {
        new WebDriverWait(driver, Duration.ofSeconds(10))
        .until(ExpectedConditions.elementToBeClickable(By.xpath(MessageFormat.format("//span[normalize-space()='{0}']", searchItemName)))).click();
    } catch (TimeoutException e) {
        Assert.fail(MessageFormat.format("The <{0}> item does not exist on the page.", searchItemName));
    }
}

NOTE: You may not have a choice on whether you use PageFactory or not if this is for work but the Selenium lead and devs have warned not to use it.

One of the main issues I have with PageFactory, other than the dev's warning, is situations like this where the fetched items can't be filtered dynamically, etc.

1
Stephy Christi On

Thank you, everyone! I figured out what was the problem. The issue was not related to Page Factory but locator. My bad! I modified my locator and it works. Thank you for the all suggestion. :)