3

We currently have a big selenium junit project which uses the page object model. We make use of the @FindBy annotation to declare our WebElements. Now when we run the tests, we randomly get the NoSuchelementException, which means the page might not have finished loading. We don't want to use an implicit wait because that won't provide a complete solution (an element might be present in the DOM but not interactable yet). An explicit wait might solve this problem. However, how do we use it in this page model architecture without having to take away the @FindBy annotation? This might have been asked before, but I don't seem to find any solutions yet.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Stephen Nsoh
  • 41
  • 1
  • 3

1 Answers1

1

There's a 4 ways I've see you can do this. None of them are fun, although, they get better as you go along (and more complicated to program)

  1. Copy your selector into an explicit wait

  2. Put your @FindBy on a List<WebElement> and wait until the size is not 0.

  3. Create a function that accepts a WebElement and calls isDisplayed() (or some other non-action function) until it doesn't throw an exception

  4. A final solution you could implement is to use the Decorator pattern around WebElement. This means that you will need to create your own FieldDecorator, and then use that decorator when you initialize your decorated WebElements. The decorated webelement would have a exists() function that would basically be a try/catch around NoSuchElementExceptions.

If you are confused about any of these solutions, I can provide code for them, but I think it really good practice to learn how WebElementWait and Page Objects really work (by implementing your chosen solution), so I won't post it now.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • #2 and #3 seems like an infinite loop bug waiting to happen. – robx Sep 28 '16 at 00:09
  • @robx in both cases I would have a timeout and throw and exception – Nathan Merrill Sep 28 '16 at 01:42
  • Ok, anyway, I realize this is an old question. Perhaps you should state your solutions a bit more obvious. What was given as an answer is a vague bug waiting to happen. Considerations for future answers. – robx Sep 28 '16 at 07:01