I have two requests: one is POST request and other is get. In first i get user access token by post and in other i used this accessToken to get login. My code does not work.
I am using window 7 and cypress 3.3.5
my code:
var value;
describe("Login operation", () => {
it("Login Request with post method", () => {
cy.request({
method:'POST',
url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
body: {
"userName": "faizanj",
"password": "abc"
}
})
.then(function(response){
this.value = response.body.accessToken;
console.log("Value "+this.value);
expect(response.body.name).to.equal('Faizan');
expect(response.status).to.equal(200);
});
});
it('Second test case', function() {
var authHeader='bearer ${'+this.value+'}';
const options = {
method: 'GET',
url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
headers:{
authorization:authHeader,
}};
cy.request(options)
.then((response)=>{
expect(response.status).to.equal(200);6+9
});
});
});
Under the authorization Tab, select Type as 'Basic Auth' and then add username and password. Click on the 'Send' button. Step 2: Once you get a '200', go to the 'Headers' section, and get the value of the 'authorization' header which is our basic token. Then finally, using cy.
Convert the response string into JSONObject and then again get the JSONObject for data and in data you can find your token object.
The problem is that you are trying using variable set between test cases when it's already reset to store token you need either use global variable (not advised), or create some login command that will be called before you need access to token. For example:
Cypress.Commands.Add('login', (userName, password) => {
cy.request({
method:'POST',
url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
body: {
userName,
password,
}
})
.as('loginResponse')
.then((response) => {
Cypress.env('token', response.body.accessToken); // either this or some global var but remember that this will only work in one test case
return response;
})
.its('status')
.should('eq', 200);
})
Then whenever you need you login user before using Cypress.env('token')
.
For example:
describe('testing token', () => {
beforeEach(() => {
cy.login();
});
it('test request', () => {
const token = Cypress.env('token');
const authorization = `bearer ${ token }`;
const options = {
method: 'GET',
url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
headers: {
authorization,
}};
cy.request(options)
.its('status')
.should('eq', 200);
})
});
You can go further and override all request to add token to them like this:
Cypress.Commands.overwrite('request', (originalFn, ...options) => {
const optionsObject = options[0];
const token = Cypress.env('token');
if (!!token && optionsObject === Object(optionsObject)) {
optionsObject.headers = {
authorization: 'Bearer ' + token,
...optionsObject.headers,
};
return originalFn(optionsObject);
}
return originalFn(...options);
});
then the above example would look like this:
describe('testing token', () => {
beforeEach(() => {
cy.login();
});
it('test request', () => {
cy.request(options)
.its('status')
.should('eq', 200);
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With