being good at waiting - using selenium to test ajax-intensive pages
TRANSCRIPT
![Page 1: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/1.jpg)
Being good at waiting
Alexander Tarnowski
Using Selenium to test Ajax-intensive pages
Selenium Conference, April 2012
![Page 2: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/2.jpg)
2
![Page 3: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/3.jpg)
3
The DOM and asynchronicity – Challenges
Existing elements may change valuesNew elements may appear Not all Ajax calls have observable side effects
![Page 4: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/4.jpg)
4
The naive approach doesn’t work
It’s uglyDifferent loads on the target host make setting the correct interval difficult Time lost! Imagine 1000+ tests where each test spends 2 seconds sleeping...
![Page 5: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/5.jpg)
5
Home-made wait
timeToWait = 10sloop while timeToWait > 0
if element is found then exit loopsleep decrease timeToWait
end loop
![Page 6: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/6.jpg)
6
Implicit waits
interface Timeouts { Timeouts implicitlyWait(long time, TimeUnit unit); Timeouts setScriptTimeout(long time, TimeUnit unit); Timeouts pageLoadTimeout(long time, TimeUnit unit);}
![Page 7: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/7.jpg)
7
Finders and timeout
findElement() Throws NoSuchElementExceptionRespects implicit waits
findElements()Returns an empty collectionRespects implicit waits
![Page 8: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/8.jpg)
8
WebDriverWait
new 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 messageIgnores exceptions by default
![Page 9: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/9.jpg)
9
Zero side effect validation
validate(invalid)
“invalid”
validate(valid)
“valid”
SERVER
CLIENT
DOM changes
Nothing
![Page 10: Being good at waiting - Using Selenium to test Ajax-intensive pages](https://reader036.vdocument.in/reader036/viewer/2022082807/554ddf4db4c905d10e8b54a9/html5/thumbnails/10.jpg)
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