Read Data From Database in Data Driven Framework
Are you having difficulty in maintaining the huge sets of exam cases for your application? Is the exam data scattered across various examination scripts? Do you lot have to maintain carve up exam scripts for every test environment and so search across all scripts if one value changes in the test data? Information technology is time-taking and hell of an effort, isn't it? Nosotros all wish the exam cases are consistent and written in a uniform way, following a set of rules like we have traffic rules and everyone tries to follow the same when on route. This is where information driven framework comes into play.
Yous can also make your test cases adhere to a uniform pattern and style, frame guidelines for your peers working on the application, to write the scripts based on the framed rules. In test automation, nosotros can reach all this using the Data Driven Framework. Allow's empathize how nosotros tin create a Data Driven Testing Framework for automating a web application using Selenium WebDriver, by covering the details in the following topics:
- What is an Automation framework?
- Why practise we need an Automation testing framework?
- What are the dissimilar types of automation frameworks in Selenium?
- What is Data Driven Framework?
- Also, what are the benefits of using the Data Driven Testing Framework?
- How to create a Data Driven Framework in Selenium using Apache POI?
What is an Automation Framework?
An automation testing framework is a set of guidelines or rules used for creating and designing test cases. The guidelines include coding standards, object repositories, test-data handling methods, processes to shop test results, or any other information on how to admission external resource.
A tester can ever write tests without a framework, information technology is not a mandatory footstep simply using an organized framework provides additional benefits like increased code re-usage, college portability, reduced script maintenance toll, and higher code readability. It also helps the team to write downwards the test script in a standard format. Using an automation testing framework, efficient design and development of automated test scripts enable and it ensures reliable analysis of issues or bugs for the system or application under test. The below section lists a few of the important benefits which justifies the need for an automation testing framework:
Why do we need an Automation testing framework?
It is important to employ a framework for automated testing every bit it improves the automation testing team's efficiency and test development speed. Some of the benefits of using the automation framework are as below:
- Standard Format for all the tests
- Improved examination efficiency
- Lower script maintenance costs
- Maximum test coverage
- Reusability of code
- Efficient Examination Data Direction
What are the dissimilar types of automation frameworks in Selenium?
When testing an application using Selenium WebDriver, at that place are three main types of frameworks that we can utilise to create automatic tests for whatever spider web application:
- Information Driven Test Framework.
- Keyword Driven Test Framework.
- Hybrid Test Framework.
Each of these frameworks has its own architecture and unlike benefits and disadvantages. When edifice out a exam programme, it'southward of import to choose the framework that is correct for yous.
- Data Driven Testing Framework is used to carve up the test script from the test data. You can test the same script with multiple sets of data. We will discuss this framework in detail in the post-obit topics.
- Keyword Driven Testing Framework is an extension of the Information Driven framework. It allows storing a set of code chosen 'keywords' in a separate code file, externally from the test script. We can reuse these keywords beyond multiple exam scripts. For details refer - Keyword Driven Framework
- Hybrid Driven Framework is a combination of both the Data-Driven and Keyword-Driven framework. Here, the keywords, as well equally the test data, are external. We maintain Keywords in a separate file and examination data in excel file or CSV files or database. For details refer - Hybrid Framework.
Hither, in this commodity, Permit us take a deep dive into the Data Driven Exam Framework.
What is Data Driven Framework?
Generally, when we examination an application manually, we run the same scenario for multiple exam data. Additionally, we keep the aforementioned exam data in some files like Excel Files, Text Files, CSV Files, or any database. The same is true for automation as well, where we would like to run the same test scenario for multiple examination information. Let's say you have written an automation script to fill the student registration grade on the ToolsQA Demo site. There can be many students for registration, the just thing that is differentiating in the code is input values (Name, Address, Phone, Gender, etc..). Will you write a dissever script for every student to annals? Is there a way, we can reuse the lawmaking and but change the student data?
Yes, this is where the Data Driven Framework comes into play and makes the exam scripts piece of work properly for dissimilar sets of examination data. It saves time to write additional lawmaking. It's like write once and run many times mechanism as you lot can run the same Selenium script multiple times.
In simple words, we utilize the Data Driven Framework when we have to execute the same script with multiple sets of test data, whose storage is at a different place and not present within the exam script. Any changes done to the data will non impact the lawmaking of the examination.
What are the benefits of using the Data Driven Testing Framework?
Following are a few of the major benefits which a QA tin can reap when he/she develops the automation framework using the Data-Driven technique:
- Test cases can exist modified without much changes to lawmaking.
- Information technology allows testing the application with multiple sets of data values, especially during regression testing.
- It helps u.s. to split the logic of the test cases/scripts from the test information.
I of the almost commonly used, data sources for the test is Microsoft Excel Sheets. Nosotros can maintain the data in excel sheets and use them in the test script. Let's run into how nosotros can create a Information Driven UI automation framework by reading the test information from Excel files.
How to create a Data Driven Framework in Selenium using Apache POI?
We have learned in the previous article "Read & Write Data from Excel in Selenium" how to read and write information in Excel files using Apache POI so laissez passer the aforementioned data sets as test information to the Selenium tests. Just in that script, all the actions of reading data from an Excel file, writing data to the Excel file, passing the information to the Selenium actions were happening in the main method of the grade. That format is acceptable if we are just writing one or ii examination cases. However, when we have to develop an automation framework that will accept multiple exam scenarios, then information technology should properly organize and should have a defined folder hierarchy.
A basic thumb rule for the data driven testing framework would be to segregate the test data from the test scripts. Also, the actions to read/write the data from files should segregate and exist available as utilities.
Follow the steps as mentioned below to create a basic Data Driven framework, which volition be used to automate the "Student Registration Form".
- Create three New Packages in your Projection for testCases, testData, and utilities.
- Nether the testData packet, put your Excel Sheet that has test data. Using this, we separate the test data from the testCases.
- Under the utilities, create a New Class and name information technology "ExcelUtils". It will contain all functions related to Excel used for reading and writing.
- Under the utilities parcel, create some other course "Constants". Information technology will contain the constant values across the framework like testdata file path, URL of the application, etc.
- Under the testCases package, we will create the exam files that contain the Selenium code for interacting with web elements. (For Example, RegisterStudentTest.java)
Subsequently performing the above steps, the folder construction will wait like:
Let's understand the details of each of these classes:
- ExcelUtils Form - It is a utility class that will incorporate all the methods related to Excel Sheet read and write operations forth with initializing the Workbook. You tin can then reuse the methods in different test cases, by creating an object of Excel Utils Form. The code for this class will be every bit below -
parcel utilities; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public course ExcelUtils { private static HSSFWorkbook workbook; private static HSSFSheet sheet; individual static HSSFRow row; private static HSSFCell jail cell; public void setExcelFile (String excelFilePath,String sheetName) throws IOException { //Create an object of File class to open up xls file File file = new File(excelFilePath); //Create an object of FileInputStream grade to read excel file FileInputStream inputStream = new FileInputStream(file); //creating workbook instance that refers to .xls file workbook=new HSSFWorkbook(inputStream); //creating a Canvass object sheet=workbook.getSheet(sheetName); } public String getCellData (int rowNumber,int cellNumber){ //getting the jail cell value from rowNumber and cell Number cell =canvass.getRow(rowNumber).getCell(cellNumber); //returning the jail cell value as string return prison cell.getStringCellValue(); } public int getRowCountInSheet (){ int rowcount = canvass.getLastRowNum()-sheet.getFirstRowNum(); return rowcount; } public void setCellValue (int rowNum,int cellNum,String cellValue,Cord excelFilePath) throws IOException { //creating a new jail cell in row and setting value to information technology sheet.getRow(rowNum).createCell(cellNum).setCellValue(cellValue); FileOutputStream outputStream = new FileOutputStream(excelFilePath); workbook.write(outputStream); } }
The above lawmaking contains different methods like setExcelFile to initialize the Excel Workbook, getCellValue to retrieve the value present in a particular prison cell in the file, setCellValue to ready some value into a newly created cell. In a similar style, you can create different methods related to excel operations in this class.
- Constants Class- It is used to put constant values in a file then that the aforementioned can be reused across examination cases. One more reward of placing values in separate files is that since these values are common beyond various tests if there is any change in any of the values, you will only have to update in one place. For example, if the file path is changed, and then instead of updating all the examination cases with the new value, you tin can but update it here in i file. The structure and values present in this grade are equally below -
package utilities; public form Constants { public static final String URL = "https://demoqa.com/automation-practice-form"; public static final String Path_TestData = "Due east:\\Projects\\src\\testData\\"; public static final String File_TestData = "TestData.xls"; }
- RegisterStudentTest- It is the test script for the pupil registration form, which nosotros used to enter the first name, final name, mobile, e-mail, gender, etc for a particular student. Since we have now separated the excel related methods in a separate file, the code of our test case likewise changes.
We will create an object of ExcelUtils form in this exam file and also use Constants to refer to the path of the file.
The updated code now looks like -
package testCases; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import utilities.Constants; import utilities.ExcelUtils; import java.io.IOException; import java.util.concurrent.TimeUnit; public class RegisterStudentTest { //creating object of ExcelUtils course static ExcelUtils excelUtils = new ExcelUtils(); //using the Constants grade values for excel file path static String excelFilePath =Constants.Path_TestData+Constants.File_TestData; public static void main (String args[]) throws IOException { //ready the Chrome Driver path Organization.setProperty("webdriver.chrome.driver","East:\\Projects\\chromedriver.exe"); //Creating an object of ChromeDriver WebDriver driver = new ChromeDriver(); //launching the specified URL commuter.get("https://demoqa.com/automation-practise-form"); //Identify the WebElements for the pupil registration form WebElement firstName=driver.findElement(By.id("firstName")); WebElement lastName=driver.findElement(By.id("lastName")); WebElement email=driver.findElement(Past.id("userEmail")); WebElement genderMale= commuter.findElement(By.id("gender-radio-1")); WebElement mobile=commuter.findElement(By.id("userNumber")); WebElement address=driver.findElement(Past.id("currentAddress")); WebElement submitBtn=driver.findElement(By.id("submit")); //calling the ExcelUtils course method to initialise the workbook and sheet excelUtils.setExcelFile(excelFilePath,"STUDENT_DATA"); //iterate over all the row to print the data present in each cell. for(int i=1;i<=excelUtils.getRowCountInSheet();i++) { //Enter the values read from Excel in firstname,lastname,mobile,email,address firstName.sendKeys(excelUtils.getCellData(i,0)); lastName.sendKeys(excelUtils.getCellData(i,1)); email.sendKeys(excelUtils.getCellData(i,ii)); mobile.sendKeys(excelUtils.getCellData(i,3)); address.sendKeys(excelUtils.getCellData(i,four)); //Click on the gender radio button using javascript JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", genderMale); //Click on submit button submitBtn.click(); //Verify the confirmation message WebElement confirmationMessage = driver.findElement(Past.xpath("//div[text()='Thanks for submitting the grade']")); //check if confirmation bulletin is displayed if (confirmationMessage.isDisplayed()) { // if the message is displayed , write PASS in the excel canvas using the method of ExcelUtils excelUtils.setCellValue(i,vi,"Pass",excelFilePath); } else { //if the message is not displayed , write Neglect in the excel sheet using the method of ExcelUtils excelUtils.setCellValue(i,half dozen,"FAIL",excelFilePath); } //shut the confirmation popup WebElement closebtn=commuter.findElement(By.id("closeLargeModal")); closebtn.click(); //wait for page to come back to registration page afterwards close button is clicked driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS); } //closing the driver driver.quit(); } }
The above class will perform the actions on the student registration page. All the same, if y'all notice, methods of the ExcelUtils handle all the excel related lawmaking.
Then, this is i of the means you can employ the data driven framework in Selenium. Additionally, you lot tin utilize the reward of running the same test across multiple sets of data.
Key Takeaways
- Data-driven is a exam automation framework that stores test data in a tabular array or spread spreadsheet format.
- Additionally, a Information-driven Testing framework helps to split up test data from Functional tests. Moreover, it allows testing applications with multiple sets of data values without rewriting the same lawmaking across unlike test scripts.
- Too, we perform data-driven Testing in Selenium using Apache POI. It is a library that helps to read and write data in the Excel Canvass.
Source: https://www.toolsqa.com/selenium-webdriver/data-driven-framework/
0 Response to "Read Data From Database in Data Driven Framework"
Mag-post ng isang Komento