javascript - Playwright throws aPIRequestContext.get: Request context disposed - Stack Overflow

I have a code that needs to loop the json file. However it throws this error:aPIRequestContext.get: Re

I have a code that needs to loop the json file. However it throws this error:

aPIRequestContext.get: Request context disposed.

And I have no idea why. Here's how I implemented it:

When I try to put the block of code outside the base.loopJsonData, it works... but not when inside. I tried to print the json details and I'm getting an output but not when I'm using request.get()

Here's the loopJsonData:

public loopJsonData(json, data, callback) {
        const inputData = this.loadJSONData(json, data);
        inputData.forEach((val, i) => {
            ((item, index) => {
                callback(item, index, inputData);
            })(val, i);
        });
    }

Here's the loadJSONData:

public loadJSONData(path: string, testData: string) {
        let jsonPath;

        this.contentPath = join(process.cwd(), path);
        this.contentPath = normalizer.normalize(this.contentPath);
        this.contentPath = JSON.parse(fs.readFileSync(this.contentPath, "utf8"));
        jsonPath = this.contentPath;
        return jsonPath[testData];
    }

I have a code that needs to loop the json file. However it throws this error:

aPIRequestContext.get: Request context disposed.

And I have no idea why. Here's how I implemented it:

When I try to put the block of code outside the base.loopJsonData, it works... but not when inside. I tried to print the json details and I'm getting an output but not when I'm using request.get()

Here's the loopJsonData:

public loopJsonData(json, data, callback) {
        const inputData = this.loadJSONData(json, data);
        inputData.forEach((val, i) => {
            ((item, index) => {
                callback(item, index, inputData);
            })(val, i);
        });
    }

Here's the loadJSONData:

public loadJSONData(path: string, testData: string) {
        let jsonPath;

        this.contentPath = join(process.cwd(), path);
        this.contentPath = normalizer.normalize(this.contentPath);
        this.contentPath = JSON.parse(fs.readFileSync(this.contentPath, "utf8"));
        jsonPath = this.contentPath;
        return jsonPath[testData];
    }
Share Improve this question asked Dec 31, 2021 at 3:26 ohloriohlori 3102 gold badges10 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

loopJsonData takes async function but doesn't await it inside (just calls it synchronously for each data item inputData.forEach((val, i) => {). With such approach inner request.get() will run after the test method finishes and it will race with tear down logic which disposes the request (essentially you call request.get() after request.dispose() has been called).

To fix this you should ensure that all async callbacks are waited for, something like this:

async loopJsonData(json, data, callback) {
    const inputData = this.loadJSONData(json, data);
    const promises = [];
    inputData.forEach((val, i) => {
        promises.push(callback(val, i, inputData));
    });
    await Promise.all(promises);
}

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信