Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Being good at waitingUsing Selenium to test Ajax-intensive pages            Selenium Conference, April 2012       Alexande...
2
The DOM and asynchronicity – Challenges Existing elements may change values New elements may appear Not all Ajax calls hav...
The naive approach doesn’t work It’s ugly Different loads on the target host make setting the correct interval difficult T...
Home-made waittimeToWait = 10sloop while timeToWait > 0   if element is found then exit loop   sleep   decrease timeToWait...
Implicit waitsinterface Timeouts {    Timeouts implicitlyWait(long time, TimeUnit unit);    Timeouts setScriptTimeout(long...
Finders and timeoutfindElement()  Throws NoSuchElementException  Respects implicit waitsfindElements() Returns an empty co...
WebDriverWaitnew WebDriverWait(webDriver, 5).until(new ExpectedCondition<WebElement>() {    public WebElement apply(WebDri...
Zero side effect validation                 validate(invalid)                     “invalid”   C                           ...
How to wait – Conclusion  Method                           When  Sleep                            Never  Home-made sleep  ...
Upcoming SlideShare
Loading in …5
×

Being good at waiting - Using Selenium to test Ajax-intensive pages

5,354 views

Published on

  • Be the first to comment

  • Be the first to like this

Being good at waiting - Using Selenium to test Ajax-intensive pages

  1. 1. Being good at waitingUsing Selenium to test Ajax-intensive pages Selenium Conference, April 2012 Alexander Tarnowski
  2. 2. 2
  3. 3. The DOM and asynchronicity – Challenges Existing elements may change values New elements may appear Not all Ajax calls have observable side effects 3
  4. 4. The naive approach doesn’t work It’s ugly Different loads on the target host make setting the correct interval difficult Time lost! Imagine 1000+ tests where each test spends 2 seconds sleeping... 4
  5. 5. Home-made waittimeToWait = 10sloop while timeToWait > 0 if element is found then exit loop sleep decrease timeToWaitend loop 5
  6. 6. Implicit waitsinterface Timeouts { Timeouts implicitlyWait(long time, TimeUnit unit); Timeouts setScriptTimeout(long time, TimeUnit unit); Timeouts pageLoadTimeout(long time, TimeUnit unit);} 6
  7. 7. Finders and timeoutfindElement() Throws NoSuchElementException Respects implicit waitsfindElements() Returns an empty collection Respects implicit waits 7
  8. 8. WebDriverWaitnew WebDriverWait(webDriver, 5).until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver webDriver) { return webDriver.findElement(By.id("some id")); }}); Parameters: timeout, polling interval, ignored exceptions, error message Ignores exceptions by default 8
  9. 9. Zero side effect validation validate(invalid) “invalid” C SDOML changes E I R validate(valid) E V N “valid” E T R Nothing 9
  10. 10. How to wait – Conclusion Method When Sleep Never Home-made sleep Once – and remove it Implicit waits When you really, really don’t care about Ajax WebDriverWait Whenever you can Poll an ”Ajax status” variable When there are no observable side effects 10

×