Cypress test need to receive a valid token and the

2019-07-26 06:34发布

问题:

I would need to access a MS website, enter the test email, then a pop up displays, select an account and enter password. Now at this stage, receives a valid token from the MS Website and then system will be redirected to another test_site. In this scenario, how can I redirected to that test_site using Cypress so that I can perform testing in that test site.

Below is my Commands.js getLoginToken()

var accessToken;
Cypress.Commands.add('getLoginToken', () => { 
  cy.request({
    method: 'POST',
    url: 'url_here',
    body: {
        email: 'yourEmail',
        password: 'yourPassword'
     }
  })
  .then((response) => {
    return new Cypress.Promise((resolve, reject) => {
      const rbody = (response.body);
      var tokenPattern = "(?i)\"access_token\":\\s*\"([^\"]*)\"";
      const authToken = rbody.access_token;
      accessToken = authToken;
      cy.log(accessToken);
      resolve(accessToken);
      return accessToken;
     })
  })

})

Cypress test :

describe('Navigate to test website and check the Home button', function(){
    before('Clear the cookies and run the test', ()=>{
        cy.clearCookies();
    }) 

    it('Verify whether the Home button is displaying in the test portal', function(){
      const newToken = cy.getLoginToken();
      cy.log(newToken);
      cy.visit(newToken);
    }) 
})