Wolfpack is designed to simplify and automate end-to-end testing. To identify and interact with web page elements, Wolfpack uses selectors, which are strings that pinpoint a specific element in the DOM (Document Object Model).
Selectors are crucial for ensuring automated tests are reproducible and robust. However, they can become fragile if the targeted elements change in the web application, particularly due to code updates or dynamic variations.
Wolfpack primarily uses two types of selectors: CSS selectors and XPath selectors. These selectors are automatically generated by the recorder but can be manually adjusted if necessary.
CSS selectors are based on the CSS language syntax and allow targeting of elements using their classes, IDs, tags, attributes, or combinations of these criteria.
#submit-button
: Selects an element with the ID “submit-button”..form-input
: Selects all elements with the class “form-input”.div.container > ul > li:first-child
: Targets the first item in a list within a “div” element with the class “container”.XPath (XML Path Language) is a more expressive and powerful syntax that allows navigation through the DOM hierarchy. Unlike CSS selectors, it can target elements based on their attributes or text content.
//button[@id='submit-button']
: Selects a button with the ID “submit-button”.//div[contains(@class, 'error-message')]
: Targets a “div” element containing the class “error-message”.//a[text()='Learn more']
: Finds a link with the text “Learn more”.When a test fails because an element cannot be found, it’s essential to diagnose the problem and fix the relevant selector. Here are the steps to follow:
Whenever an action is recorded, Wolfpack generates at least two or three selectors to identify the element. These selectors typically include a mix of CSS and XPath. If one selector doesn’t work, simply try using another one from the available options. This might resolve the issue without needing further adjustments.
To view and modify the selectors, open the test in the scenario editor and examine the suggested options for the relevant action.
Wolfpack records all user interactions during the session. However, not all actions are essential to validate a test scenario. Ask yourself:
Example: Clicking on a blank area of the page to trigger a focus loss might not be relevant to the primary scenario. Removing this action could resolve the error.
With modern frameworks like React, Angular, or Vue, the classes and IDs of elements can be dynamically generated. This means an element might:
Solution: Use more generic selectors or those based on stable attributes (like data-testid
) to target the elements.
Selectors automatically generated by the recorder can sometimes be overly precise. They may include a combination of classes or complicated paths within the DOM. Simplifying the selector can increase its robustness.
Generated Selector:
form > div:nth-child(3) > button.btn-primary.btn-lg
Simplified Selector:
button.btn-primary
The simplified selector is less sensitive to structural changes in the DOM while remaining functional.
Browser development tools make it easy to retrieve XPath or CSS selectors. To do this:
F12
or right-click > “Inspect”.
Tip: Test your selectors with document.querySelector
(CSS) or document.evaluate
(XPath) to validate their functionality before adding them to the test.
Some elements lack unique or stable attributes, making them hard to identify. In this case:
data-*
attributes to facilitate identification.Example:
Suppose a button has no ID or class but is located within a unique container:
//div[@id='unique-container']//button[text()='Submit']
This selector uses the container to indirectly target the button.
Fixing selectors is an essential skill for ensuring the stability of end-to-end tests in Wolfpack. By following these steps, you can diagnose the cause of errors, simplify your selectors, and create more robust tests. Remember, the main goal is to maximize the relevance and reliability of your scenarios while minimizing maintenance efforts.