Created
June 22, 2020 09:56
-
-
Save roma-glushko/25771f237ed03617a0cee38cb2bd5782 to your computer and use it in GitHub Desktop.
Cypress - Magento2 loginAsCustomer() command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cypress.Commands.add("loginAsCustomer", (username, password) => { | |
cy.request('/customer/account/login') | |
.its('body') | |
.then((body) => { | |
const $html = Cypress.$(body) | |
const formKey = $html.find('input[name="form_key"]').val() | |
cy.request({ | |
method: 'POST', | |
url: '/customer/account/loginPost', | |
failOnStatusCode: false, // dont fail so we can make assertions | |
form: true, // we are submitting a regular form body | |
body: { | |
login: { | |
username, | |
password, | |
}, | |
form_key: formKey, // insert this as part of form body | |
}, | |
}) | |
.then((resp) => { | |
expect(resp.status).to.eq(200) | |
resp.headers['set-cookie'].forEach(cookie => { | |
const [nameValueString, ] = cookie.split(';') | |
const [cookieName, cookieValue] = nameValueString.split('=') | |
cy.setCookie(cookieName, cookieValue); | |
}) | |
}); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment