DEV Community

Cover image for The Complete Guide to Reading Files in Test Automation
JigNect Technologies
JigNect Technologies

Posted on

The Complete Guide to Reading Files in Test Automation

In modern test automation, keeping test logic separate from test data is a best practice that significantly improves the clarity, flexibility, and maintainability of your framework. Rather than embedding static or hard-coded values within test scripts, external data files like JSON, CSV, XML, or .properties offer a cleaner and more scalable solution. This approach is especially helpful in data-driven testing scenarios, where the same test case needs to run with multiple input combinations, or when working with dynamic content such as API payloads or environment-specific settings. By managing test data externally, teams can make changes quickly without altering the core test logic, which saves time, reduces errors, and supports smoother cross-environment executions. Whether you’re working with Selenium, Cypress, or other automation tools, reading external files enables you to build a modular and adaptable test suite that’s easier to update and more robust in the long run.

If you’re just starting your automation journey, don’t worry! We’ve got your back. Start with our blog:
Beginner’s Guide to Selenium with Java and Testing. It walks you through everything from setup to best practices.

Once you’re comfortable with the basics, this blog will guide you through the next level: working with dynamic test data using XML, JSON, and CSV files.

Why Reading External Files is Crucial in Automation

In modern automation frameworks, relying on hard-coded data can slow down progress and introduce unnecessary maintenance work. Instead, using external files to manage test data offers a smarter and more efficient approach. Here’s why it matters:

Separation of Data and Test Logic

Keeping test data separate from the test scripts helps maintain a clean and modular framework. This separation allows testers to update test data without modifying the actual code, improving clarity and maintainability.

Easy Maintenance and Scalability

External files like XML, JSON, or CSV make it easier to manage and scale your tests. When your application evolves, you only need to update the data files, not every single test case.

Enhanced Reusability and Fewer Hard-Coded Values

By avoiding hard-coded data, you make your test cases reusable across multiple scenarios. This reduces redundancy and ensures your automation framework is adaptable and future-ready.

Reading JSON Files in Automation Testing

JSON (JavaScript Object Notation) is one of the most commonly used file formats in test automation today. It’s lightweight, easy to read, and ideal for storing structured data making it a perfect fit for both backend and frontend testing needs.

What is JSON and Why It’s Used in Testing?

JSON is a text-based format for representing structured data, similar to how objects work in programming. It uses key-value pairs and is widely used to transmit data between a server and a client.

In the context of test automation, JSON files are useful because:

  • They can hold large sets of test data in a structured format.
  • They’re easy to parse using various libraries in almost every programming language.
  • They are readable and editable, even by non-programmers like QA analysts.

When Do Testers Use JSON Files?

  • API Testing: JSON is the default format for RESTful APIs. Testers often store expected API responses in JSON files and compare them during validation.
  • Data-Driven Testing: Instead of hardcoding test values, testers use JSON files to store multiple sets of inputs like usernames, passwords, or search terms.
  • Mocking Data: Sometimes APIs may not be ready yet, so testers mock the expected responses using JSON files.
  • Environment Configuration: URLs, credentials, timeouts, and other environment-specific values can be stored in JSON for easier switching between QA, staging, and production.

Tools and Libraries to Read JSON Files

Here are the most commonly used libraries and methods for reading JSON files:

Java doesn’t natively support JSON parsing, but several libraries make it easy:

Jackson (Most Popular)

  • Developed by FasterXML
  • Supports converting JSON to Java objects (POJO) and vice versa.

Gson (by Google)

  • Simple and efficient, widely used in Android development.

How to Read JSON Using Jackson in Java

Step 1: Add Jackson Dependency (for Maven users)

Add Jackson Dependency

** What jackson-databind Is Used For:**

  • Reading JSON files and mapping them to Java classes (e.g., LoginDetails)
  • Writing Java objects into JSON
  • Frequently used with ObjectMapper in automation frameworks

Note: These two classes remain the same in every test case. For simplicity, they are only shown once here and referenced in each scenario below.Step 2: Create a dataobjects Class (LoginDetails.java)

Reading JSON files

Purpose:

  • This class is part of the dataobjects package.
  • It defines a data model to store login information.
  • The class has three private fields: email, password, and userType.
  • It uses Lombok annotations to reduce boilerplate code.
  • @Getter and @setter auto-generate getter and setter methods.
  • @AllArgsConstructor creates a constructor with all three fields.
  • @NoArgsConstructor creates a no-argument constructor.
  • It’s used in the test to fill the login form with the correct credentials.
  • This makes the code cleaner, more maintainable, and reusable.
  • This class helps pass user data cleanly from CSV to tests.

Step 2: Create PageObject Class (Login Page.Java)

Create PageObject Class

*Explanation : *

  • The class LoginPage models the login page of a web application using the Page Object Model pattern.
  • It takes a Selenium WebDriver instance through its constructor to interact with the browser.
  • The PageFactory.initElements method initializes the web elements annotated with @FindBy.
  • The emailField is located by its HTML element ID “input-email”.
  • The passwordField is located by its HTML element ID “input-password”.
  • The loginButton is located using a CSS selector targeting a submit input with the value “Login”.
  • The method fillLoginFormFields takes a LoginDetails object and fills the email and password input fields accordingly.
  • It first clears the existing text in both fields before entering new data.
  • The method clickOnLoginButton clicks the login button to submit the form.
  • This class encapsulates all login page interactions, making test code cleaner and easier to maintain.

Step 3: Create JsonUtils Class (JsonUtils.java)

Create JsonUtils Class

Explanation:

  • The JsonUtils class reads user login data from a JSON file located at src/test/resources/data/users.json.
  • It uses Jackson’s ObjectMapper to parse the JSON file into a static JsonNode object called usersJsonObj.
  • This loading happens once in a static block when the class is first used; if loading fails, it throws a runtime exception.
  • The method getLoginDetailsFromSystemUserType() reads the system property userType.
  • If the userType property is not set, it throws an exception.
  • It then calls getLoginDetailsFromJsonFile() passing the userType value.
  • The method getLoginDetailsFromJsonFile(String userType) searches the JSON users array for a user matching the specified type.
  • When it finds a matching user, it converts that JSON node into a LoginDetails Java object using Jackson.
  • If no user matches the given type, it throws a runtime exception indicating the user was not found.
  • This utility helps fetch user login info dynamically based on system properties, useful for test automation.

Read The Full Blog Here:- [JigNect Technologies]

Top comments (0)