Selenium Framework is a code structure that helps to make code maintenance easy. Without frameworks, we will place the ?code? as well as ?data? in the same place which is neither re-usable nor readable. Using Frameworks, produce beneficial outcomes like increased code re-usage, higher portability, reduced script maintenance cost, higher code readability, etc.
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
Three types of framework
>> Data-Driven Test Framework:
In the data-driven framework, all of our test data is generated from some external files like Excel, CSV, XML or some database table.
To read or write an Excel, Apache provides a very famous library POI. This library is capable enough to read and write both XLS and XLSX file format of Excel.
Data Driven Framework with Apache POI ? Excel
We need a way to open the Excel sheet and read data from it within our Selenium test script. For this purpose, use the Apache POI library, which allows you to read, create and edit Microsoft Office-documents using Java. The classes and methods we are going to use to read data from Excel sheet are located in the org.apache.poi.hssf.usermodel package.
How to do it?
1) Download JAR files of Apache POI and Add Jars to your project library. You can download it from here. That?s all about configuration of Apache POI with eclipse. Now you are ready to write your test.
2) Create a ?New Package? file and name it as ?testData?, by right click on the Project and select New > Package. Place all of your test data in this folder (package) whether it is a sql file, excel file or anything.
3) Place a Excel file in the above created package location and save it asTestData.xlsx. Fill the data in the excel like below image:
4) Add two constant variables (testData package path & Excel file name) in theConstant class.
5) Create a ?New Class? file, by right click on the ?utility? Package and select New> Class and name it as ?ExcelUtils?. First we will write basic read/write methods.
6) Once we are done with writing Excel functions we can go ahead and modify the SignIn_Action module to accept the test data from excel file.
7) Create a ?New Class? and name it as Apache_POI_TC by right click on the ?automationFramework? Package and select New > Class. In this we will read the values from the Excel sheet to use them as the test data and write the test result in the Excel. Give it a run, see how beautify your script will execute the code.
8) Once it finished open the Excel file and check for the result.
>> Keyword Driven Test Framework:
In the keyword-driven test framework, all the operations and instructions are Keyword Driven Framework is a type of Functional Automation Testing Framework which is also known as Table-Driven testing or Action Word based testing. The basic working of the Keyword Driven Framework is to divide the Test Case into four different parts. First is called as Test Step, second is Object of Test Step, third is Action on Test Object and fourth is Data for Test Object.
The above categorization can be done and maintained with the help of Excel spreadsheet:
Test Step: It is a very small description of the Test Step or the description of the Action going to perform on Test Object.Test Object: It is the name of the Web Page object/element, like Username & Password.Action: It is the name of the action, which is going to perform on any Object such as click, open browser, input etc.Test Data: Data can be any value which is needed by the Object to perform any action, like Username value for Username field.
>> Hybrid Test Framework
Hybrid Test framework is a concept where we are using the advantage of both Keyword and Data-driven framework.
Here for keywords, we will use Excel files to maintain test cases, and for test data, we can use data, provider of TestNG framework.
Here in our hybrid framework, we don?t need to change anything in Keyword driven framework, here we just need to replace ExecuteTest.java file with HybridExecuteTest.java file.
This HybridExecuteTest file has all the code for keyword driven with data provider concept.
The complete pictorial representation of hybrid framework will look like
So, We can create three types of test framework using Selenium WebDriver.These are Data Driven, Keyword Driven, and Hybrid test framework.We can achieve Data-driven framework using TestNG?s data provider.In Keyword driven framework, keywords are written in some external files like excel file and java code will call this file and execute test cases.The hybrid framework is a mix of keyword driven and data driven framework.