我测试的应用程序具有 SSO 登录功能。所以,第一个我是 cy.vist(xxx) 到一个 URL,然后系统引导我另一个 cy.visit(yyy)。但目前我无法处理我目前面临的问题。你能帮我弄清楚吗?
谢谢
it.only("logs in using env variables", () => {
const username = Cypress.env("username");
const password = Cypress.env("password");
let href;
cy.visit("/");
cy.get(".chakra-stack > .css-1n94901").click()
cy.contains("Login with Carmarket").click();
cy.get(":nth-child(1) > .form-control").type(username);
cy.get(":nth-child(2) > .form-control").type(password);
cy.get(".login > .submit > .button").click({multiple: true,force: true});
cy.url().then((url) => {
href = url;
cy.log("href ", href);
cy.visit(href);
cy.url().should("include", "blabla[][1]][1]"); //assertion of that we are in this url
});
});
});
最佳答案
点击登录按钮后,可以直接断言url是否包含一些文本。您还可以添加一些超时以完成重定向。
cy.url({timeout: 6000}).should("include", "blabla[][1]][1]")
考虑到您的网址是 https://example.com/
在同一测试中,如果网址的格式类似于 https://,您就可以使用它们example.com/something
或 https://example.com/something/123
或 https://superdomain.example.com/
。所以基本上 url 应该具有相同的来源。但如果 url 与您的情况不同,那么您必须进行新的测试来解决此问题。这是 cypress 的一种权衡,您可以从 here 阅读更多相关信息。 。所以你可以做这样的事情:
it('logs in using env variables', () => {
const username = Cypress.env('username')
const password = Cypress.env('password')
let href
cy.visit('/')
cy.get('.chakra-stack > .css-1n94901').click()
cy.contains('Login with Carmarket').click()
cy.get(':nth-child(1) > .form-control').type(username)
cy.get(':nth-child(2) > .form-control').type(password)
cy.get('.login > .submit > .button').click({multiple: true, force: true})
})
it('validate content of url', () => {
cy.url({timeout: 6000}).should('include', 'blabla[][1]][1]')
})
关于javascript - Cypress 中的 SSO 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69738395/