top of page

External File Reading

Reading External files is important for several reasons

1)      Data Driven Testing:

  • External Files such as Excel Spreadsheets, JSON files, Properties files, XML files, Text files, PDF files allows us to separate test data from test code.

  • This supports data-driven testing, where the same test logic can be executed with different sets of input data.


2)      Maintenance is Easy:

  • Storing test data and configurations(URL, Credentials. For each environment these could change) in external files promotes easy of maintenance.

  • Instead of modifying the test code, you can update the external files making it simple to manage changes, especially in large and complex test suites.


3)      Reusability:

  • Externalizing data enables the creation of more reusable and modular test code.

  • Test cases can be reused for different test environments or data sets.

  • Efficiently test multiple scenarios without code duplication.


4)      Collaboration:

  • Even non-technical team members can contribute test data without altering code.

  • This way we can be more collaborative and communicative in testing process.

Benefits of External Files in One Word:

1)      Increased test coverage.

2)      Improved maintenance and reusability of test cases.

3)      Enhanced collaboration and communication.

4)      Efficient test configuration and data management.


When we use External Files in Selenium Testing:

  • When we want to run same test with different sets of  input data.

  • When we want to keep test data separate from the code for easy maintenance.

  • When we need to configure test environments(Dev, QA, Staging), URLs, other Settings.

  • When we need to handle sensitive information securely.

Some Common External File Types that we are exploring in this blog are:

1)      Properties File: Syntax is in Key-value pairs for configuration settings.

2)      Excel File: Used for different data types and formatting.

3)      JSON File: For web-based data exchange.

4)      XML: Flexible for Complex Data

5)      Text: Append test execution logs and results to a text file.

6)      PDF: Extracting data from PDF files to perform verification or comparisons within our test scripts.


Now lets dig in to coding part with external files

This is our small project Structure.



src/test/java directory contains only one package reader which contains ExternalDataReader Class.


src/test/resources directory contains different kinds of files.


pom.xml(project object model) file which contains all required dependencies.


testng.xml file where we can execute all the tests at a time or we can execute in separate groups or even we can execute the tests individually.


Lets start our project by adding required dependencies in pom.xml file


  • Here we are adding selenium-java, testng, commons-io, Apache-poi(poi, poi-ooxml), WebDriverManager, pdf box dependencies which are required for our project.

  • selenium-java dependency: Which is essential for interacting with web elements, performing actions like clicks and data input, and validating web pages.

  • testng dependency: Which is very useful for test configuration, parallel execution and report generating. 

  • commons-io dependency: Which provides utility classes for file and stream handling operations.

  • Apache-poi dependency: For reading and writing Microsoft Office files like EXCEL. Which is very useful for reading data from EXCEL and writing data to EXCEL.

  • WebDriverManager dependency: Very useful for driver setup like Chrome, Firefox, Edge, Internet Explorer, Opera.

  • pdf box dependency: Useful to interact with PDF files, extract text and validate PDF content.

pom.xml File

Below is the ExternalDataReader class that contains five TestNG test methods for reading Text, JSON, PDF, Excel, XML, Property files.


Now lets dig deep into test methods

1)      loginToApplicationWithTxtData()

  •  Below is our text file screenshot

  •  Code to read the data from Text file

  • Explanation of above code: we are  logging into an application using data from a text file.

  • Method is annotated with @Test, indicating that it is a test method that should be executed by using TestNG framework.

  • A new instance of the ChromeDriver is created. Browser window is maximized.

  • A FileReader and BufferedReader are used to read data from the "TextData.txt" file.

  • While loop, reading each line from the text file until there are no more lines.

  • Each line from the file is split into an array of strings using a space as the delimiter.

  • The first element (index 0) is the email address, and the second element (index 1) is password.

  • Rest of the code is normal selenium code easy to understand.


2)      loginToApplicationWithJsonData()

  • Below is our Json Data file

  • Code to read the Json Data from file


  • Explanation of above code:

  • The code reads the content of the "JsonData.json" file into a string using Apache Commons IO library's FileUtils.

  • The Jackson ObjectMapper(mapper) is used to convert the JSON content into a list of hash maps.

  • Each hash map represents a set of login credentials.

  • The list of hash maps is converted to a two-dimensional object array (Object[][]), where each row represents a set of login credentials.

  • for loop to iterate over each set of login credentials.

3)      PDFFileReading()

  • Sample pdf file content

  • Code to read PDF file

  • Explanation of above code:

  • The Apache PDFBox library's PDDocument class is used to load the PDF document.

  • An instance of PDFTextStripper is created.

  • The getText() method is called to extract text content from the loaded PDF document.

  • The extracted text is printed to the console for debugging or verification purposes.

4)      loginToApplicationWithExcelData()

  • excel file we used to read login information

  • Code to read the data from EXCEL

  • Explanation of above code:

  • A File object is created to point the Excel file located at "src\test\resources\ExcelData.xlsx".

  • A FileInputStream is created to read data from the Excel file.

  • An XSSFWorkbook is created to point the Excel workbook, and the first sheet is retrieved.

  • A for loop is used to iterate through each row in the Excel sheet. The loop starts from the second row (index 1) because the first row is often used for headers in our case emailaddress and password.

 5)      loginToApplicationWithXMLData()

  • Sample XML data

  • Code to read XML data

  • Explanation of above code:

  • DocumentBuilderFactory and DocumentBuilder are used to create a Document object from the XML file.

  • The document is normalized to ensure consistent handling of the XML structure.

  • Here we first retrieved a list of "user" nodes from the XML document.

  • For each "user" node, the code checks if it's an element node and then retrieves the values of "emailaddress" and "password" child elements.


6)      loginToApplicationWithPropertyFileData()

  • Property file data

  • Code for reading Property File


  • Explanation of above code:

  • A File object is created to represent the properties file located at "src\test\resources\".

  • A FileInputStream is created to read data from the properties file.

  • A Properties object is used to load the properties from the file.

  • The code retrieves the values of "loginUrl," "emailAddress," and "password" from the properties file.

Now lets run the tests using testng.xml

  • The only class included in this test is "ExternalDataReader" from the "reader" package.

Below is our final Execution Report

Hope I covered everything we need even if I miss anything with code you can visit my git repo

Tip: In any project it is always better to maintain the test data outside of the code/logic.

Thank You, Sudha.


29 views0 comments
bottom of page