使用Cypress绕过UI登录 [英] Bypass UI Login using Cypress

查看:16
本文介绍了使用Cypress绕过UI登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绕过UI登录时遇到问题。我的Web应用程序不使用API对用户进行身份验证。没有类似/login的终结点。index.php将只打开登录页并提交表单进行登录。

应用程序通过以下方式对用户进行身份验证 auth($_REQUEST['username'], $_REQUEST['password_tx']);

这是Cypress在UI登录提交后打印的内容。

我不知道如何继续前进。

    // This doesn't work. The application doesn't get the user details from the body. It is in the submitted form. 
    cy.request({
        method: 'POST',
        url: '/index.php?p=sys001',
        form: true, 
        body: {
            username: 'user',
            password_tx: 'pass'
        }
    })

推荐答案

这是该问题的完整测试用例。添加了注释以使其易于理解。

it("login via form spoof", () => {
cy.get("div#mDiv > form").invoke("attr", "action").then(($action) => { //get 
the attribute of 'action' and pass encoded uname and pwd to it
  let username = Cypress.env("username"); 
  let password = Cypress.env("password");

  cy.intercept("POST", $action, (req) => { //post request and populate body
    // intercepting the POST form to spoof it.
req.body = $action + encodeURIComponent(username)+ encodeURIComponent(password)
  })
    .as("loginForm"); //alias
});

cy.get("div#mDiv > div.login > form")
  .submit();  //Submit the form after locating it.
 });

这篇关于使用Cypress绕过UI登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆