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!
I use POM on a daily basis but not
PageFactory(see NOTE below). If it were me, I would rewrite yourselectItem()method as below.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
searchItemNamedoes not appear on the page, I would do this... same thing as above but wrapped intry-catch.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.