By adopting a test automation framework, enterprises can significantly increase the speed and accuracy of software testing, provide a higher return on investment (ROI), and systematically minimize risk.
This post will dive into the concepts of test automation frameworks and provide code samples for getting started today.
Contents
Infographic: Selenium Automation Framework
Before kicking off the deep dive, here’s an infographic providing a visual of several areas this post goes through.
Key benefits of adopting an automation framework
A framework defines the organization’s way of doing things. A test automation framework provides a standard for simplifying the automation effort. The end result is a reduction in software testing costs and maintenance costs.
- The ability to test large volumes of data on the system quickly.
- Scripting standards will be maintained across the framework in library creation, which includes business components, system communications, data checkpoints, loggers, reporters, and more.
- Engineering benefits including increased code re-usage, higher portability, reduced script maintenance cost, higher code readability, and more.
- Licensing costs are reduced through mature open-source solutions, such as Selenium Web Driver.
- Automated regression test suites that massively reduce manual effort while also increasing quality.
Introduction to the Selenium automation framework
There are mainly three types of frameworks created by Selenium WebDriver to automate manual test cases:
- Data-Driven Test Framework
- Keyword Driven Test Framework
- Hybrid Test Framework
In a data-driven framework, all of our test data is generated from some external files like Excel, CSV, XML, or some database table.
In the keyword-driven test framework, all the operations and instructions are written in some external files like Excel worksheets. According to the keywords written in an Excel file, the framework will perform the operation on UI.
Hybrid Test framework is a concept where one can take advantage of both keyword and data-driven framework. The Selenium framework inherits the essence of the Hybrid Test framework. This framework comprises of different components that provide the foundation for an automation framework for any web application
Selenium automation framework components
TestNG is a testing framework inspired from JUnit and NUnit but introduces new functionality that makes it more powerful and easier to use. TestNG test case is built using TestNG annotations. Each testing class can have multiple Test case, but test cases are distributed on the basis of the package on application modules
Test Data is driven through a testData Excel sheet. Each row of data is dedicated to one test case.
Generic libraries contain multiple Java files that support various features of the framework, like customized reporting, test data fetching, and web driver-specific methods. Any application-independent reusable method, which can be used for many projects such as Excel_Lib, Reporting_Lib, WebdriverCommonUtill, and Driver Java class can be classified under Generic Libraries.
For example, You may create a “Driver” class as shown below to create the choice of browser based on the requirement.
public class BDriver { public static WebDriver driver = new FirefoxDriver(); public static WebDriver IEDriver = new InternetExplorerDriver(); public static WebDriver cromeDriver = new ChromeDriver(); }
You can create an “Excel_Lib” class containing few reusable methods that can fetch test data from an Excel sheet.
Similarly, you can implement WebdriverCommonUtil class that contains web driver specific custom method that can be used for multiple applications such as clickAndWait(), waitForPageToLoad(), waitForElementPresent(), and more.
Selenium business components and POM model
Business components contain application-specific reusable methods that form the building block of our automation test scripts. Business components are broken down into several Java files. Based on the application module, each java file contains a business method specific to the Application.
Page Object Model (POM) is a design pattern to create an Object Repository for web UI elements that form an integral part of the framework.
Under this model, there should be one Page class for each web page. You can have Page class that will have to find WebElement methods of that web page (i.e. In Page factory WebElement are identified by @FindBy or @FindBys annotation) and also contains Page methods which perform operations on those WebElements(i.e. getters/setters methods in Page factory class to accesses page WebElements.
For example, let us consider a web page (such as a login) as one class and keep all the WebElements on a specific page.
public class BDriver { public static WebDriver driver = new FirefoxDriver(); public static WebDriver IEDriver = new InternetExplorerDriver(); public static WebDriver cromeDriver = new ChromeDriver(); } public class Login { @FindBy(name="username") private WebElement userName; @FindBy(name="pwd") private WebElement password; @FindBy(xpath="//input[@type='submit']") private WebElement loginBtn; public WebElement getUserName() { return userName; } public WebElement getPassword() { return password; } public WebElement getLoginBtn() { return loginBtn; } public void login(){ userName.sendKeys("admin"); password.sendKeys("manager"); loginBtn.click(); }
Once you create a Class, as shown above, you may use the WebElement in the actual test case as shown below:
public class SampleTest { @Test public void TimeTrak1Test(){ Login loginPage = PageFactory.initElements(Driver.driver, Login.class); loginPage.login(); } } } }
Advantages of the POM model:
- You can avoid duplicate object locators(for example object XPath) among the automated test suite
- During the development any Object ID changes, modification is required in page class and no need to make a change to automated scripts/test cases
- Also writing test cases will be easy and looks simple (just use getters to design test steps)
- Logging using Log4J API
For any web application automation, web events are helpful to view the events triggered by Web driver. Whenever you develop test scripts, you should write your own implementation for handling events during the execution.
When you run test cases, you may want to implement logging to see when the test started executing, when the test failed and what is the message/warning it has provided.
Log4j is built with a component that works together to enable logging of messages according to the message type and level, and to control at runtime how these messages are formatted and where they are reported.
Controller framework – TestNG XML
The TestNG XML file is the brain of our automation framework, which takes care of executing the testing classes as specified in the XML file. With this XML file, you may plan and control of the automated test cases execution:
- Declare environment variable (global variable)
- Prioritize and execute test cases in order
- Execute batch execution and grouping parallel execution
- Also, you can skip a particular method or particular test in the test suite
This framework is driven through the TestNg.xml file, which has details of the test suite. A test suite contains a bunch of TestNG classes and each tesNg class can have multiple test cases. For example, a sample of testng.xml is shown here:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Automated Test suite" > <test name="Regression Test suite"> <classes> <class name="TestClassName1" /> <class name="TestClassName2" /> <class name="TestClassName3" /> … </classes> </test> </suite>
Automation build environment
We utilize ANT as a build tool for automation test scripts written in Java.
Apache Ant is a Java-based Open Source tool from Apache. It can be downloaded and installed on any system.
Once the ANT tool is set up properly, you can execute the command below to start the automated test suite from the project directory and generate a Test NG report.
ant testng-execution
Selenium test automation reporting
Any automation without good reporting is of no use. When you integrate Selenium with TestNG, you will get some default reports by TestNG which is very useful, but still, we can generate custom reports via XSLT Reporter.
To generate an XSLT Report in Selenium, below are the preconditions:
- ANT should be installed.
- At least some test cases should be executed by TestNG (i.e. -output directory should be available in Project home directory).
You may download and install XSLT on the Project home directory, one can execute the below command to generate the XSLT report after proper environment setup.
ant testng-xslt-report
Conclusion
This post has covered a lot of ground, from business justifications for test automation all the way down to real-world applications and samples to get your teams started. If you have any tips or questions, let us know in the comments below!
Special thanks to Thiru Swaminathan and Veeresh Math for this contribution.