Created
April 29, 2022 20:25
-
-
Save wpscholar/c6c50e5c9dba391cb663567565a40e5f to your computer and use it in GitHub Desktop.
Cypress commands for conditionally logging into WordPress. Add to cypress/support/commands.js
This file contains hidden or 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('login', () => { | |
// Fetch username and password from the cypress.env.json file. | |
const username = Cypress.env('wpUsername'); | |
const password = Cypress.env('wpPassword'); | |
cy | |
.getCookies() | |
.then(cookies => { | |
let hasMatch = false; | |
cookies.forEach((cookie) => { | |
if (cookie.name.substr(0, 20) === 'wordpress_logged_in_') { | |
hasMatch = true; | |
} | |
}); | |
if (!hasMatch) { | |
cy.visit('/wp-login.php').wait(1000); | |
cy.get('#user_login').type(username); | |
cy.get('#user_pass').type(`${password}{enter}`); | |
} | |
}); | |
}); | |
Cypress.Commands.add('logout', () => { | |
cy | |
.getCookies() | |
.then( | |
cookies => { | |
cookies.forEach( | |
cookie => { | |
cy.clearCookie(cookie.name); | |
} | |
) | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment