Test Automation Frameworks: How to Build a Framework in Selenium?

Last updated on Sep 11,2024 7.3K Views

Test Automation Frameworks: How to Build a Framework in Selenium?

edureka.co

Every business runs on software and all the software needs to be tested before it is released to users. Automation software testing is vital to release efficient, effective and reliable software at a rapid pace. For that, you need automation frameworks. Automation frameworks allow testers with less experience to manage sophisticated testing on shorter development cycles. In this Test Automation Frameworks article, let’s explore more about the concept of testing frameworks. You can get a better understanding from the Automation Testing Courses.

You can watch this Automation Testing video lecture, in which our Software Testing Training expert discusses every nitty-gritty of the technology.

Test Automation Using Python | Selenium Webdriver Tutorial With Python

This video will teach you the basics of Selenium with Python.

What is a Test Automation Framework?

A test automation framework is a comprehensive set of guidelines and rules created to make test automation efficient and effective. A framework is comprised of a combination of test tools and practices that are designed to help QA specialist test more efficiently.

Test Automation Frameworks are an essential part of any successful automated testing process. A framework allows testers with less experience to manage sophisticated testing on shorter development cycles. Frameworks incorporate components such as:

Benefits of a Test Automation Framework

Consider a simple scenario. Let’s say you are working in a testing team comprising of 5 members. Now, if each one of you uses your own technique to write the automation script, it will lead to a divergent style of coding. Moreover, the entire team may not be able to leverage common pieces of code and scripts. This is where test automation frameworks come into the picture. 

So, with an automation framework you can:

Use of an automation framework in any project leads to the following advantages:

So, technically test automation frameworks, when designed and implemented correctly, deliver frequent and stable automated test code. There are different types of frameworks. Now lets’ look at what each framework is built to accomplish, along with their pros and cons.

Find out our Automation Testing Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUK
HyderabadUSA
PuneCanada
ChennaiSingapore

Different types of Test Automation Frameworks

There are several types of test automation frameworks that you can use for different types of testing scenarios. It’s important to choose the framework that is right for you. The 6 primary test automation frameworks we’ll be exploring are: 

Linear Automation Framework

It is a basic level test automation framework which is in the form of ‘Record and Playback’ in a linear fashion. In this type of framework, creation and execution of test script are done individually for each test case individually. The tester records each step such as navigation, user input, or checkpoints, and then plays the script to automatically conduct the test from the beginning in sequential order. This type of framework is mostly used to test small sized applications.

Advantages

Disadvantages

Data-Driven Framework

While automating or testing any application, at times you might to test the same functionality multiple times with the different set of input data. In such cases, we can’t embed test data in the test script itself. Data Driven Testing Framework helps the user segregate the test script logic and the test data from each other. The test data is kept in the external storages resources such as MS Excel Sheets, MS Access Tables, SQL Database, XML files etc.

Advantages

Disadvantages

Keyword-Driven Framework

This framework is an extension of the data-driven testing framework in some ways. It not only segregates the test data from the scripts, but it also keeps a certain set of code belonging to the test script into an external data file. Here we use a table format to define keywords or action words for each method that we would execute. Once the table has been set up, all you have to do is write the code that will trigger the necessary action based on the keywords.

Advantages

Disadvantages

Modular Testing Framework

This framework is based on one of the popularly known OOPs concept – Abstraction. The testers divide the entire application into a number of logical and isolated modules. After that, a test script is created for each part and they are combined to build larger tests in a hierarchical fashion.

Advantages

Disadvantages

Library Architecture Testing Framework

This framework is slightly based on the modular framework, except has some extra benefits. Instead of dividing the application into test scripts, we divide the application into common functions that can be used by the other parts of the application as well. These functions are then kept in a library which can be referred to by the test scripts whenever needed.

Advantages

Disadvantages

Hybrid Testing Framework

As the name suggests, the Hybrid Testing Framework is a combination of more than one above mentioned frameworks. The best thing about such a setup is that it leverages the benefits of all kinds of associated frameworks. The best thing about this framework is it can be customized to your specific needs and requirements. You can pick and choose the capabilities of each framework that you want that suits your requirements.

So, these are some most widely used test automation frameworks. How a company chooses among them is ultimately determined by its requirement and testing team’s expertise to the right automation tools. Now let’s see how to build a simple data-driven framework using selenium.

 

Building a Data-Driven Framework using Selenium

Demand for web development and software testing is huge. As a result, a lot of tools are being released into the market and competition is much stronger than ever. But so far, no one has outshone Selenium in terms of popularity and use. The biggest sweet spot of Selenium is the fact that it is completely free to download and use. Testers can craft their tests in many programming languages, including Java, C#, Python, etc.

Now before we get started with the demo let’s check out the steps that you follow to generate a framework using Selenium:

Step 1: The first step is to select a programming language. In the Selenium world, you have a wide range of programming languages to choose from such as Java, C#, Ruby, Python, etc. In my opinion, Java is a good choice because it is widely adopted and cross-platform.

Step 2: Next you choose a unit test framework. It helps to mark a method as part of the test using annotations, generate logs, execute test cases from IDE, executes tests in parallel. For example, you could use TestNG as it offers a lot of benefits.

Step 3: Next design the framework architecture according to your needs. Make sure you come with a come up with a sustainable, maintainable, and scalable architecture. Now based on the architecture start building your test modules.

Step 4: Next step is to choose a recording mechanism. With a proper recording mechanism reading and analyzing test results will be easier.

Step 5: Lastly, if needed you can integrate your framework with other tools. For example, TestRail is a test case management (TCM) system that can be used when your project has a large number of tests and related work items such as bugs. Similarly, Jira integration.

Let’s get started with the demo. So, in this demo, I have two files:

You can get a better understanding with the Software testing course certification.

Take a look at the below code to understand the test case. The code is written for testing the login functionality of a flight booking application.

package DataDriven;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DDTExcel
{
 ChromeDriver driver;
 
 @Test(dataProvider="testdata")
 public void DemoProject(String username, String password) throws InterruptedException
 {
 System.setProperty("webdriver.chrome.driver", "C:UsersVardhanDownloadschromedriver.exe");
 driver = new ChromeDriver();
 driver.get("<a href="http://newtours.demoaut.com/" data-mce-href="http://newtours.demoaut.com/">http://newtours.demoaut.com/</a>");
 
 driver.findElement(By.name("userName")).sendKeys(username);
 driver.findElement(By.name("password")).sendKeys(password);
 driver.findElement(By.name("login")).click();
 Thread.sleep(5000);
 Assert.assertTrue(driver.getTitle().matches("Find a Flight: Mercury Tours:"), "Invalid credentials");
 System.out.println("Login successful");
 }

@AfterMethod
void ProgramTermination()
 {
 driver.quit();
 }

@DataProvider(name="testdata")
public Object[][] TestDataFeed()
 {
 ReadExcelFile config = new ReadExcelFile("C:UsersVardhanworkspaceSeleniumLoginCredentials.xlsx");
 int rows = config.getRowCount(0);
 Object[][] credentials = new Object[rows][2];

 for(int i=0;i<rows;i++)
 {
 credentials[i][0] = config.getData(0, i, 0);
 credentials[i][1] = config.getData(0, i, 1); 
 }
 return credentials;
 }

}

In the above code, there is a method called  “TestDataFeed()”. In this method, I have created an object instance of another class named “ReadExcelFile”. I have fed the path of my excel file containing the data. Then I defined a ‘for’ loop to retrieve the text from the excel workbook.  For the demo, I have created an excel file called “LoginCredentials” in which the usernames and passwords have been stored in different columns.

Moving on to our next file, “ReadExcelFile”.

<pre>package DataDriven;

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile
{
 XSSFWorkbook wb;
 XSSFSheet sheet;

 public ReadExcelFile(String excelPath)
 {
 try
 {
 File src = new File(excelPath);
 FileInputStream fis = new FileInputStream(src);
 wb = new XSSFWorkbook(fis);
 }
 catch(Exception e)
 {
 System.out.println(e.getMessage());
 }
}
 public String getData(int sheetnumber, int row, int column)
 {
 sheet = wb.getSheetAt(sheetnumber);
 String data = sheet.getRow(row).getCell(column).getStringCellValue();
 return data;
 }
 public int getRowCount(int sheetIndex)
 {
 int row = wb.getSheetAt(sheetIndex).getLastRowNum();
 row = row + 1;
 return row;
 }
 }

Like I mentioned earlier, the file had code to read data from excel file. There are two main functions of importance here. “getRowCount” which calculates the number of rows of data present in my excel sheet. For more information, you can refer to the Automation Testing Training in Austin.

“String getData” which extracts the data from the excel sheet for every iteration of testing code. To understand this framework better, you can refer to the below video, where I have explained this in a structured manner.

Test Automation Framework Using Selenium | Software Testing Training

This video will help you learn what testing frameworks are and how to build one using Selenium.

With this, we have reached the end of the blog. Hope the things that you have learned here today will help you as you head out on your automation journey. If you want to dive deeper in Automation Testing, We at Edureka also offer a masters program in Automation engineer course.

If you found this “Test Automation Frameworks” article relevant, check out the live-online Selenium Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

Got a question for us? Please mention it in the comments section of this ‘Test Automation Frameworks’ article and we will get back to you. Alternatively, check out the Automation Testing Training in Bangalore!

 

 

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 4th January,2025

4th January

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 13th January,2025

13th January

MON-FRI (Weekday Batch)
View Details
BROWSE COURSES