Skip to content

Instantly share code, notes, and snippets.

@roma-glushko
Created June 22, 2020 09:56
Show Gist options
  • Save roma-glushko/25771f237ed03617a0cee38cb2bd5782 to your computer and use it in GitHub Desktop.
Save roma-glushko/25771f237ed03617a0cee38cb2bd5782 to your computer and use it in GitHub Desktop.
Cypress - Magento2 loginAsCustomer() command
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