Here is how you do it. It took me HOURS, - but this works for me and it's thoroughly tested. I did swap out the variables below (obviously).
I made a bunch of Commands, to swiftly be able to log in/out, between tests.
WARNING!!!!!
Remember to add adminUserLoginCookiesFromCypress.json to you .gitignore - and don't upload it to your server. If that gets in the wrong hands, then someone would be able to login as your user (by adding the cookies to their own browser).
Env
This content goes in cypress.json in the project root.
{
"env": {
"baseUrl": "https://s1.demo.opensourcecms.com/wordpress",
"dashboardUrl": "https://s1.demo.opensourcecms.com/wordpress/wp-admin",
"domain": "s1.demo.opensourcecms.com/wordpress",
"users": {
"admin": {
"username": "opensourcecms",
"email": "test@example.org",
"pw": "opensourcecms"
}
}
},
}
Commands
Clear cookies
For some reason, I couldn't (and can't) get cy.clearCookies() to work.
If I run a test that logs into WP, then it's still logged, when the test runs the second time. Even if I run cy.clearCookies() first thing (?!)
This manual/specific clearCookie-Command worked for me:
Cypress.Commands.add( "clearWordPressCookies", () => {
cy.clearCookie( 'wordpress_a8b94154380982c3184a469b8aa525c6' );
cy.clearCookie( 'wordpress_a8b94154380982c3184a469b8aa525c6' );
cy.clearCookie( 'wordpress_logged_in_a8b94154380982c3184a469b8aa525c6' );
cy.clearCookie( 'wordpress_test_cookie' );
});
Remember to find the actual cookie-names for your site and replace the hash. The reason for the hash in the cookie-name is due to multisite purposes
Get cookies
Saves the cookies to a file (in the project root).
Cypress.Commands.add( "getWordPressCookies", () => {
cy.getCookies()
.then( (cookies) => {
cy.writeFile( 'adminUserLoginCookiesFromCypress.json', cookies );
});
});
Set cookies
Sets the cookies from the saved file (from the Get cookies-function).
Cypress.Commands.add( "setWordPressCookies", () => {
cy.readFile( 'adminUserLoginCookiesFromCypress.json' )
.then( (cookies) => {
cookies.forEach( (cookie) => {
// cy.log( JSON.stringify( cookie ) ); // See the cookie contents
cy.setCookie( cookie.name, cookie.value, {
domain: Cypress.env('domain'),
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expiry: cookie.expiry
});
});
});
});
Manual login
This has to be done the first time, to get WordPress to generate the cookies.
Cypress.Commands.add( "manualWordPressLogin", () => {
cy.clearWordPressCookies();
cy.visit( Cypress.env('dashboardUrl') );
cy.get('#user_login').wait(200).type( Cypress.env('users').admin.username , { force: true } );
cy.get('#user_pass').wait(200).type( Cypress.env('users').admin.pw, { force: true } );
cy.get('#wp-submit').click();
cy.get('h1').contains( 'Dashboard' );
});
Example usage
context( 'Login, set and prep cookies' , function () {
it( 'Ensure no one is logged in', function() {
cy.clearWordPressCookies();
cy.visit( Cypress.env('dashboardUrl') );
cy.location('pathname').should('eq', '/wp-login.php' ); // Not logged in
});
it( 'Logs in a admin user', function(){
cy.manualWordPressLogin();
cy.getWordPressCookies();
cy.visit( Cypress.env('dashboardUrl') );
cy.location('pathname').should( 'match', /^\/wp-admin/ );
});
it( 'logs out the user - and logs back in using setting wp-cookies', function(){
cy.clearWordPressCookies();
cy.visit( Cypress.env('dashboardUrl') );
cy.location('pathname').should( 'match', /^\/wp-login\.php/ ); // Not logged in
cy.setWordPressCookies();
cy.visit( Cypress.env('dashboardUrl') );
cy.location('pathname').should( 'match', /^\/wp-admin/ ); // Is logged in
});
});