javascript - Scenario reads old data from the previous run - Stack Overflow

What I am trying to achieve is that it should filter users' data in the first scenario which is cu

What I am trying to achieve is that it should filter users' data in the first scenario which is currently working well and save in csv file which is also working. When the 2nd scenario execution begins, it should read filtered data and run tests. This is not working as expected as it reads old data from the previous run. I have to run test twice for the desired result.

I am currently facing this challenge and wondering if you might be able to help me out. I'd appreciate any guidance or advice you could offer. Please suggest some better way of doing it. I have an alternative approach to handle this using

gauge.dataStore.scenarioStore.put("filteredUsers", filteredUsers)

But I want to see if the same can be achieved with csv as DataStore makes troubleshooting harder if there are many rows in a table. Thanks.

This is my spec file written in Gauge.

The problem lies in scenario 2 table: test_data\QA3\users.csv

# Verify users
tags: filter

// scenario 1
## Populate CSV with users based on role
* Filter users data based on role
    |id     |name       |role       |
    |-------|-----------|-----------|
    |1      |Alice      |admin      |
    |2      |John       |editor     |
    |3      |Bob        |admin      |
    |4      |Shawn      |subscriber |
    |5      |Eve        |admin      |
    |6      |Tom        |editor     |
    |7      |Jenny      |author     |   

// scenario 2
## Verify the filtered users
table: test_data\QA3\users.csv
* Show filtered users based on role on the console <id>, <name>, <role>

Step implementation

"use strict"

const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');

const filePath = path.join(process.cwd(), "test_data/QA3/users.csv");

beforeSpec(() => {
    if(fs.existsSync(filePath)) {
        // delete the existing file if it exists
        fs.unlinkSync(filePath);
        console.log("Deleted existing csv file");
    }
}, {tags: ["filter"]});

step("Filter users data based on role <table>", async (table) => {
    let testData = [];

    for(let i=0;i<table.rows.length;i++) {
        let obj = {
            id: table.rows[i].cells[0],
            name: table.rows[i].cells[1],
            role: table.rows[i].cells[2]
        }
        
        testData.push(obj);
    }

    console.log(testData)

    console.log(`ROLE from env: ${process.env.ROLE}`);

    let selectedRoles = process.env.ROLE ? process.env.ROLE.split(",").map(role => role.trim().toLowerCase()) : [];

    let filteredUsers;
    if(selectedRoles.length === 0) {
        console.log("No Role specified. Loading all users data");
        filteredUsers = testData; // Run all users data
    }else {
        console.log(`Filtering users based on roles: ${selectedRoles.join(",")}`);
        filteredUsers = testData.filter(user => selectedRoles.includes(user.role.toLowerCase()))
    }

    let csvContent = "id,name,role\n" + filteredUsers.map(u => `${u.id},${u.name},${u.role}`).join("\n");

    try {
        // write filtered data to csv file synchronously
        fs.writeFileSync(filePath, csvContent, "utf8");
        console.log("Filtered data saved");
    } catch (err) {
        console.log("Error writing to CSV file", err);
    }

    new Promise(resolve => setTimeout(resolve, 300));
})

step("Show filtered users based on role on the console <id>, <name>, <role>", async (id,name,role) => {
    console.log(`ID is ${id}`);
    console.log(`Name is ${name}`);
    console.log(`Role is ${role}`);
});

I am using following command to run test

set ROLE=editor && gauge run specs\Verify_users2.spec --tags "filter"

ROLE is defined in properties file.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744213220a4563433.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信