cypress - 使用 Cypress 绕过 UI 登录

标签 cypress

我在绕过 UI 登录时遇到问题。我的 Web 应用程序不使用 API 来验证用户。没有像 /login 这样的端点. index.php只会打开登录页面并提交表单进行登录。

应用程序通过以下方式验证用户auth($_REQUEST['username'], $_REQUEST['password_tx']);
这是 cypress 在 UI 登录提交后打印的内容。

enter image description here

我不知道如何从这里继续前进。

    // 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 - 使用 Cypress 绕过 UI 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51878136/

相关文章:

javascript - 我无法在 Mac 上打开 Cypress : Cypress not found

javascript - Cypress :找不到模块:错误:无法解析 'cypress-wait-util'

javascript - Cypress.io 正在执行注释代码行

node.js - 在 Cypress 中模拟网络请求

testing - Protractor Vs Suitest vs Cypress 哪种框架更适合大型角度应用程序?

javascript - Cypress 拦截不匹配的网址

javascript - 如何使用 Cypress 获取 javascript 弹出窗口中的内容?

npm - "Cypress Run"的路径问题

javascript - Cypress .io : Server Error | 404 - File or director not found

reactjs - Cypress Github 操作失败