Principle:Webdriverio Webdriverio Browser Navigation and Interaction
Overview
Browser Navigation and Interaction encompasses the techniques for navigating to web pages and querying DOM elements within an automated browser session. These are the core operations of browser automation -- moving between pages and manipulating or inspecting page content. Together, navigation and element interaction form the backbone of every browser automation test.
Description
Browser navigation loads URLs into the controlled browser, while element interaction involves locating DOM nodes using CSS selectors, XPath, or other strategies and performing actions on them (click, type, read text). These are the core operations of browser automation -- moving between pages and manipulating/inspecting page content.
The two operation categories are:
- URL Loading: Directing the browser to a specific URL, either absolute or relative to a configured base URL.
- Page Load Waiting: Determining when the page has finished loading (none, interactive, complete, or networkIdle states).
- Request/Response Inspection: Capturing metadata about the navigation request such as response status, headers, and redirect chains.
Element Interaction
- Element Location: Finding DOM nodes using various selector strategies -- CSS selectors, XPath expressions, text content, accessibility IDs, or custom strategy functions.
- Single Element Queries: Returning the first matching element for a selector.
- Multiple Element Queries: Returning all matching elements as an iterable collection.
- Element Chaining: Composing element queries without intermediate
awaitcalls for fluent DOM traversal. - Action Execution: Performing actions on located elements such as clicking, typing, reading text, and checking visibility.
Usage
Use these techniques after establishing a browser session. Navigation is needed to load the application under test. Element querying is needed to interact with and assert on page elements.
When to use:
- After calling
remote()or after the testrunner has initialized the browser instance - To load the application under test via its URL
- To locate, interact with, and assert on specific DOM elements
- To traverse complex DOM structures using chained selectors
When not to use:
- Before a browser session has been established (commands will fail with "invalid session id")
- For non-browser automation tasks (e.g., API testing without a browser)
Theoretical Basis
The W3C WebDriver protocol defines navigation commands and element location strategies:
- POST /session/{id}/url: Navigates the browser to a given URL. The server loads the URL in the current top-level browsing context.
- The protocol respects the pageLoadStrategy capability (none, eager, normal) to determine when navigation is considered complete.
Element Location Protocol
- POST /session/{id}/element: Finds a single element using a location strategy. The request body contains
"using"(the strategy) and"value"(the selector expression). - POST /session/{id}/elements: Finds all matching elements using the same strategy/value pattern.
- POST /session/{id}/element/{elementId}/element: Finds a child element within a parent element.
WebdriverIO Enhancements
WebdriverIO enhances the raw protocol with:
- The
$(single element) and$$(multiple elements) shorthand selectors. - Support for CSS, XPath, text content, accessibility ID, tag name, and custom function strategies.
ChainablePromiseElement: The$command returns a chainable promise, enabling fluent chaining without intermediateawaitcalls (e.g.,await $$('div')[1].nextElement().$$('img')[2].getAttribute('src')).- Async iterators: The
$$command returns aChainablePromiseArraysupportingfor await...ofloops. - Deep element lookup: When using WebDriver BiDi, element queries perform deep lookups across shadow DOM boundaries.
Related Pages
Implementation:Webdriverio_Webdriverio_Url_Dollar_DollarDollar_Commands
- implemented_by Implementation:Webdriverio_Webdriverio_Url_Dollar_DollarDollar_Commands - The
url(),$(), and$$()commands that concretely implement navigation and element interaction. - Heuristic:Webdriverio_Webdriverio_Click_Interception_Workaround
- Heuristic:Webdriverio_Webdriverio_Mobile_Platform_Differences