【问题标题】:Cypress execute API calls outside of the browser赛普拉斯在浏览器之外执行 API 调用
【发布时间】:2019-04-04 15:54:45
【问题描述】:

我使用 axios 创建了函数,这些函数将在每次测试运行之前设置测试数据。它们位于 FOY.js 文件中

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;

使用 cy.task()

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

插件/index.js

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

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

【问题讨论】:

  • 这些是您的真实网址吗?你需要https://www.example.com,而不是https:www.example.com...我刚刚用axios测试过,后者不起作用:runkit.com/flotwig/5ca629999486de0012adecd6
  • 这些不是真正的 URL
  • 好的。您应该知道您发布的这段代码将在浏览器上下文中运行,而不是在 Node.js 上下文中。要在 Node.js 上下文中运行它,请使用 cy.task()
  • @ZachBloomquist。我已经对 cy.task() 进行了更改,但仍然无法使其正常工作。我之前从来没有用过cy.task(),你能看看代码看起来是否正确吗?
  • 好的,分享你的代码

标签: node.js cypress


【解决方案1】:

您需要从您的任务代码中返回一些内容,以便 Cypress 知道要等待什么,在运行其他代码之前知道您的任务已完成。

查看cy.task() documentation

task插件事件中,如果返回未定义,命令将失败。这有助于捕捉错别字或未处理任务事件的情况。

要解决此问题,您只需修改任务代码以返回承诺。现在,你没有返回任何东西。

在你的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);
        }
    })
}

在您的 FOY.js 文件中(为简洁起见,排除了不相关的部分):

// 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...

【讨论】:

  • 我已将我的解决方案添加到我原来的问题中,但这正是我所需要的。谢谢
猜你喜欢
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 2020-01-09
相关资源
最近更新 更多