Writing assertions
Leverage Momentic’s powerful assertions to verify any condition.
Momentic offers three types of assertions, giving you full control over how to test your application’s correctness:
Category | Description |
---|---|
Basic | Run prebuilt checks against a specific element or the entire page content. |
AI | Ask Momentic’s AI to verify any English statement about the current page. |
JavaScript | Execute arbitrary JavaScript code with access to the current page, executed results, and libraries. |
Basic assertions
Element and page checks are the fastest assertions in Momentic.
With Element Checks, you can check an element’s text content, attribute values (e.g. href
, value
, min
), or interactivity status (e.g. enabled
, editable
, visible
).
Target the element by providing either a CSS selector, or a plain English description for AI auto-healing capabilities.
With Page Checks, you can assert that the HTML content of the current page matches a certain condition. This is useful for checking for the presence of specific text on the page.
All basic assertions support a timeout option. If the assertion initially fails, it will be automatically retried up until the timeout elapses. As such, basic assertions can be extremely useful for waiting for page transitions and loading states to complete before proceeding with interactive actions.
AI assertions
Our AI can access HTML content from the entire page, not just the portion visible in the current visible area. However, our AI can only access visual information from the current visible area as it relies on a screenshot of the page. You can use scroll commands to ensure that the relevant content is visible before an assertion.
AI checks are English statements verified by Momentic’s AI. They are more powerful and reliable than expect
or assert
statements from traditional testing libraries.
Use AI checks to verify complex conditions without having to worry about syntax:
Category | Example |
---|---|
Presence (or absence) of text | Verify that a congratulations message is on the page |
Presence (or absence) of elements | Verify that the page shows 3 search results |
Logical statements | The publish date of this article is more than 30 days ago |
General state of page | There is no error message present on the page |
Your assertion can also reference visual properties. For example:
Category | Example |
---|---|
Color | Verify the buy button is blue |
Layout | The ‘XL’ button is the largest button on the page |
Imagery | There is a LinkedIn icon on the page |
Finally, you can use {{}}
expressions to inject dynamic content into your assertion. For example: the current page contains a link to {{ env.BASE_URL }}
. For more information, visit the Executing JavaScript page.
Data visualizations
Momentic supports high-level assertions about SVG or canvas-based data visualizations rendered by libraries such as D3, Hicharts, Recharts, and more. However, Momentic’s AI agents are not designed to perform complex calculations over a large number of data points. For math-intensive operations, we recommend using a JavaScript step.
To improve accuracy for graphical assertions:
- Maximize or expand your graph so that it takes up a majority of the screen. If your charting library does not have this functionality, you can try setting
document.body.style.zoom
manually using a JavaScript step. - Provide clear labels and units for all axes. Include grid lines when possible.
- For interactive visualizations, use Click or Hover steps to show tooltips and interact with data visualizations.
Prompting best practices
Over your first hours building on Momentic, you should develop a good “feel” for how our AI processes your assertion. In general, we recommend treating our AI like a new QA engineer joining your team. By following the best practices below, you should be able to instruct our AI to understand what correctness means on your app:
-
Keep assertions short and focused on a single topic.
The user is logged in and the user’s profile picture is in the top right corner and the table is populated.
The user is logged in.
-
Provide useful context and hints. To avoid misinterpretations, include information such as a specific part of the page the AI should focus on or the element type you are looking for.
Google is visible.
There is a Google logo in the middle of the page.
-
Avoid jargon and narrowly specific language. AI agents may be confused by non-standard terminology. In addition, descriptions that are too specific may prevent proper auto-healing.
The page contains a code-mirror div with the text ‘https://google.com’.
The page contains a text input that has a URL in it.
Unsupported assertions
The following types of assertions are currently not supported:
Category | Example |
---|---|
Assertions with commands | Expand the dropdown and then check that there are five options available. |
Assertions over time | After 5 seconds, a popup appears. |
Subjective assertions | The page looks great! |
Complex math | The average slope of the graph is 31.4. |
Complex conditionals | If the user is logged in, check that their profile photo is visible. Otherwise, check that there is a Google icon on the page. |
JavaScript assertions
You can use the JavaScript step to execute assertions that require coding features, such as conditionals, loops, math, and regular expressions. Any error thrown from a JavaScript step will automatically fail the test.
To access the page content and DOM functions directly, turn the execute in browser
option on. Your code will then run in the current page’s context. For example, the following code verifies that there is an element on the page with a UUID as its content:
Was this page helpful?