cypress - 如何在赛普拉斯测试中实现自定义命令
问题描述
我编写了一个自定义命令来从如下窗口获取身份验证令牌
Cypress.Commands.add("getToken", AUTH => {
return cy.window().then(window => window.localStorage.getItem(AUTH));
});
const authToken = JSON.parse(window.localStorage.getItem("AUTH"));
authToken = returned the authtoken. I want to know how to make`enter code here` this as
function/custom command so that the other t`enter code here`est could use this.
解决方案
我建议这样的事情:
describe('', () => {
let tokens = {};
it('', () => {
cy
.getToken('AUTH', ({ token }) => {
Object.assign(tokens, { auth: token });
})
.request({
headers: { "Content-Type": "application/json", Authorization: `Bearer ${tokens.auth}`, }
})
});
});
您还必须更改一点getToken
命令:
Cypress.Commands.add("getToken", (AUTH, cb) => {
return cy.window().then(window => cb(window.localStorage.getItem(AUTH)));
});
推荐阅读
- powerapps - Power Automate 将成员添加到组
- ios - 如何处理 React Native iOS 中的内存警告?
- neo4j - 根据 Cypher neo4j 中的属性值总和创建新关系?
- python - 可以模拟图书馆吗?
- r - nanotime:如何处理 tibble 中的 NA?
- r - 如何在 R 中堆叠我们的数据
- web-services - 可以从远程 iis 浏览 Web 服务,但不能在 Internet 上浏览
- visual-studio-code - 配置设置在 VSCode 中的什么位置?
- java - 如何在 HandlerInterceptor 中读取请求正文?
- javascript - 删除某些单词直到一组不同的单词