【发布时间】: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(),你能看看代码看起来是否正确吗?
-
好的,分享你的代码