Skip to content

Instantly share code, notes, and snippets.

@roma-glushko
Created June 22, 2020 09:56
Show Gist options
  • Select an option

  • Save roma-glushko/25771f237ed03617a0cee38cb2bd5782 to your computer and use it in GitHub Desktop.

Select an option

Save roma-glushko/25771f237ed03617a0cee38cb2bd5782 to your computer and use it in GitHub Desktop.

Revisions

  1. Roman Glushko created this gist Jun 22, 2020.
    32 changes: 32 additions & 0 deletions commands.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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);
    })
    });
    })
    })