向Cypress中的所有请求添加基本身份验证 [英] Adding basic auth to all requests in Cypress
本文介绍了向Cypress中的所有请求添加基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cy.visit()
调用添加基本身份验证。
身份验证凭据取决于部署(即,它们特定于我们在环境配置文件中设置的"baseUrl")。
目前,我有;
cy.visit("/", {
auth: {
username: '...',
password: '...'
}
});
我想要的是将‘auth’对象移动到EVG配置文件,这样我只需要规范中的cy.visit("/")
。
非常感谢
推荐答案
编辑:由于您的语法正确,我将仅分享我在任务中使用的方法。
如果您的身份验证工作正常,您可以创建自定义命令-visitAndAuthorise,例如:
Cypress.Commands.add("shopAdmin_visitAndLogin", (url) => {
cy.log('shopAdmin_visitAndLogin')
cy.visit(url)
cy.get('[data-qa="emailInput"]')
.type(Cypress.env('credentials').email)
cy.get('[data-qa="passwordInput"]')
.type(Cypress.env('credentials').password)
cy.get('[data-qa="loginButton"]')
.click()
cy.get('[data-qa="logOutButton"]')
.should('be.visible')
})
并且您的cypress.env.json文件需要包含如下凭据的对象:
{
"credentials": {
"email": "myEmail@gmail.com",
"password": "myPassword"
}
}
或遵循您的语法:
Cypress.Commands.add("shopAdmin_visitAndLogin", (url) => {
cy.log('shopAdmin_visitAndLogin')
cy.visit(url, {
auth: {
username: Cypress.env('credentials').username,
password: Cypress.env('credentials').password
}})
})
这篇关于向Cypress中的所有请求添加基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文