提问人:Jimmy 提问时间:4/17/2023 最后编辑:Jimmy 更新时间:4/18/2023 访问量:310
如何在其他 .js 文件中获取 cy.wrap 数据 赛普拉斯
How to get cy.wrap data in other .js file Cypress
问:
尝试将其放入命令 .js 中以创建一个在登录时生成令牌的通用函数。
const data = require('../fixtures/User_data.json')
Cypress.Commands.add('get_token', () => {
cy.api({
method: "POST",
url: "https://api-stg.sample.com/login",
body:
{
"mobile_number": "+91" + data.mobile,
"password": "p@ssw0rd"
}
})
.then((response) => {
const token = response.body.data.access_token.token
cy.log("Bearer token: " + token )
cy.wrap(token).as('token')
})
})
然后在我的测试规范文件中,
create_mobilenum () {
cy.get_token()
cy.api({
method: "POST",
url: "https://api-stg.sample.com/mobile_numbers",
headers: { 'Authorization': "Bearer " + ('@token') },
...
...
但是在 cypress dashboard 中,我无法登录,因为它在我的规范文件运行时返回。token is undefined
答:
1赞
Shirley.Manson
4/18/2023
#1
从我所读到的变量和别名来看,这不是使用别名的方法。
它们不像变量一样起作用,您需要提取值
cy.get('@token').then(token => {
cy.api({
...
headers: { 'Authorization': "Bearer " + token },
评论
0赞
Jimmy
4/18/2023
是的,我刚刚阅读了文档。感谢您提醒此:))
评论