Data-driven framework with selenium, Scala, and SBT

Reading Time: 4 minutes

Hello everyone, in our last blog we saw that how we can execute ours test in headless mode and now in this blog we will see how we can achieve Data-driven framework with selenium, Scala, and SBT.

Need for data-driven framework

  • A data-Driven framework helps us in separating the test script logic and the test data from one another. It allows us to write test automation scripts in which we can pass different sets of test data. 
  • The main reason behind a data-driven framework is the re-usability of code. The same block of code can be reused multiple times with different data sets every time.
  • The data is usually stored in an excel sheet or a CSV file. This enables us to modify our tests without making any changes to the code. We can simply update our data files and we are done.
  • The execution is faster as compared to the manual approach.

Data-driven testing using Apache POI

  • Apache POI is an open-source library that is used to design and modify Microsoft Office product files.
  • Using Apache POI we can read data from excel files and use them in our test suite. It is easy to use and implement.

DataDriven.scala file with Apache POI

import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.util.NumberToTextConverter
import org.apache.poi.xssf.usermodel.XSSFWorkbook

import java.io.FileInputStream
import java.util._


object DataDriven {
  def extractData(testSheetName: String, testCaseName: String, fieldName: String): ArrayList[String] = {
    val file = new FileInputStream(System.getProperty("user.dir") + "/Data/LoginData.xlsx")
    //taking location of the excel sheet
    val workbook = new XSSFWorkbook(file)
    //storing excel sheet in a XSSFWorkbook file
    val excelData = new ArrayList[String]  
    val sheets = workbook.getNumberOfSheets
    //extracting number of sheets in the workbook
    for (i <- 0 until sheets) {
      if (workbook.getSheetName(i).equalsIgnoreCase(testSheetName)) {
        //Check for the expected name of sheet inside the workbook
        val sheet = workbook.getSheetAt(i)
        val rows = sheet.iterator
        val firstRow = rows.next
        val ce = firstRow.cellIterator
        var k = 0
        var column = 0
        while ( {
          ce.hasNext
        }) {
          val value = ce.next
          if (value.getStringCellValue.equalsIgnoreCase(fieldName)) column = k
          //Check for the expected name of column inside the workbook
          k += 1
        }
        while ( {
          rows.hasNext
        }) {
          val r = rows.next
          if (r.getCell(column).getStringCellValue.equalsIgnoreCase(testCaseName)) {
            //Check for the expected name of row inside the workbook
            val cv = r.cellIterator
            while ( {
              cv.hasNext
            }) {
              val c = cv.next
              if (c.getCellType eq CellType.STRING) excelData.add(c.getStringCellValue)
              //handling of the input if it is of string type
              else excelData.add(NumberToTextConverter.toText(c.getNumericCellValue))
              //handling of the input if it is of integer type
            }
          }
        }
      }
    }
    excelData
  }
}
  • In the above given code we have written a small function which takes three inputs.
    • testSheetName : Sheet in which our tests is stored.
    • testCaseName : Name of the row in which data is stored.
    • fieldName : Name of the column from which we need to extract data.
  • We will read the excel sheet from the given location.
  • Then, store our whole workbook in an XSSFWorkbook type file.
  • Extract the number of sheets inside the workbook and compare it with the required sheet name.
  • When we find the correct sheet we will iterate through that sheet searching for the desired column name.
  • Once we find the desired column, we will start looking for the desired row.
  • After this, we will handle the scenario where if the input from the excel file is of integer instead of a string. Then we will convert it to string.
  • Finally, we will be returning the data.

Selenium test script

import org.scalatest._
import org.scalatest.matchers._
import org.scalatestplus.selenium.Chrome.webDriver
import org.scalatestplus.selenium._

class Test extends flatspec.AnyFlatSpec with should.Matchers with WebBrowser {

  System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/DriverFiles/chromedriver")
  //reading location of chrome driver
  val host = "https://login.salesforce.com/?locale=in"
  // storing URL to be automated
  val extractedData = DataDriven.extractData("Sheet1", "Login", "TestCases")
  //call the function from DataDriven.scala
  "The blog app home page" should "have the correct title" in {
    go to host
    //Hit the required URL
    emailField("username").value = extractedData.get(1)
    //Find the email field and enter the email data from the excel sheet
    pwdField("password").value = extractedData.get(2)
    //Find the password field and enter the password data from the excel sheet
    click on id("Login")
    //Click on login button
    pageTitle should be("Login | Salesforce")
    //Validate that the title of the page is correct 
    webDriver.close()
    //Close the web browser 
  }
}
  • In the above code, we are extracting the chrome driver.
  • Then we are calling the function which we described above and giving appropriate inputs to it.
  • After this, we are hitting the Salesforce.com login page and trying to locate email and password fields.
  • Once the fields are identified we are entering the data which we are reading from the excel sheet.
  • After entering the data we are hitting on the login button to check if we can log in or not.

So, this was a short blog on how we can create a data-driven framework with selenium and Scala. See you in the next one

References


Knoldus-blog-footer-image

Written by 

Ankur is a Sr. QA Consultant having experience of more than 3 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advanced concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading