Skip to main content

How to Assert?

You can easily check if your web application is setting the right URLs and returning the right content during your tests with Thunder Code.

Jihed Othmani avatar
Written by Jihed Othmani
Updated over a month ago

What You'll Learn

  • How to write Content assertions in your Test Cases

  • How to write URL assertions in your Test Cases

  • Best practices for writing assertions

Before You Start

  • Make sure you have an existing Test Case ready

How to Add an Assertion

  • Open your Test Case: Navigate to the Test Case where add some assertions.

  • Add a new step or create a new scenario:

    • Click Add a new step and type the description of the assertion, or

    • Describe what you want in chat.

  • Execute your Test Case: You can now execute your Test Case and let Thunder Code ensure you have the right URLs.

What can you Assert on?

In Thunder Code, you can assert on three main aspects of your web application:

1. Content Assertions

These assertions verify the actual content displayed on your webpage.

  • Text content: Check if specific text appears on the page Example: Assert that "Welcome back, User" appears on the dashboard

  • Element presence: Verify if certain elements exist Example: Assert that the shopping cart icon is visible

  • Element state: Check if elements are enabled/disabled Example: Assert that the submit button is enabled

  • Metadata: Verify page metadata like title and description Example: Assert that page title is "Product Catalog"

  • Element count: Check the number of specific elements Example: Assert that there are exactly 5 product cards displayed

  • Visual elements: Verify styling and appearance Example: Assert that “Catalog” is bold and highlighted

2. URL Assertions

These assertions verify different components of your URLs.

  • Protocol: Check the URL protocol Example: Assert that URL starts with "https://"

  • Full URL: Check the complete URL Example: Assert that URL is "https://example.com/products";

  • Path verification: Check the URL path Example: Assert that current path is "/products/category"

  • Query parameters: Verify URL parameters Example: Assert that URL contains "?sort=price&order=desc"

  • Hash fragments: Check URL hash values Example: Assert that URL hash is "#section-2"

3. Cookie Assertions

These assertions verify cookie-related behaviors.

  • Cookie presence: Check if specific cookies exist Example: Assert that "session_id" cookie exists

  • Cookie values: Verify cookie content Example: Assert that "language_preference" cookie is set to "en"

  • Cookie attributes: Check cookie properties Example: Assert that "user_token" cookie has HttpOnly flag

For more details, see How to Assert on Cookies?

Tips for Writing Effective Assertions

  • Focus on what matters: Only check components that are important for your scenario.

  • Provide Details: For better results, give a clear description of the assertion including the exact target and the expected state (Eg. Test Projects in the sidebar is visible and displayed in bold)

  • Be flexible with dynamic values: It's okay to give a general description of values that change often. Example: Ensure the URL contains /users/ and has a tab parameter.

Common Use Cases

Here are some typical scenarios where assertions are particularly useful:

  • Validate Successful Actions: Ensure interface elements update correctly after user actions Example: Assert that the shopping cart count increases after adding an item

  • Verify Interface Updates: Check if interface elements display correctly after making visual changes to your website

Example: Assert that new styling is applied correctly to navigation menu items

  • Check Error Handling: Verify appropriate error messages and states Example: Assert that an error message appears when submitting invalid data

  • Test Navigation Flow: Ensure proper page transitions and routing Example: Assert that the user is redirected to /login after logging out

Did this answer your question?