Skip to content

Auth and Users

Paul English edited this page Jul 22, 2022 · 5 revisions

Default users

By default there are five users generated one for each default user role:

Admin

  • Username: admin
  • Password: password

Editor

  • Username: editor
  • Password: password

Author

  • Username: author
  • Password: password

Contributor

  • Username: contributor
  • Password: password

Subscriber

  • Username: subscriber
  • Password: password

Authentification

To save time on tests and so you don't have to perform any login/authentification logic, by default login will be bypassed and the user will be logged in as the admin user. If you wish to change user you can use the cypress command cy.switchUser('<username>') to programmatically switch users at specific points during your test.

Creating a user

You can generate other users when seeding your database with test data. You can make use of the existing User fixture or perform any custom logic that may required.

An example of using a fixture:

To learn more about using a seed see Seeds and Fixtures

public function run() {
    $user = new Fixtures\User( [
        'role'         => 'admin',
        'user_login'   => 'exampleuser',
        'user_pass'    => 'password123',
        'display_name' => $this->faker->name(),
        'first_name'   => $this->faker->firstName(),
    ] );

    $user->create( 1 );
}

Then to log in as this user in your cypress tests you can use the switchUser command:

cy.switchUser('exampleuser');

Manual User Switching

In some cases you may have situations where the login needs to go through the full process to execute any hooks or background processes as part of your site. The standard user switching method above will not work for these requirements. Instead a manual login will be required, forcing WP-Cypress to run through the wp-login.php rather than bypassing the authentication as outlined above. A manual login can simply be achieved by adding a password as the optional second parameter to cy.switchUser().

cy.switchUser('exampleuser', 'password123');