Using PageObjects, Page Factory and WebDriverWait in Selenium WebDriver using Java

前端 未结 1 2018
自闭症患者
自闭症患者 2020-12-17 01:27

I\'ve been using Selenium WebDriver to implement functional tests for some projects that I\'ve worked with. I\'m trying to use the Page Object design pattern with Page Facto

相关标签:
1条回答
  • 2020-12-17 01:58

    The WebElements in a PageFactroy are really proxies to WebElements. This means that every time you access a WebElement it will perform a search to find the element on the page.

    This has some advantages:

    • When the PageFactory is initialised the proxies are configured, but the WebElements are not found at that point (so you won't get a NoSuchElementException)
    • Every time you use a WebElement it will go and find it again so you shouldn't se StaleElementException's

    You are using the @CacheLookup annotation which is losing you the second benefit as it will find the element once and then keep a reference to it, you are now far more likely to see StaleElementExceptions.

    That being said you still keep the main benefit which is that Selenium will not go to the page and actually find the element until you first use it.

    So in summary all you need to do is move

    PageFactory.initElements(driver, this);
    

    Into your constructor and it will all work fine.

    0 讨论(0)
提交回复
热议问题