Testing Login Functionality with TestDriver

Test login functionality with TestDriver. This scenario demonstrates how to automate testing the login process for a web application using TestDriver.

Prerequisites

Before running the tests, ensure you have performed the following steps:

  1. If you haven’t already, signup for a Free Trial on the TestDriver website
  2. Install the TestDriver CLI globally using npm or just follow the VS Code Extension Setup Walkthrough:
npm install -g testdriverai
  1. Run the init command to set up the TestDriver configuration using the API key you got when you signed up for the trial:
testdriverai init
Now you are ready to run the tests!

Scenario overview

  1. Visit the login page of the web application.
  2. Enter the username and password into the respective fields (see [Reusable Snippets](/features/reusable-snippets#How-to Create-and-Use-Reusable-Snippets)).
  3. Click the “Login” button.
  4. Verify that the user is redirected to the dashboard or home page after a successful login.
  5. Optionally, check if the user is logged in by verifying the presence of a logout button or user profile information.

Setup your test environment with predefined credentials.

This example uses GitHub secret management to store credentials.

You can also use the exec command to fetch a test user dynamically from a database or API.

To use GitHub secrets, create or modify an existing .env file in the root of your project and add the following lines:

TD_TEST_USERNAME=your_test_username
TD_TEST_PASSWORD=your_test_password
  1. Create a test file and use the credentials like this:
version: 4.2.18
steps:
  - prompt: Log in to the application
    commands:
      - command: hover-text
        text: Email address
        description: email input field label
        action: click
      - command: type
        text: ${TD_USERNAME} # Use environment variable for username
      - command: hover-text
        text: Password
        description: password input field label
        action: click
      - command: type
        text: ${TD_PASSWORD} # Use environment variable for password
      - command: hover-text
        text: Log In
        description: log in button
        action: click
  1. Run the test using the command line:
testdriverai run login.yaml
  1. Watch replays in Your account

Conclusion

In this scenario, we demonstrated how to automate the login process for a web application using TestDriver. By leveraging reusable snippets and environment variables, you can create efficient and maintainable tests for your applications. This approach not only saves time but also ensures that your tests are easily adaptable to changes in the application or test data.