0

I follow a Page object model and I use a Base class which has all common and reusable method and separate pages for each page of my web application.

Right now I am trying to create a method in BasePage that can be used in other pages. And in each page I have used page factory for the elements.

I use the below method in my BaseClass and I am stuck with extracting the locators into the method below so that I can use this method in all pages for different element locators.

BasePage method:

    protected boolean CheckSorting(List<WebElement> element) {
        List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.)); // stuck here
        LinkedList<String> issueTypes = new LinkedList<String>();
        for (int i = 0; i < issueTypeDropdown.size(); i++) {
//            System.out.println(issueTypeDropdown.get(i).getText());
            issueTypes.add(issueTypeDropdown.get(i).getText());
        }
        return Compare(issueTypes);
    }

I am stuck here:

List<WebElement> issueTypeDropdown = new LinkedList<>(driver.findElements(By.)); // stuck here

My PageFactory in the pages that I use:

public @FindBy(xpath = "(//INPUT[@type='search'])[4]/following-sibling::UL") List<WebElement> List;
moh17
  • 223
  • 1
  • 6
  • 25
  • what is the question ? – Prany Jun 21 '18 at 16:05
  • I have edited the last line of the OP. I need method formatted in a way such that I can use element locators into it. Right now I am not sure how to handle this : `List issueTypeDropdown = new LinkedList<>(driver.findElements(By.)); // stuck here` – moh17 Jun 21 '18 at 16:15
  • so you want to use this method for various location strategies ? – Prany Jun 21 '18 at 16:32
  • Yes ..across different pages – moh17 Jun 21 '18 at 16:34

1 Answers1

2

I've dealt with this in the past, and always had to resort to something else. If you take a look at the WebElement interface definition, there's no method to get the "By" that was used to locate the element.

This question has both answers to your problem: Get the By locator of an already found WebElement

Using a class that implements WebElement and separating both the By and the Webelement and have the By stored on his own reference variable.

On my experience, the best way to deal with this kind of things is to dig on how the page factory works, and try to create your own View Factory and from there, customize whatever you want.

I know that this is not a great answer, but I wanted to share this, the only way to do this kind of thing is by NOT using the page factory, or creating your own.

Cheers!