Automation Best Practices

Automation Best Practices

Page Objects
Can simply be thought of as a collection of methods representing the services offered on a page, thus allowing us to abstract the details and mechanics of a page. The tests then use the methods of the page object class whenever they need to interact with the page UI.

The Page Object Pattern provides the following advantages:

If the UI changes for a page, the tests themselves don’t need to change, only the code within the page object needs to change.
There is clean separation between the test code and the page object code.
The Page Object serves as a single repository for the services or operations offered by the page.

Loadable Components
Are a method for making the work of creating Page Objects easier. This is done by creating a standard way of ensuring that pages are loaded and providing hooks to make debugging the failure of a page to load easier.

The Loadable Component Pattern provides the following advantages:

Uses the “load” method that is used to navigate to the page and the “isLoaded” method which is used to determine if we are on the right page.
The Loadable Component Pattern also allows you to model your page objects as a tree of nested components.

Page Factory
Is a factory class that is built into WebDriver’s support library in order to support the Page Object Pattern.

Use Robust and Portable Selectors
The preferred selector order is: 1. Id, 2. Name, 3. Css, 4. Xpath

In actual practice you will discover Id and name are the easiest and surest selectors to use.
Xpath is often very brittle.
css works well in conjunction with id and name.

Avoid Thread sleep if at all possible
Instead of Thread sleep try to use FluentWait or Expected Condition

FluentWait
public void clickButton() {
driver.findElement(By.id("qButton")).click();
SeleniumUtil.fluentWait(By.name("win1"), driver());
}

Expected Condition
(new WebDriverWait(driver, 30)).until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("Code Example");
},

Use relative URL’s in the Loadable Components
Avoid hard coding hosts

Incorrect
driver().get("http://nl.wikipedia.org/wiki/Wiki");

Pass the relative URL’s
openPartialUrl(“/”);

That use an abstract method like
public void gotoUrl(String uri) {
String baseUrl = testParams.getProperty("base_url");
baseUrl = baseUrl.replaceAll("\\.com/.*\\.do", ".com/");
baseUrl = StringUtils.remove(baseUrl, "\n");
baseUrl = StringUtils.trim(baseUrl);
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
driver.get(baseUrl + uri);
}

Don’t rely on a specific Driver Implementation
When you run your integration tests on a continuous build box you will actually receive a RemoteDriver.

You should then rely on a setup such as Selenium Grid Sauce Labs or a framework like TestNG to run your tests in parallel against multiple browser types.

Encapsulate Your Tests
All automation tests your write should be independent of each other.

Common encapsulation techniques:

Tests never rely on another test to set data, move to a location or initiate something.
In a framework like JUnit if you nest multiple tests inside a single test, the tests don’t always run in order from test run to test run.
After each test logout and then have the next test confirm the logout and then login.
Don’t rely on moving back to a starting point like “Home” via navigating elements, instead move back to a starting point by passing a relative URL.

Internet Explorer Tips
The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose “Internet Options…” from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled “Enable Protected Mode”.

“Data”, it’s the key! Having a plan for managing your test data is one of the main keys to a successful automation infrastructure. To be discussed in a future post.

Posted in automation, best practices

Download and extend ATF...

ATF Is Now Open Source

Join this 10 week program anytime...

DevOps Mastery Program

Get your DevOps health check now...

Free DevOps Assessment

X