We want to automate testing for our analytics calls. I am currently evaluation front end test tools, that could be used for this. I found TestCafe Studio to be what we would need to create the tests, but first I need some proof of concept. Thus I try to code a simple test case first. But I struggle with what seems to be the most basic. I want to assert that some parameters in a request have a certain value. So what I did is to create a RequestHook
according to the documentation: https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/creating-a-custom-http-request-hook.html (what they omit is that you need to export your class if you place it in a separate file...)
I have two problems now:
- How can I wait for this request to be executed? The
await t
is only for the page I am requesting, but the analytics call is then executed later. - How do I provide the test with some data collected in the hook?
Here is how far I have gotten (I got the URL in the console):
import { RequestHook} from 'testcafe';
export class AnalyticsRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventConfigureOpts) {
super(requestFilterRules, responseEventConfigureOpts);
}
async onRequest (event) {
console.log(event.requestOptions.url);
}
async onResponse (event) {
}
}
I then instantiate this class with:
const analyticsRequestHook = new AnalyticsRequestHook(/https:\/\/trackinghost.com\//);
In some other examples, they just import t
as well and you should have access to it in the methods. But this seems not to work for RequestHooks, as I get the following error as soon as I try to access t
:
Cannot implicitly resolve the test run in the context of which the test controller action should be executed. Use test function's 't' argument instead.
But in onRequest
I can't pass other arguments.
So are my two questions even possible, if yes, please provide an example, as I am really a complete newbie with testcafe.