You are on page 1of 9

How to build a good Automation

Test framework?

From Next Generation Automation Academy

Step 1: SELECT THE TOOL FIRST…

Commercial tool vendors are fiercely stomping on each other to get a piece of the test tool pie. But so
far, no one has outshone Selenium in terms of popularity and adoption.

The biggest sweet spot of Selenium is the fact that it is OPEN SOURCE, in other words, it is completely
free to download and use. Selenium provides an API called WebDriver which enables testers to craft
their tests in many programming languages, including Java, C#, Python, etc. Besides web browsers, you
can also automate mobile devices like Android, and iOS via Appium.
Step 2: CHOOSE A PROGRAMING LANGUAGE

From an architectural perspective, the very first question you should ask is: in what form do I want to
write my tests? There are several competing choices: Keyword Driven, BDD, code, etc. Try to find
answers to below questions before you proceed further.

What language does your company/client currently use for their software development? If they work
with Java to develop software, then maybe that’s the language you should start with.

Who will use the framework to write the tests? Are they proficient in the chosen programming
language?

In my personal opinion, Java is a good choice because it is widely adopted and cross-platform. We can
easily find code examples, or troubleshooting tips when you’re stuck. It’s also the top priority for each
release version of Selenium.

Step 3: CHOOSE A UNIT TEST FRAMEWORK

Next, we need to select the unit test framework that we will base it on. A unit test framework helps us
to:

 Mark a class or a method as part of the test using annotations (e.g. @Test)
 Perform the assertion/verification
 Execute test cases from IDE, command line, CI/CD, etc.
 Generate logs
 Produce XML/HTML reports of the test execution (test results)
 Group and prioritize test cases
 Execute tests in parallel

In this case, I have selected TestNG unit test framework as TestNG eliminates most of the limitations of
the older framework and gives developers the ability to write more flexible and powerful tests. Some of
the highlight features are- easy annotations, grouping, sequencing and parameterizing.
Register Now
Step 4: BUILD THE “SELENIUM CORE” COMPONENT

 “Selenium Core” will control/manage the browser instances as well as element interactions.
 This core helps you to create, reuse, and destroy WebDriver objects. in this framework, we will use
the Factory design pattern in creating WebDriver objects.
 One WebDriver object, as its name suggests, “drives” a browser instance – moving from web page
to web page.
 The test writers should not care how the browser instances are created. They just need a WebDriver
object to execute a given test step in your test flow.
 In the above diagram, you can observe that DriverManager is an abstract class dictating that its
implementations must provide methods such
as createWebDriver(), getWebDriver() and quitWebDriver().
 To easily manage the browsers that our project focuses on, we define an enum called DriverType
 DriverManagerFactory is the “factory” that manufactures DriverManager objects for you. You invoke
the getDriverManager() method of this class with your DriverType to receive a DriverManager-type
object. Since DriverManager is an abstract class, you won’t receive an actual DriverManager, just
one of its implementations, such as ChromeDriverManager or FirefoxDriverManager.

In below example you can see, the test writer doesn’t care whether the WebDriver for Chrome is
called ChromeDriver. They only specify the browser type using one of the values inside the enum. This is
a simple test for visiting Google.com and verifying the page title if the user is actually
visiting Google.com
Step 5: BUILD THE “SELENIUM TEST” COMPONENT

This component contains all test cases that use the utilities provided by the “Selenium Core” we saw
earlier. The design pattern we’ll apply here is called Page Object Model (POM).

Applying POM means you organize the UI elements into pages.

A page can also include “actions” or “business flows” that user can perform on the page. For instance, if
your web app includes several pages called the Login page, Home page, Register page, etc., you’ll create
corresponding Page objects such as LoginPage, HomePage, RegisterPage, etc.

Thanks to this smart structure, if the UI of any page changes, we don’t need to update any tests. We just
need to refactor the code of the page objects (only at one place)

Step 6: A SIMPLE PAGE OBJECT

Let’s zoom into a specific Page object. In the below example, we see that the LoginPage contains several
important pieces of information:

 A constructor that receives a WebDriver object and sets its internal WebDriver to that object.
 The selectors that help the WebDriver object find the web elements you want to interact with.
E.g. userNameTextBox
 Methods to perform on the Login page such as setUserName(), setPassword(), clickLogin(), and
most importantly–login() method that combines all of the three methods above.
HOW TO USE A PAGE OBJECT – To use this LoginPage page in your test, you can simply create a new
LoginPage object and call on its methods. Since you abstract the web element definitions (selectors)
from the test writer, he/she doesn’t need to know how to find the userNameTextBox. He/she just calls
the login() method and passes in the username/password. If the web element definitions (selectors)
happen to change, you don’t need to update all of the tests.
Step 7: CHOOSE A REPORTING MECHANISM

 There are a lot of options available out there for logging your automated tests. Reporting that is
provided by testing frameworks such as Junit, TestNG are often generated in XML format, which can
easily be consumed by other software like CI/CD servers (Jenkins) but it’s not human-readable.
 Third party libraries such as ExtentReport or Allure can create test result reports which are readable
for humans. They also include pie charts and screenshots. There are some other open source
reporting libraries such as ReportNG for Java – a simple HTML reporting plug-in for the TestNG unit-
testing framework. ReportNG provides a simple, color-coded view of the test results. Additionally,
setting up ReportNG is very easy.

Step 8: DECIDE HOW TO BUILD, VERSION CONTROL AND IMPLEMENT CI/CD


Build tools and dependency managers: Dependency managers help manage the dependencies and
libraries that the framework is using. Examples of these tools include Maven, Gradle, Ant, NPM, and
NuGet. Build tools assist in building the source code and dependent libraries, as well as in running tests.
For example, in the below image, we used Maven to execute our tests (mvn clean test)
Version Control: All automation teams must collaborate and share source code with each other. Just
like a software development project, source code of the tests and test utilities are stored in a source
control system also known as version control system. Popular source control systems are GitHub,
Bitbucket and TFS.

CI/CD integration: Popular CI systems include Jenkins, Bamboo, TFS, to name a few.

You might also like