Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created April 29, 2022 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpscholar/c6c50e5c9dba391cb663567565a40e5f to your computer and use it in GitHub Desktop.
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
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