being good at waiting - using selenium to test ajax-intensive pages

10
Being good at waiting Alexander Tarnowski Using Selenium to test Ajax-intensive pages Selenium Conference, April 2012

Upload: alexander-tarnowski

Post on 09-May-2015

3.823 views

Category:

Documents


0 download

TRANSCRIPT

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

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

2

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

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

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

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

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

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

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

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

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