Selenium performance tips

1 min


626
2 comments, 1 share, 626 points
Selenium performance tips

Selenium performance tips

Couple of days ago we had an article : How to get selenium to wait for ajax response , but I feel here is still a lot to discuss about ajax and performance .

Ajax typical flow:

ajax typical flow

You always need to wait for ….

  • Page loading
  • Element appereance
  • Element visibility
  • Element disappearance
  • Alert or popup dialog
  • New window
  • Text changes
  • Element style changes

WebDriver waits for page loading

  • By default page is loaded synchronously
  • No need to use waitForPageToLoad

BUT

  • Waiting is broken for long polling and some other techniques
  •  http://code.google.com/p/selenium/issues/detail?id=687#c4

You can avoid waiting manually

new HtmlUnitDriver(){
@Override
protected WebClient modifyWebClient(WebClient client){
WebClient noWaitClient = super.modifyWebClient(client);
noWaitClient.setRefreshHandler(new WaitingRefreshHandler());
return noWaitClient;
}
};
HtmlUnit
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference(“web driver.load.strategy”, “unstable”);
Firefox

WebDriver works with DOM

  • webdriver works with domEverything is dynamic
  • Complex unreliable structure
  • JavaScript libraries
  •  Third-party widgets
  • Rules are broken sometimes

 

 

 

 

 

DOM element workflow

dom element workflow

WebDriver is clever enough

driver.manage().timeouts().implicitlyWait(10, TimeUnit.seconds);
webdriver implicit wait

 

  • Waiting on browser side
  • Can’t stop earlier
  • findElements wait for at least one element
  •  May become hidden cause of long tests
  •  Doesn’t work for element presence check
  • Works for all findXXX methods transparently

Manual wait is available

private void waitForSuggestions(){
new WebDriverWait(driver, 30).until(new Predicate<WebDriver>() {
public boolean apply(WebDriver webDriver){
By selector = by.cssSelector(“selector”);
return webDriver.findElement(selector).isDisplayed();
}
});
}
manual wait

Lots of ready to use wait conditions

WebDriverWait wait = new WebDriverWait(driver, 30);
				wait.until(presenceOfElementLocated(By.id("foo")));
				wait.until(titileIs("titile"));
				wait.until(titile.contain("text"));
				wait.until(textToBepresentInElement(By.id("foo")));
				wait.until(frameToBeAvailableAndSwitchToit(By.name("framename")));
				wait.until(elementToBeClickable(By.id("foo")));
				
waits

Fluent waits for tuned configurations

//waiting 30 seconds for an element to be present on the page,
// check for its presence once every 3 seconds.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
             .withTimeout(30, TimeUnit.SECONDS)
              .poolingevery(3, TimeUnit.SECONDS)
              .ignoring(NoSuchElementExeption.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>(){
public WebElement apply(WebDriver driver){

return driver.findElement(By.id(“foo”);
}

});
fluent wait

Mouse operations

  • You can click on any visible element
  • OS events are emulated
  • DOM events are processed as always
  • Click is performed in the center of element area
  • Autoscrolling is performed for click but is not reliable

This are just couple of the Selenium performance tips , but we are going to discuss more in some further articles.

We will be covering :

  • Keyboard operation
  • Speed up type big amount of text
  • Actions for experts 🙂 and more …

Happy testing!


Like it? Share with your friends!

626
2 comments, 1 share, 626 points

What's Your Reaction?

cute cute
0
cute
scary scary
1
scary
wtf wtf
0
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

2 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story