使用 cypress 提示登录

Dan*_*nos 3 cypress

目前我找不到将用户名和密码插入提示窗口以使用 cypress 登录页面的方法:

登录提示窗口

有人可以帮我吗?欢迎任何帮助,谢谢!

小智 5

假设您的测试网站是 www.yourtestingwebsite.com 并且您的 username=username password=password
如果它们在加载您的测试站点时提示,您的脚本应该是这样的。cy.visit(' https://username:password@yourtestingwebsite.com ')

别的,

您可以只在 test.js 文件中使用 cy.visit ('/'),​​但请在您的 integration/command.js 文件中包含以下内容:

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) 
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options)             
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
    return false
});

Cypress.Commands.overwrite('visit', async (orig, url, options) => {
    let localBool = Cypress.config().baseUrl.includes('local');
    if (!localBool) {
        const auth = {
            username: 'username',
            password: 'password'
        };

        if (options) {
            options.auth = auth;
        } else {
            options = { auth };
        }
    }

    return await orig(url, options);
});
Run Code Online (Sandbox Code Playgroud)