使用赛普拉斯绕过UI登录

时间:2018-08-16 13:22:27

标签: cypress

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

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

这是提交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'
        }
    })

2 个答案:

答案 0 :(得分:0)

这里是set a token in localStorage before the test in Cypress.

解决方案的链接

对于我的测试,我没有使用localstorage,我只是访问登录URL,并在每次测试之前输入用户名和密码。 我使用这种方式:

const login = () => { cy.visit('http://0.0.0.0:8080/#/login'); cy.get('#username').type('username'); cy.get('#password').type('1234password$'); cy.get('#login-button').click(); }

describe('UI', () => { 
// beforeEach(login); 
  beforeEach(() => { login();      Cypress.Cookies.preserveOnce('session_id',    'remember_token'); });

});

希望可以帮助您。

答案 1 :(得分:0)

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

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.

});

相关问题