https://www.browserstack.com/guide/data-driven-framework-in-selenium
Let’s write a code snippet to read the data files.
Step 1: Go to the Eclipse IDE and create a project. Add all the dependencies for TestNG, Selenium and Apache POI.
Step 2: Create a class file to write the functionality.
import org.openqa.selenium.By; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ExcelExample{ @Test(dataProvider="testdata") public void demoClass(String username, String password) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "Path of Chrome Driver"); Webdriver driver = new ChromeDriver(); driver.get("<a href="https://www.browserstack.com/users/sign_in</a>"); driver.findElement(By.name("user[login]")).sendKeys(username); driver.findElement(By.name("user[password]")).sendKeys(password); driver.findElement(By.name("commit")).click(); Thread.sleep(5000); Assert.assertTrue(driver.getTitle().matches("BrowserStack Login | Sign Into The Best Mobile & Browser Testing Tool"), "Invalid credentials"); System.out.println("Login successful"); } @AfterMethod void ProgramTermination() { driver.quit(); } @DataProvider(name="testdata") public Object[][] testDataExample(){ ReadExcelFile configuration = new ReadExcelFile("Path_of_Your_Excel_File"); int rows = configuration.getRowCount(0); Object[][]signin_credentials = new Object[rows][2]; for(int i=0;i<rows;i++) { signin_credentials[i][0] = config.getData(0, i, 0); signin_credentials[i][1] = config.getData(0, i, 1); } return signin_credentials; } }
In the above code, there is a “TestDataExample() method” in which the user has created an object instance of another class named “ReadExcelFile”. The user has mentioned the path to the excel file. The user has further defined a for loop to retrieve the text from the excel workbook. But to fetch the data from the excel file, one needs to write a class file for the same.
Try Running Selenium Tests on Cloud for Free
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 work_book; XSSFSheet sheet; public ReadExcelFile(String excelfilePath) { try { File s = new File(excelfilePath); FileInputStream stream = new FileInputStream(s); work_book = new XSSFWorkbook(stream); } catch(Exception e) { System.out.println(e.getMessage()); } } public String getData(int sheetnumber, int row, int column){ sheet = work_book.getSheetAt(sheetnumber); String data = sheet.getRow(row).getCell(column).getStringCellValue(); return data; } public int getRowCount(int sheetIndex){ int row = work_book.getSheetAt(sheetIndex).getLastRowNum(); row = row + 1; return row; }
In the code above, the user has used Apache POI libraries to fetch the data from the excel file. Next, it will point to the data present in the excel file and then enter the relevant username and password to the sign in page.
Note: The same thing can be done using a Data provider in TestNG. But to fetch the data from the Excel sheet, the user needs Apache POI jar files.
Note: Please enter one valid credential to test.
Advantages of Data Driven Testing Framework
- Allows testing of the application with multiple sets of data values during regression testing
- Separates the test case data from the executable test script
- Allows reusing of Actions and Functions in different tests
- Generates test data automatically. This is helpful when large volumes of random test data are necessary
- Results in the creation of extensive code that is flexible and easy to maintain
- Lets developers and testers separate the logic of their test cases/scripts from the test data
- Allows execution of test cases several times which helps to reduce test cases and scripts
- It does not let changes in test scripts affect the test data.
By incorporating data-driven testing using Selenium, testers can refine their test cases for more efficient execution. This shortens timelines, makes their lives easier and results in more thoroughly tested and better quality software.
No comments:
Post a Comment