I am fairly new to TypeScript and Playwright, but I have experience in coding. I believe I have a good understanding of what I am trying to achieve, but I am encountering a problem that I can't seem to figure out. I'm hoping someone can help me.
My project involves multiple TypeScript files where one file contains individual API calls and the other file orchestrates these calls in a specific order. This separation helps me focus on setting up the test cases before executing them, making my tests more concise and organized.
The issue arises when I try to call the orchestrated API calls from the second file. While the individual calls work fine when called directly from the test, they fail when encapsulated in a method within another file. Debugging this issue has proven challenging as the execution abruptly fails back to the test level without clear error messages.
The individual API call .ts file structure is as follows: -
import dotenv from 'dotenv';
import {APIRequestContext, expect, request} from '@playwright/test';
module.exports = {
authorisation: async function ( request: any ){
dotenv.config();
const response = await request.post(`myURL`, {
data: {
stuffs
}
});
const token = JSON.parse(await response.text());
return token.access_token;
}
}
Calling this from a test like below works fine:
import { test, expect, request, APIRequestContext } from '@playwright/test';
const API_Calls = require("./common/API_Calls");
test('Do the thing', async ({ page, request }) => {
var authtoken = await API_Calls.authorisation(request);
...execute the rest of the test logic...
});
However, when attempting to consolidate all calls into a "collection" .ts file, the failure occurs at the specified point:
import {expect, request} from '@playwright/test';
const API_Calls = require("./API_Calls");
module.exports = {
doAllTheThingsForMe: async function (ADetailsJson: string, BDetailsJson: string, request: any){
const authtoken = await API_Calls.authorisation(request); <--this is where it dies
...continue with the remaining actions...
}
And running this setup through a test results in the following error message:
1) My Testies API.spec.ts:49:5 › Do that long test
─────────────────────────────
TypeError: finalResponse.status is not a function
50 | var finalResponse = Group_API_Calls._doAllTheThingsForMe("AString","BString");
51 |
> 52 | const responseCode = finalResponse.status();
| ^
53 |
54 | });
55 |
If I manually include all individual calls in each test, it works fine, but that's not an efficient approach for repetitive testing scenarios. Any suggestions or insights would be greatly appreciated!