我在绕过UI登录时遇到问题。我的Web应用程序不使用API对用户进行身份验证。没有像/login
这样的端点。 index.php
只会打开登录页面并提交表单进行登录。
应用程序通过以下方式对用户进行身份验证
auth($_REQUEST['username'], $_REQUEST['password_tx']);
这是提交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'
}
})
答案 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.
});