简体   繁体   中英

Cypress execute API calls outside of the browser

I created functions using axios that will setup the testing data before each test is run. They are located in a FOY.js file

const axios = require('axios');

//Get the token needed for Bearer Authorization
async function getJWT() {
    const bearerToken = await axios.post('https://www.example.com', {username: 'user', password: 'test1234'});
    return bearerToken.data.access_token
}

//Get the UserId from the email address.
async function getUserId(emailAddress) {
    var bearerToken = await getJWT();
    const userId = await axios.get('https://example.com/users/search?contains='+emailAddress+'', {'headers':{Authorization: 'Bearer '+bearerToken+''}});
    console.log(userId.data.users[0].id);
    return userId.data.users[0].id
}

//Delete a record for a user
async function TMDeleteFOY (emailAddress) {
    var bearerToken = await getJWT();
    var userId = await getUserId(emailAddress);
    const response = await axios.delete('https://example2.com/'+userId+'/record', {'headers':{Authorization: 'Bearer '+bearerToken+''}});
    return response.status
}
module.exports.TMDeleteFOY = TMDeleteFOY;
module.exports.TMUpdateFOY = TMUpdateFOY;

Using cy.task()

beforeEach(function() {
    cy.task('TMDeleteFOY', 'example@mail.com');
});

plugins/index.js

const FOY = require('../resetScripts/talentMine/FOY');

module.exports = (on, config) => {
    on('task', {
        'TMDeleteFOY': (emailaddress) => {
         return FOY.TMUpdateFOY(emailaddress);
        }
    })
};

You need to return something from your task code so that Cypress knows what to wait for, to know that your task is finished before running other code.

Check out the cy.task() documentation :

In the task plugin event, the command will fail if undefined is returned. This helps catch typos or cases where the task event is not handled.

To fix this, you just need to modify your task code so that a promise is returned. Right now, you aren't returning anything.

In your plugins/index.js :

const FOY = require('../resetScripts/talentMine/FOY');

module.exports = (on, config) => {
    on('task', {
        'TMDeleteFOY': (emailaddress) => {
            // CHANGED: return a promise so Cypress can wait for it
            return FOY.TMDeleteFOY(emailaddress);
        }
    })
}

In your FOY.js file (excluded irrelevant sections for brevity):

// start of your FOY.js...

//Delete a record for a user
async function TMDeleteFOY (emailAddress) {
    var bearerToken = await getJWT();
    var userId = await getUserId(emailAddress);
    // CHANGED: return this promise chain so Cypress can wait for it
    return await axios.delete('https://example2.com/'+userId+'/record', {'headers':{Authorization: 'Bearer '+bearerToken+''}});
}

// end of your FOY.js...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM