2

I use Page object pattern with Page Factory in selenium Webdriver with Java (as language).

Will my inputSearch variable be still referencing to the same webelement on page, after refreshing the respective page?

@FindBy(id = "inputSearch")
private WebElement inputSearch;
2
  • Add some more details about your exact problem? Did you mean that -How to refresh a page in POM with pagefactory pattern?
    – Bharat Mane
    Commented Aug 17, 2017 at 11:18
  • I want to refresh value in this WebElement object Commented Aug 17, 2017 at 11:20

4 Answers 4

1

PageFactory follows the LazyLoading design pattern where the variable initialization is delayed till the object is needed.

So, once you do the PageFactory.initElements(driver, this) the variables will be wired. Now if you have have not done any operation(sendKeys etc.) on the element inputSearch prior to the page refresh then you the variable will be refer to the same element you desire.

But if you have done some kind of operation on it(i.e. the variable is initialized), then you might get a StaleElementException.

2
  • No, you will never get stale element, element is only initialized when you do something with it. You can Click, Refresh page, Click and so on.
    – George
    Commented Aug 17, 2017 at 16:33
  • My point was on getting stale element exception is that if you do some operation on the element and refresh the page which changes some property of the element, then you might get a staleElementException.
    – Swastik
    Commented Aug 17, 2017 at 16:57
0

No. After refresh, you need to locate the element again (using same locator).

Page factory is irrelevant. After refresh, all objects on the page are rebuild, so all references to previous versions (before refresh) are invalid.

5
  • No, its not irrelevant. It works like this. Find Element > Click, Find Element > Click with PageFactory
    – George
    Commented Aug 17, 2017 at 16:35
  • Irrelevant is a sense what regardless if you use page factory or not, after refresh, you need to locate elements again. Unless you are using some fancy schmancy factory which can detect that page was refreshed and will redo the locating for you. Page Factory is just a design pattern, there might be zillions implementations with different features. Commented Aug 17, 2017 at 16:45
  • No, you dont. Pagefactory does locate elements, exactly before interacting with them.Therefore you can element.Click, pagerefresh, element.Click, with no problems, meaning while 'technically' elements are being found, you dont have to manually do that. But yes, normally, el = findElement, el.click, pagrefresh, el.click would result in error.
    – George
    Commented Aug 17, 2017 at 17:20
  • Implementation of PageFactory design pattern you are lucky to use does what you say. Other implementation might not. Are you 100% that OP's version does it? And if it does, why he has the problem? Commented Aug 17, 2017 at 19:03
  • @George - maybe you want to write your own answer, instead of commenting on answers of others? Feel free to do so. Commented Aug 17, 2017 at 21:48
0

i Just tried this on my project.

When PageFactory is called, the Element has different value e1 has element value and e2 has 0 value . lets say two elements e1 and e2, e1 is a slider element and e2 represents the changed value when e1 used as Slider.

sequence of actions done

  1. PageFactory initialized for e1 and e2.

  2. E1 used as Slider causes E2 to change a value in 'Text'.

  3. Retrieved E2 value for verification and sysout , E2 gets changed value.

conclusion : There is no need to call PageFactory.initElements() method again.

-1

No,it will not refresh ,this function is used to search element, you have to add codedriver.navigate().refresh(); by using this code you can refresh page

1
  • 1
    This doesn't answer the question. The OP is not asking how to refresh the page, they want to know if their reference will still be valid after the page has been refreshed.
    – c32hedge
    Commented Aug 17, 2017 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.