DEV Community

Cover image for [Part 7]File I/O and Data Persistence in TypeScript for QA Automation
TestAmplify
TestAmplify

Posted on

[Part 7]File I/O and Data Persistence in TypeScript for QA Automation

Introduction

Handling files and data persistence is crucial in automation testing for storing and retrieving test results. This module covers reading and writing files, working with CSV and JSON data, and automating file operations using Node.js with TypeScript.


Lesson 1: Reading and Writing Files using Node.js fs Module

Concept:
The fs module in Node.js enables file system interactions for handling test configurations, logs, and results.

Key Topics:

  • fs Module Basics: Performing file operations.
  • Reading Files: Loading configuration and test data.
  • Writing Files: Storing test results.
  • File Manipulation: Appending, renaming, and deleting files.

Example:

import * as fs from 'fs';

// Writing to a file
fs.writeFileSync('testResults.txt', 'Login Test: Passed');

// Reading from a file
const data = fs.readFileSync('testResults.txt', 'utf8');
console.log(data);
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Always handle file read/write operations asynchronously to avoid blocking execution.


Lesson 2: Working with CSV Files in TypeScript

Concept:
CSV files are widely used in test automation for storing test data and results.

Key Topics:

  • Reading CSV Data: Extracting test case inputs.
  • Writing CSV Files: Storing automation results.
  • Using CSV Libraries: Implementing csv-parser or fast-csv.
  • Data-Driven Testing: Using CSV for multiple test scenarios.

Example:

import * as fs from 'fs';
import * as csv from 'csv-parser';

fs.createReadStream('testData.csv')
  .pipe(csv())
  .on('data', (row) => console.log(row))
  .on('end', () => console.log('CSV File Processing Completed'));
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use CSV files for data-driven test execution to improve efficiency.


Lesson 3: Parsing JSON Data in TypeScript for API Testing

Concept:
JSON is commonly used for API responses and test configurations in automation.

Key Topics:

  • JSON Structure: Understanding JSON format.
  • Parsing JSON: Converting JSON strings to TypeScript objects.
  • Stringifying JSON: Writing JSON data to files.
  • Using JSON for API Testing: Handling request and response validation.

Example:

let testData = '{ "testName": "Login", "status": "Passed" }';
let parsedData = JSON.parse(testData);
console.log(parsedData.testName);

let jsonString = JSON.stringify(parsedData, null, 2);
console.log(jsonString);
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use TypeScript interfaces for strong typing in JSON data processing.


Lesson 4: Automating File System Operations in TypeScript

Concept:
File system automation helps manage logs, backups, and test results effectively.

Key Topics:

  • Creating and Managing Directories: Handling test storage folders.
  • File Copying: Automating test data backups.
  • Monitoring File Changes: Detecting modifications in test logs.
  • Deleting Files: Cleaning up temporary files.

Example:

import * as fs from 'fs';

// Creating a new directory
if (!fs.existsSync('logs')) {
    fs.mkdirSync('logs');
}

// Watching a file for changes
fs.watch('testResults.txt', (event, filename) => {
    console.log(`File ${filename} was modified: ${event}`);
});
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use file watchers to trigger automation workflows when logs update.


Conclusion

This module provided insights into TypeScript’s file handling capabilities for test automation, enabling better test data management and logging.

Key Takeaways:

  • The fs module allows efficient reading and writing of files.
  • CSV files simplify data-driven test execution.
  • JSON is essential for handling API test data.
  • Automating file system operations improves test reporting and log management.

What’s Next?
In the next module, we will explore Introduction to TypeScript for Playwright – Preparation for UI Automation, where we will cover setting up Playwright, interacting with web elements, and transitioning to advanced UI automation strategies.

Visit us at Testamplify | X | Instagram | LinkedIn

Image description

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay