Reputation: 884
I've relatively new to typescript and playwright but not so much to coding so I'd say I have a fair idea of what Im attempting to achieve and can't see a logical reason why it doesn't work so I'm hoping someone might be able to aid me...
I have a spec.ts file with a test in, as usual, and this in turn calls a method from a .ts file which has been written to call multiple other methods from a second .ts file.
I've set it up like this because one file has all the individual API calls in, whereas the second file calls a selection of these in specific order so that my tests can just focus on getting a case to the state I want before I then complete the actual test I want, enabling me to write the tests with less lines, I split it between two ts files to help compartmentalise what was held where for my own sanity.
The issue I have is, if I call the individual API call methods directly from my test they work fine, but the moment I put the collection into a method in a second ts file and attempt to call it via that it errors, and if I try and step through it to debug when I get to the first post and attempt to run it it just exits right back to the test level with a failure.
So the individual API call .ts file has an entry looking like this: -
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;
}
}
If I call that from a test that looks like this it 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);
...draw the rest of the owl...
});
But If I create a "collection" .ts file designed to make all the calls in turn like this
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
...do the rest of the owls...
}
And then write a test like this to run it
test('Do that long test', async ({page}) => {
var finalResponse = Group_API_Calls.doAllTheThingsForMe("AString","BString");
const responseCode = finalResponse.status();
...owls all the way down...
});
It will literally just drop out at the point I've marked as I try to step over it in debug mode. The error reads: -
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 put all the individual calls into a test and run them that way that will also work, but I was hoping I wouldn't need to write 20 lines of code to run a bunch of the same API tests every single time I want to test something 90% of the way down the track...
I've omitted all the other methods/code that's not being reached as I figured its not applicable but if there's anything people think might be worth looking at let me know and I can update.
Any suggestions gratefully received!
Upvotes: 1
Views: 801
Reputation: 196227
The actual problem is that you do not await
at the var finalResponse = Group_API_Calls.doAllTheThingsForMe("AString","BString");
.
Since doAllTheThingsForMe
is async
it will return a promise. And if you want to use the actual returned value, you need to await
it first.
so
var finalResponse = await Group_API_Calls.doAllTheThingsForMe("AString","BString");`
Upvotes: 1