Earlier, we presented you with the basics of test automation frameworks, their elements, and varieties. We examined a few common frameworks embraced by the testing community.
We offered a succinct explanation of Module-based Frameworks, Library Architecture-based frameworks, Keyword-driven frameworks, Data-driven Frameworks, and Hybrid frameworks. There are several other frameworks available as well.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Keep in mind that we will be employing a Data-Driven Test Automation Framework for the future lessons.
In this part of our Selenium instructional series, we will present a prototype framework that harnesses Excel to store test data and handle Excel files. We will progressively enhance this methodology and implement new strategies and tools to fortify it.
Let’s get started and discover:
- Strategy for creating a framework using a prototype project
- Extracting test data from an external data repository
Forwarding, we will start by outlining the project structure we will establish to organize the disparate project components.
Check out the diagram below for the project layout we have devised for the prototype. Any Eclipse user can effortlessly set up this Java project, akin to the ones we assembled in earlier lessons.
An Overview of Selenium Project Folder Structure
#1) src – This folder houses all the test scripts, generics, readers, and utilities. These assets typically exist as Java classes. We have devised a hierarchy of folders within the source (src) folder.
a) test – The “test” folder predominantly comprises two components: test suite and folders embodying various modules of the application under test. Every folder contains test scripts pertinently relating to the corresponding module. A test suite is a logical aggregation of several test scripts. Users can choose the test scripts they wish to run in subsequent instances.
b) utilities – The “utilities” folder encompasses various generics, constants, readers, and classes for handling custom exceptions. Each subfolder within utilities has its unique purpose.
- Excel Reader – A universal class for extracting test data (input parameters and expected outcomes) from Excel sheets
- EnvironmentConstants – This folder comprises Java classes that hold static variables referring to paths and additional environmental specifics. These specifics can span application URLs, database URLs, database login data, and URLs for third-party tools. Different environments (development, production, testing, etc.) might have distinguished application URLs.
- DataSetters – This folder houses classes that implement getters and setters for test data obtained from Excel sheets. ArrayLists are employed to load numerous sets of test data.
- UserRoles – This folder contains classes that regulate role-based access rules for certain users.
- FunctionLibrary – This folder possesses classes that encompass shared functions and methods utilized by several classes. Typically, there is a need to perform specific tasks before and after test performance, such as logging into the application, setting up environments, activities concerning roles, data manipulation, writing results, and methods that construct pre/post-conditions for additional methods. Instead of repeatedly programming these activities in each test script, it is preferable to establish a separate class for such activities.
- PreConditionalMethods
- PostConditionalMethods
Usually, certain procedures must be performed prior to and subsequent to test execution, such as logging into the application, setting up environments, tasks in relation to user roles, data manipulation, results reporting, and methods that yield pre/post-conditions for other procedures. Rather than repeatedly encoding these activities in each test script, it is advisable to design a separate class for these procedures.
CommonMethods
Similar to pre and post conditions, there might be methods and functions utilized by multiple test scripts. These procedures are consolidated in a class, and test scripts can avail of them using an instance of the common class.
#2) excel files – These Excel files function as the data supplier for test script execution. Every test script has its distinct test data file in the form of a standalone Excel sheet. The test script name and the commensurate test data file/Excel sheet share the same name for ease of tracing. The sample test data format is shown below:
Test Data Format
Each column signifies a key, and every row indicates a test data value. You can specify multiple rows to execute the same test script with manifold data sets.
Bear in mind that the test data format is user-defined; thus you can modify the test data files to align with your needs.
#3) library – This folder acts as a depot for all the required jar files, libraries, drivers, etc., essential for setting up the test environment and running the test scripts. Refer to the diagram below to observe the libraries employed in our project.
#4) logs – This folder holds a .txt file that saves logging information for each execution.
#5) test material – This folder contains any test data required to be uploaded for specific test scenarios, such as files, documents, images, reports, etc.
#6) build.xml – This XML file is harnessed by the Ant Server to automate the entire build procedure.
#7) log4j.xml – This XML file is used by a Java-based utility named Log4j to generate execution logs.
Note: We will delve into logs, user-defined exceptions, and Ant in more depth in subsequent lessons. Therefore, don’t fret if you currently find these concepts perplexing.
Let’s now proceed and comprehend how to access Excel files and populate test data into our test scripts.
For easier understanding of the process, it will be broken down into the following steps.
Creation of Test Data
Step 1: The initial and essential step is to produce the test data that we will implement to run the test scripts. Abiding by the test data format outlined earlier, let’s fabricate an Excel file named “TestScript1” and populate the values for each element.
Step 2: Subsequently, we ought to download a Java-based API/Library dubbed “Java Excel Library” (jxl) that permits us to use the generic methods for Excel manipulation.
Step 3: Devise a generic Excel reader class aka “ExcelReader.java”. Paste the following code into ExcelReader.java.
package Utilities; import java.io.File; import java.io.IOException; import java.util.Hashtable; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; /** * This ancillary class is devised to read the test data from an Excel file prior to performing the test steps. * It loads the Excel file and reads its column entries. */ public class ExcelReader { /** * The worksheet to read in the Excel file */ public static Sheet worksheet; /** * The Excel file to read */ public static Workbook workbook = null; /** * Preserve the column data */ public static Hashtabledictionary = new Hashtable (); /** * Construct a constructor * * @param excelSheetPath * @throws BiffException * @throws IOException */ public ExcelReader(String excelSheetPath) throws IOException, BiffException { // Initialize try { workbook = Workbook.getWorkbook(new File(excelSheetPath)); worksheet = workbook.getSheet("Sheet1"); } catch (IOException e) { throw new IOException(); } } /** * Provides the number of rows * * @return rows */ public static int getRowCount() { return worksheet.getRows(); } /** * Provides the cell value given column and row values * * @param column * @param row * @return cell contents */ public static String readCell(int column, int row) { return worksheet.getCell(column, row).getContents(); } /** * Construct a column dictionary to retain all the column names */ public static void createColumnDictionary() { // Iterate through all the columns in the Excel sheet and store the // value in the Hashtable for (int col = 0; col < worksheet.getColumns(); col++) { dictionary.put(readCell(col, 0), col); } } /** * Read column names * * @param columnName * @return value */ public static int getCell(String columnName) { try { int value; value = (Integer) dictionary.get(columnName); return value; } catch (NullPointerException e) { return 0; } } }
Step 4: Build a generic class labeled "CommonMethods.java". Inside this class, build a common method that reads cells off the Excel sheet utilizing the methods executed in ExcelReader.java.
/** * Read the test data from the excel file * * @param data The TestData data object */ public void readExcelData(TestData data) { ArrayListbrowser = new ArrayList (); ArrayList username = new ArrayList (); ArrayList password = new ArrayList (); ArrayList element1 = new ArrayList (); ArrayList element2 = new ArrayList (); ArrayList element3 = new ArrayList (); // Acquire the data from the Excel file for (int rowCnt = 1; rowCnt < ExcelReader.getRowCount(); rowCnt++) { browser.add(ExcelReader.readCell(ExcelReader.getCell("Browser"), rowCnt)); username.add(ExcelReader.readCell(ExcelReader.getCell("User ID"), rowCnt)); password.add(ExcelReader.readCell(ExcelReader.getCell("Password"), rowCnt)); element1.add(ExcelReader.readCell(ExcelReader.getCell("Element1"), rowCnt)); element2.add(ExcelReader.readCell(ExcelReader.getCell("Element2"), rowCnt)); element3.add(ExcelReader.readCell(ExcelReader.getCell("Element3"), rowCnt)); } data.setBrowser(browser); data.setLoginUser(username); data.setPassword(password); data.setElement1(element1); data.setElement2(element2); data.setElement3(element3); }
Step 5: Set up a new Java class labeled "TestData.java". This class will function as a getter and setter for the Excel data. Copy and paste the ensuing code into the TestData.java class.
package Utilities.dataSetters; import java.util.ArrayList; public class TestData { private ArrayListloginUser = null; private ArrayList password = null; private ArrayList browser = null; private ArrayList element1 = null; private ArrayList element2 = null; private ArrayList element3 = null; /** * @return loginUser */ public ArrayList getLoginUser() { return loginUser; } /** * @param loginUser */ public void setLoginUser(ArrayList loginUser) { this.loginUser = loginUser; } /** * @return password */ public ArrayList getPassword() { return password; } /** * @param password */ public void setPassword(ArrayList password) { this.password = password; } /** * @return browser */ public ArrayList getBrowser() { return browser; } /** * @param browser */ public void setBrowser(ArrayList browser) { this.browser = browser; } /** * @return element1 */ public ArrayList getElement1() { return element1; } /** * @param element1 */ public void setElement1(ArrayList element1) { this.element1 = element1; } /** * @return element2 */ public ArrayList getElement2() { return element2; } /** * @param element2 */ public void setElement2(ArrayList element2) { this.element2 = element2; } /** * @return element3 */ public ArrayList getElement3() { return element3; } /** * @param element3 */ public void setElement3(ArrayList element3) { this.element3 = element3; } }
Step 6: The next move is to create instances of the TestData.java and CommonMethods.java classes in your test script to retrieve and populate the test data. Use this code fragment to instantiate objects, read Excel data, and populate values as required.
// Initialize Objects public ExcelReader excelReaderObj; CommonMethods commonMethodObj = new CommonMethods(); TestData td = new TestData(); // Import the Excel file for testing excelReaderObj = new ExcelReader(Excel file path); // Load the Excel sheet columns into a dictionary for employing in test cases excelReaderObj.createColumnDictionary(); // Extract the data from the Excel file commonMethodObj.readExcelData(td); // Populate the username driver.findElement(By.id("idOfElement")).sendKeys(td.getLoginUser().get(0));
By utilizing the object of the TestData.java class in combination with the getters, you can populate any test data value inside the script.
Summing Up:
This lesson spotlighted subjects like the Framework Creation and Fetching test data from Excel. We put forth a blueprint for setting up a framework leveraging a prototype project and briefly reviewed its distinct components and aspects.
For extracting test data stored in an external data store, we made use of a Java-based API called jxl. We also furnished sample code for reading and embedding Excel data into test scripts.
Subsequent Tutorial #22: In the upcoming lesson, we will center around the notions of generics and their access mechanisms. We will create prototype generic methods and exhibit how to access them inside test scripts. We will also familiarize you with the concept of a Test Suite and furnish sample code for its creation.