Test Automation Best Practices

In my first post, I’d like to talk about a more general subject rather than a particular problem and its solution. Let’s say we want to build a test automation framework. For the sake of following the tradition, we completed our market research, and come to the conclusion of developing a project based on selenium will be our company’s best interest (what a surprise!). So where do we start? What will be our main focus when writing codes?

I will talk about these details separately on another post, but for now I’ll summarize the topic in 6 quotes:

1- Browser synchronization

You should never use Thread.sleep() unless you really have to. Instead, there are FluentWait(), ImplicitlyWait() or ExplicitlyWait() methods that selenium offers.

2- Do not try to perform UI tests with Selenium

Unfortunately UI testing is not a big part of the test automation area and selenium is quite ineffective on this. Instead of trying to use Selenium for this, there are specific frameworks you can benefit that focus on UI testing.

3- Write simplest code blocks on test layer

You designed your architecture, created layers and started coding, and now you need to develop your layers starting from the simple to the advanced. Your test layer (top layer) should be written in the simplest way that anyone can understand while reading. Keep in mind that you will need your teammates on QA being able to run and debug the tests, whenever they have to be run or debugged. Since most of the time QA members are not developers, we need to make their lives easier (and ours in advance).

Sub layers can be more technical of course, but remember: Simple is better. So don’t make anything complex when it can be simple. But also don’t forget that simple is difficult, writing simple codes require experience.

4- Errors vs. Failures

Code error is an unwanted situation on test projects. These false negatives may cause the teams on company losing their trust to the project. You can call them bugs of your test project. You can minimize these code errors by writing quality codes with software development standards (oop, clean code, solid, dry, design patterns etc.) and by continuous refactoring. Continuous refactoring is a must on projects that are being developed with scrum since the website or app changes on every sprint. Otherwise, at some point you cannot handle the test maintenance. There are lots of projects that fail after some time and the reason is this uncontrolled growing. Without refactoring, expanding a project with technical debts can make the managers happy because of the rapidly increasing test counts. But added value will be less and less in time.

It’s not just bad coding that causes errors, there could be many variables as well: Test environment, parallel testing problems, test infrastructure, test data corruption etc. You can reduce these effects by setting up a rule that reruns failed tests (rerunFailingTestsCount). But they still need to be fixed for a stable project.

5- No need to execute tests for all browsers and OSs

Test automation does not focus on cross browser testing, even though the consulting companies say otherwise — and with large fonts for marketing purposes! Specifically, with IE and Safari, your tests will be very unreliable and unsecured. Unless you have a team of 10 people dedicated for test automation, you should focus on 1 browser and 1 version, preferably the latest version that selenium supports (at the moment chrome 52 for chromedriver and firefox 47 for firefoxdriver, firefoxdriver will be deprecated soon and gecko driver will be on the table but that’s a subject for another post :)

6- Start with the simplest smoke tests

There is no point in starting with big scenarios when you start a new project. It would have a negative impact on the progress. Instead, when you start with smoke tests with testing just one little functionality, your project will be on solid ground. Once you finish coding enough smoke tests, your framework layer will be tough enough to handle more complex scenarios. This way your project will grow properly and also you will get fast results from day 1.

P.S: You can find further discussions from Joe Colantonio’s podcasts on this subject. Also, there is an awesome book by Dima Kovalenko called Selenium Best Practices & Design Patterns.

Originally published at Selenium Notes.