3 weeks stint with Selenium WebDriver 2.0
by Eing Ong
1. Initial page loading
Typically the first page takes a longer time to load depending on how your site is designed.
What we want to solve for
- variable delays in different environments
- detects that page is loaded in a minimal time delay
- overall timeout if page doesn't get loaded
The code :
// this dictates the overall maximum timeout if page doesn't load.
int count = WAIT_LOOPCOUNT;
// this detects if page gets loaded
while ( (count > 0) && driver.exists(elementLoadingPage) )
{
count--;
// MAXWAIT - 3 seconds is rather optimal
Thread.sleep(MAXWAIT);
}
2. Helpful naming conventions
One thing you'll be doing a lot is to create Strings to associate with elements, properties etc. Prefix your variables with the type will help you differentiate faster.
E.g.
public static final String elementTwitterButton = "twitter_button"; // Twitter element
public static final String propertyTextSize = "font_size"; // Property of element
public static final String frameSocial = "socialFrame" // Frame for social elements
3. Create a base class
As with any tests you write, gather all the common code such as login, getting window handles etc into your base class so that no code is duplicated in all tests.
Login should only be tested by login tests but all other tests should try to reuse the browser that is already logged-in if you want faster performance in your test execution. Design your tests to clean up after each test whether tests pass or fail in your tear down method.
4. Some tips
On switching between frames
// switch to frame
driver.switchTo().frame(frameSocial)
// ... test in frame ...
// switch to main window
driver.switchTo(frameMain)
On XPath - if all matching fails (no id/class/classname/linkText/name etc), don't get blocked - use XPath.
Use Firebug to see XPath of element.
To understand XPath : http://www.w3schools.com/xpath/xpath_examples.asp
5. Run tests in Hudson - headless or not
This is the best thing QA should spend time doing as this watches the build for you while you get to work on funner stuff.
Subscribe via RSS