Skip to main content

Guide: Using assertions in TestDriver

Assertions in TestDriver allow you to validate that your application behaves as expected during a test. By using the assert command and visual assertions, you can ensure that specific conditions are met, such as verifying the presence of text, images, or UI elements on the screen.

What are assertions?

Assertions are checks that validate whether a specific condition is true. If the condition isn’t met, the test will fail, providing feedback on what went wrong.

Types of assertions in TestDriver:

  1. Text Assertions: Verify that specific text is visible on the screen.
  2. Visual Assertions: Validate the presence of images, icons, or UI elements.
  3. Custom Assertions: Use descriptive conditions to check for specific outcomes.

How to use the assert command

The assert command is used to validate conditions during a test. It checks whether the specified expectation is true.

Syntax:

  • expect: The condition to validate (for example, “The login form is displayed”).
  • async: (Optional) If set to true, the test will continue running without waiting for the assertion to pass.

Example: Text assertion

TestDriver command:

This assertion checks if the login form is visible on the screen.

Example: Async assertion

TestDriver command:

This assertion runs asynchronously, allowing the test to continue without waiting for the success message to appear.

Visual assertions

Visual assertions validate the presence of images, icons, or UI elements on the screen. These are particularly useful for verifying non-text elements.

Example: Verifying an image

TestDriver command:

This command hovers over the company logo to ensure it’s present on the screen.

Example: Verifying a button

TestDriver command:

This command hovers over the “Submit” button to confirm its presence.

Combining assertions with other commands

Assertions can be combined with navigation and interaction commands to validate workflows.

Example: Login workflow with assertions

login.yaml

Debugging assertions

  1. Review Error Messages:
    • If an assertion fails, TestDriver provides detailed error messages to help identify the issue.
  2. Use Visual Feedback:
    • Leverage screenshots and visual feedback to verify the state of the application during the assertion.
  3. Refine Descriptions:
    • Ensure that the expect condition or description is specific and matches the application’s state.

Best practices for assertions

  1. Be Specific:
    • Use clear and concise conditions for assertions (for example, “The login form is displayed”).
  2. Use Visual Assertions for Non-Text Elements:
  3. Combine Assertions with Navigation:
    • Place assertions after navigation or interaction steps to validate the application’s state.
  4. Leverage Async Assertions:
    • Use async: true for non-blocking checks, especially for dynamic content.
  5. Test Incrementally:
    • Add assertions step-by-step to validate each part of the workflow.

By using the assert command and visual assertions effectively, you can create robust and reliable tests that ensure your application behaves as expected.