如何在Cypress测试中实现自定义命令

如何在Cypress测试中实现自定义命令,cypress,cucumberjs,Cypress,Cucumberjs,我编写了一个自定义命令,从下面的窗口获取身份验证令牌 Cypress.Commands.add("getToken", AUTH => { return cy.window().then(window => window.localStorage.getItem(AUTH)); }); 我建议这样做: 描述(“”,()=>{ 设tokens={}; 它(“”,()=>{ 赛义德 .getToken('AUTH',({token})=>{ 赋值(标记,{auth:token});

我编写了一个自定义命令,从下面的窗口获取身份验证令牌

Cypress.Commands.add("getToken", AUTH => {
 return cy.window().then(window => window.localStorage.getItem(AUTH));
});

我建议这样做:

描述(“”,()=>{
设tokens={};
它(“”,()=>{
赛义德
.getToken('AUTH',({token})=>{
赋值(标记,{auth:token});
})
.请求({
标题:{“内容类型”:“应用程序/json”,授权:`Bearer${tokens.auth}`,}
})
});
});
此外,您还必须更改一点点
getToken
命令:

Cypress.Commands.add(“getToken”,(AUTH,cb)=>{
返回cy.window().then(window=>cb(window.localStorage.getItem(AUTH));
});

我想在测试
标题中的下面位置使用authtoken:{“内容类型”:“应用程序/json”,授权:`Bearer${authtoken.token},},
但是你想从cypress或application发送带有此标记的请求?当然是从cypress tests发送的,但是你使用的是
cy.request(url)
在您的测试中?是这样的
cy.request({method:“POST”,url:`${Cypress.env(“apiBaseUrl”)}/versions`,头:{“Content Type”:“application/json”,授权:`Bearer${authToken.token},},
Hi,我得到了类型错误:cb不是一个函数。我能知道这个cb的作用吗?请ECB代表回调,我忘记了第一个参数
AUTH
try now我编辑了应答我得到了401个请求错误,因为“授权”:“承载未定义”,嗯,请尝试添加
console.log(令牌)
object.assignconsole.log之前返回`{“令牌”:“Eyjrawqioijxyvwqxnbukjqn25iwgw3v2i4neo1tvptqwvltvaxdwl0zwdiyxy2s004az0ilcjhbgcioijsuzi1nij9.eyjzdwioiyingzgzogzmy0xmwq2ltrjogytowyzzc05zg5zgq0yyyyyxzjxjgwjgwntailbwwwwfffff92zxjpzzzm”,accountName:“英国”、“账户ID”:19645,“单一账户”:true}}
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.