How to enable Authentication? #27
Comments
Hey @KyleCraviotto, it is not possible to set access the fixture or test object directly. Please write a custom request hook and manipulate the request's credentials from there. Before('@requestWithCredentials', async t => {
t.addRequestHooks(AddAuthCredentialsHook);
}); |
@Lukas-Kullmann Could I please request an example of an auth credential hook? :) |
Sure: import { RequestHook } from 'testcafe';
class AddAuthCredentialsHook extends RequestHook {
constructor(username, password) {
super();
this.username = username;
this.password = password;
}
async onRequest (event) {
if (event.isAjax) {
event.requestOptions.credentials.username = this.username;
event.requestOptions.credentials.password = this.password;
}
}
async onResponse () {}
}
Before('@requestWithCredentials', async t => {
const addAuthCredentiasHook = new AddAuthCredentialsHook('user', 'pw');
t.addRequestHooks(addAuthCredentialsHook);
}); Disclaimer: I did not test if this actually works, but this is what the documentation would suggest so it might be a good starting point. :) |
Thanks you :) It gives me a better understanding how it works Sadly, I am not able to get it to work :(
This is what I have based on what you suggested:
Any ideas? |
I removed
|
Maybe try async onRequest(event) {
event.requestOptions.credentials = {
...event.requestOptions.credentials,
username: this.username,
password: this.password
};
} Plus I don't think you need to set the URL. |
I LOVE YOU! |
I need to be able to add Authentication to the fixture and can't figure out how to do this.
Are there any fixture hooks or other ways to handle this?
fixture.httpAuth({ username: 'user', password: 'pw'});
The text was updated successfully, but these errors were encountered: