Selenium Webdriver: Page factory initialization using paths relative to other elements?

前端 未结 2 1601
逝去的感伤
逝去的感伤 2021-01-03 00:53

I\'m trying to write a page object in Selenium Webdriver using the page factory @FindBy annotations. The page object is for a sidebar, and the parent WebElement

2条回答
  •  长情又很酷
    2021-01-03 00:56

    Answering my own question.

    The answer is to implement an ElementLocatorFactory that allows you to provide a search context (meaning, a driver or a WebElement).

    public class SearchContextElementLocatorFactory
            implements ElementLocatorFactory {
    
        private final SearchContext context;
    
        public SearchContextElementLocatorFactory(SearchContext context) {
            this.context = context;
        }
    
        @Override
        public ElementLocator createLocator(Field field) {
            return new DefaultElementLocator(context, field);
        }
    }
    

    Then, when instantiating your page object, use this locator factory.

    WebElement parent = driver.findElement(By.xpath("//div[contains(@class,'yui3-accordion-panel-content') and child::div[.='Sidebar']]"));
    SearchContextElementLocatorFactory elementLocatorFactory = new SearchContextElementLocatorFactory(parent);
    PageFactory.initElements(elementLocatorFactory, this);
    

    Now your @FindBy annotations will be relative to parent. For example, to get the main sidebar WebElement:

    @FindBy(xpath = ".")
    WebElement sideBar;
    

提交回复
热议问题