我尝试使用 cypress 进行自动化测试。 为了使应用程序正常工作,用户需要登录。 所以我尝试自动化这一步。
我编写了以下命令,并在其中传递了用户名密码。
Cypress.Commands.add('login', ( userId, pwd ) => {
const domain = 'XXXX'
const clientId = 'XXXX'
const clientSecret = 'XXXX'
const scope = 'XXXX'
cy.request({
method:'POST',
url:`https://login.microsoftonline.com/${domain}/oauth2/v2.0/token`,
header:
{
'cache-control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: true,
body: {
grant_type: 'password',
client_id: clientId,
client_secret: clientSecret,
password: pwd,
scope: scope,
username: userId
}
})
.then((resp)=>{
const token = resp.body['access_token']
console.log(token)
//cy.visit('http://{URL}/auth/#access_token='+token)
})
})
这样我就得到了错误消息。用户名或密码错误。
有人知道如何解决这个问题吗?或者知道使用 azure AD 的 SSO 设置 Cypress 的更好方法吗?
最佳答案
Azure AD ROPC flow应该可以,使用时请确保您使用的工作帐户格式为 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9de5e5e5dde5e5e5b3f2f3f0f4feeff2eef2fbe9b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>
,并且未启用 MFA,详细信息请参见下面的屏幕截图。如果您没有这样的账户,请关注this创建一个。
根据您的情况,请仔细检查用户名和密码,确保其正确。(如果正确,请确保实际请求中不存在转义问题。)
我在 postman 中测试了流程,工作正常。
关于azure - 如何设置 Azure AD 和 Cypress 以便 SSO 登录工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63775475/