Skip to content

Instantly share code, notes, and snippets.

@shwetaneelsharma
Last active September 1, 2020 12:33
Show Gist options
  • Select an option

  • Save shwetaneelsharma/a25581a6e6f0594c1902a144be209828 to your computer and use it in GitHub Desktop.

Select an option

Save shwetaneelsharma/a25581a6e6f0594c1902a144be209828 to your computer and use it in GitHub Desktop.
Cypress blog
/**
* Create a User through Drush
* @param {*} user - String
* @param {*} password - String
*/
Cypress.Commands.add('createUser', (user, pass, role) => {
let drush = 'lando drush';
cy.exec(`${drush} user-create "${user}" --mail="${user}@example.com" --password="${pass}"`,
//Code will continue to execute if the given user account data already exists
{ failOnNonZeroExit: false }
);
cy.exec(
`${drush} user-add-role "${role}" "${user}"`,
{ failOnNonZeroExit: false }
);
cy.exec(`${drush} user-information "${user}"`);
//we didn’t explicitly set the failOnNonZeroExit property here and the test will fail
//if the given user account doesn’t exist.
});
/**
* Login with different user roles(considered: "administrator" and "editor")
*/
Cypress.Commands.add('login', (type) => {
let perms = {};
switch (type) {
case 'admin':
perms = {
name: Cypress.env('cyAdminUser'),
pass: Cypress.env('cyAdminPassword'),
};
break;
case 'editor':
perms = {
name: Cypress.env('cyEditorUser'),
pass: Cypress.env('cyEditorPassword'),
};
break;
}
return cy.request({
method: 'POST',
url: '/user/login',
form: true,
body: {
...perms,
form_id: 'user_login_form',
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment