Skip to content

Commit f83c12a

Browse files
authored
[Editorial review] BiDi - add pages for session module's subscribe, unsubscribe commands and a how-to (mdn#43343)
* adds session.subscribe page * edits for better phrasing * adds session.unsubscribe page, plus consistency edits to subscribe page * updates webdriver landing page * adds how to page * adds how-to to sidebar * fixes tech review comments * minor updates * address tech review comments * address tech review comments * tweak how-to intro * add links for existing capability pages * Apply suggestions from code review Co-authored-by: Dipika Bhattacharya <dipika@foss-community.org> * fix lint errors
1 parent f06cc15 commit f83c12a

File tree

10 files changed

+383
-47
lines changed

10 files changed

+383
-47
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Create a WebDriver BiDi connection
3+
short-title: Create a BiDi connection
4+
slug: Web/WebDriver/How_to/Create_BiDi_connection
5+
page-type: how-to
6+
sidebar: webdriver
7+
---
8+
9+
The client and the browser communicate using the WebDriver BiDi protocol over a WebSocket connection. There are two ways a client can establish this connection.
10+
11+
In one method, when creating a classic WebDriver session, the WebDriver client sets the [`webSocketUrl`](/en-US/docs/Web/WebDriver/Reference/Capabilities/webSocketUrl) capability to `true` to request BiDi to be enabled; the client then starts the browser with the WebSocket port open.
12+
13+
In the other method, the WebDriver client starts the browser from the command line by passing the required flag and the desired port. This method works with Firefox directly; however, Chromium-based browsers require the additional Chromium BiDi wrapper package. The sections in this article walk you through this method using Firefox.
14+
15+
## Launching the browser
16+
17+
To use WebDriver BiDi, you need to enable it in the browser by launching it with the `--remote-debugging-port` flag. The browser then listens for incoming WebSocket connections on the specified port. Port `9222` is a conventional default for browser debugging, but you can use any available port, or specify `0` to let the system assign a free port automatically.
18+
19+
```bash
20+
firefox --remote-debugging-port 9222
21+
```
22+
23+
On macOS, `firefox` may not be in your PATH. In that case, use the full path instead:
24+
25+
```bash
26+
/Applications/Firefox.app/Contents/MacOS/firefox --remote-debugging-port 9222
27+
```
28+
29+
## Getting the WebSocket URL
30+
31+
Firefox exposes the BiDi WebSocket directly at:
32+
33+
```plain
34+
ws://127.0.0.1:PORT/session
35+
```
36+
37+
For example, if you launched Firefox with `--remote-debugging-port 9222`, the URL is `ws://127.0.0.1:9222/session`. If you specified port `0`, check the `stderr` output for a message like `WebDriver BiDi listening on ws://127.0.0.1:46249` to find the assigned port.
38+
39+
> [!NOTE]
40+
> Firefox uses `127.0.0.1` rather than `localhost` for the BiDi WebSocket endpoint.
41+
42+
## Connecting to the WebSocket endpoint
43+
44+
With the WebSocket URL, use any WebSocket client to open a connection. Common options include the `ws` package for Node.js and the `websockets` package for Python.
45+
46+
Once connected, you can send WebDriver BiDi [commands](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules#commands) as JSON messages and receive responses and [events](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules#events) from the browser. See [`session.new`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/new) to create a BiDi session after connecting.
47+
48+
## See also
49+
50+
- [WebDriver BiDi reference](/en-US/docs/Web/WebDriver/Reference/BiDi)
51+
- [`session.new`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/new) command
52+
- [WebSockets API](/en-US/docs/Web/API/WebSockets_API)
53+
- [Chromium BiDi wrapper](https://github.com/GoogleChromeLabs/chromium-bidi)
54+
- [WebDriver BiDi web client](https://firefox-dev.tools/bidi-web-client/web/)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: WebDriver how to
3+
short-title: How to
4+
slug: Web/WebDriver/How_to
5+
page-type: listing-page
6+
sidebar: webdriver
7+
---
8+
9+
This page lists how-to guides for WebDriver.
10+
11+
{{SubpagesWithSummaries}}

files/en-us/web/webdriver/index.md

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,32 @@ page-type: landing-page
55
sidebar: webdriver
66
---
77

8-
WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.
8+
WebDriver is a browser automation interface that allows external programs to remotely inspect and control a browser. It is a platform- and language-neutral wire protocol, supporting both command-based automation and real-time, event-driven communication.
99

10-
To have the ability to write instruction sets that can be run interchangeably in many browsers on different platforms is critical to deliver a consistent experience to users. With the new wave of developments on the web platform, the increase diversity in devices and demands for real interoperability between the technologies, WebDriver provides tooling for [cross-browser testing](/en-US/docs/Learn_web_development/Extensions/Testing/Introduction).
10+
WebDriver provides tooling for [cross-browser testing](/en-US/docs/Learn_web_development/Extensions/Testing/Introduction), helping teams deliver consistent user experiences across many browsers and platforms. It is intended to allow web authors to write tests to automate a browser from a separate controlling process.
1111

12-
Provided is a set of interfaces to discover and manipulate DOM elements in web documents and to control the behavior of a user agent. It is primarily intended to allow web authors to write tests that automate a user agent from a separate controlling process, but may also be used in such a way as to allow in-browser scripts to control a — possibly separate — browser.
13-
14-
## Usage
15-
16-
So what does WebDriver let you do and what does it look like? Since WebDriver is programming language neutral, the answer to this question depends on which WebDriver client you're using and the choice of language.
17-
18-
But using a popular client written in Python, your interaction with WebDriver might look like this:
19-
20-
```python
21-
from selenium import webdriver
22-
from selenium.webdriver.common.by import By
23-
from selenium.webdriver.common.keys import Keys
24-
from selenium.webdriver.support.ui import WebDriverWait
25-
from selenium.webdriver.support.expected_conditions import presence_of_element_located
26-
27-
with webdriver.Firefox() as driver:
28-
29-
driver.get("https://google.com/ncr")
30-
wait = WebDriverWait(driver, 10)
31-
driver.find_element(By.NAME, "q").send_keys(f"cheese{Keys.RETURN}")
32-
wait.until(presence_of_element_located((By.XPATH, '//*[@id="rcnt"]')))
33-
results = driver.find_elements(By.XPATH, "//a[@href]")
34-
35-
for i, elem in enumerate(results):
36-
print(f'#{i} {elem.text} ({elem.get_attribute("href")})')
37-
```
38-
39-
This might produce output akin to this:
40-
41-
```plain
42-
#1 Cheese - Wikipedia (https://en.wikipedia.org/wiki/Cheese)
43-
```
12+
WebDriver is available in two variants: [WebDriver classic](/en-US/docs/Web/WebDriver/Reference/Classic) that uses HTTP, and [WebDriver BiDi](/en-US/docs/Web/WebDriver/Reference/BiDi) that uses WebSocket and enables bidirectional, event-driven communication.
4413

4514
## Reference
4615

4716
Browse the complete [WebDriver reference](/en-US/docs/Web/WebDriver/Reference) documentation.
4817

49-
### WebDriver classic reference
18+
- [WebDriver BiDi](/en-US/docs/Web/WebDriver/Reference/BiDi)
19+
- : Reference for WebDriver BiDi modules, commands, and events.
5020

51-
- [Commands](/en-US/docs/Web/WebDriver/Reference/Classic/Commands)
52-
- : Reference for all WebDriver classic commands.
21+
- [WebDriver classic](/en-US/docs/Web/WebDriver/Reference/Classic)
22+
- : Reference for WebDriver classic commands and timeouts.
5323

5424
- [Capabilities](/en-US/docs/Web/WebDriver/Reference/Capabilities)
55-
- : Reference for all WebDriver classic capabilities.
25+
- : Reference for WebDriver capabilities.
5626

5727
- [Errors](/en-US/docs/Web/WebDriver/Reference/Errors)
58-
- : Reference for WebDriver classic errors.
59-
60-
- [Timeouts](/en-US/docs/Web/WebDriver/Reference/Classic/Timeouts)
61-
- : Reference for WebDriver classic timeouts.
28+
- : Reference for WebDriver errors.
6229

6330
## Specifications
6431

6532
- [WebDriver](https://w3c.github.io/webdriver/)
33+
- [WebDriver BiDi](https://w3c.github.io/webdriver-bidi/)
6634

6735
## See also
6836

files/en-us/web/webdriver/reference/bidi/modules/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ The following is a sample event message sent by the browser when the client is s
6565
## Browser compatibility
6666

6767
{{Compat}}
68+
69+
## See also
70+
71+
- [WebDriver BiDi web client](https://firefox-dev.tools/bidi-web-client/web/)

files/en-us/web/webdriver/reference/bidi/modules/session/end/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The `result` field in the response is an empty object (`{}`).
3434

3535
### Ending an automation session with the browser
3636

37-
With a WebDriver BiDi connection established, send the following message to end the current session:
37+
With a [WebDriver BiDi connection](/en-US/docs/Web/WebDriver/How_to/Create_BiDi_connection) established, send the following message to end the current session:
3838

3939
```json
4040
{

files/en-us/web/webdriver/reference/bidi/modules/session/new/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The `params` field contains:
4141

4242
The `alwaysMatch` and `firstMatch` objects can include the following features:
4343

44-
- `acceptInsecureCerts` {{optional_inline}}
44+
- [`acceptInsecureCerts`](/en-US/docs/Web/WebDriver/Reference/Capabilities/acceptInsecureCerts) {{optional_inline}}
4545
- : A boolean that indicates whether untrusted TLS certificates (for example, self-signed or expired) are accepted for the duration of the session.
4646
- `browserName` {{optional_inline}}
4747
- : A string that specifies the name of the browser to use (for example, `"firefox"` or `"chrome"`).
@@ -62,7 +62,7 @@ The following fields in the `result` object of the response describe the charact
6262
- : A string that contains the unique identifier for the newly created session.
6363
- `capabilities`
6464
- : An object that describes the capabilities that were negotiated and are active for the session. It includes the following fields:
65-
- `acceptInsecureCerts`
65+
- [`acceptInsecureCerts`](/en-US/docs/Web/WebDriver/Reference/Capabilities/acceptInsecureCerts)
6666
- : A boolean that indicates whether untrusted TLS certificates (for example, self-signed or expired) are accepted for the duration of the session.
6767
- `browserName`
6868
- : A string that contains the name of the browser.
@@ -78,7 +78,7 @@ The following fields in the `result` object of the response describe the charact
7878
- : An object that describes the active proxy configuration. An empty object (`{}`) indicates no proxy is configured.
7979
- `unhandledPromptBehavior` {{optional_inline}}
8080
- : An object that describes the default behavior when a user prompt (such as an `alert`, `confirm`, or `prompt` dialog) is encountered during a command. This field is present only when specified in the `capabilities` parameter.
81-
- `webSocketUrl` {{optional_inline}}
81+
- [`webSocketUrl`](/en-US/docs/Web/WebDriver/Reference/Capabilities/webSocketUrl) {{optional_inline}}
8282
- : A string that contains the WebSocket URL for the session.
8383

8484
The browser may also return vendor-specific capabilities prefixed with a browser identifier (for example, `moz:buildID` for Firefox).

files/en-us/web/webdriver/reference/bidi/modules/session/status/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The `result` object in the response with the following fields:
4141

4242
### Checking browser status before creating a session
4343

44-
With a WebDriver BiDi connection established, send the following message to check whether the browser is ready to create a new session:
44+
With a [WebDriver BiDi connection](/en-US/docs/Web/WebDriver/How_to/Create_BiDi_connection) established, send the following message to check whether the browser is ready to create a new session:
4545

4646
```json
4747
{
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: session.subscribe command
3+
short-title: session.subscribe
4+
slug: Web/WebDriver/Reference/BiDi/Modules/session/subscribe
5+
page-type: webdriver-command
6+
browser-compat: webdriver.bidi.session.subscribe
7+
sidebar: webdriver
8+
---
9+
10+
The `session.subscribe` [command](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules#commands) of the [`session`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session) module registers the client to receive events asynchronously, either per event or per module, globally or scoped to specific contexts.
11+
12+
## Syntax
13+
14+
```json-nolint
15+
{
16+
"method": "session.subscribe",
17+
"params": {
18+
"events": ["<event name>"]
19+
}
20+
}
21+
```
22+
23+
### Parameters
24+
25+
The `params` field contains:
26+
27+
- `events`
28+
- : An array of one or more event name strings. Use a module name (for example, `"log"`) to subscribe to all events in that module or a specific event name (for example, `"log.entryAdded"`) to subscribe to only that event.
29+
- `contexts` {{optional_inline}}
30+
- : An array of one or more context IDs ([UUIDs](/en-US/docs/Glossary/UUID)), each corresponding to a tab or frame.
31+
If specified, events are received only for those contexts and their descendants.
32+
If the context ID corresponds to a frame, the subscription is created for the top-level context (tab) that owns the frame.
33+
34+
This field cannot be used if `userContexts` is also specified.
35+
36+
- `userContexts` {{optional_inline}}
37+
- : An array of one or more user context IDs, each corresponding to a browser context or container.
38+
If specified, events are received only for those user contexts.
39+
40+
This field cannot be used if `contexts` is also specified.
41+
42+
If neither `contexts` nor `userContexts` is provided, the subscription is global, so events are received for all contexts.
43+
44+
### Return value
45+
46+
The `result` field in the response is an object with the following field:
47+
48+
- `subscription`
49+
- : A string that contains the unique identifier for this subscription.
50+
51+
### Errors
52+
53+
- [`invalid argument`](/en-US/docs/Web/WebDriver/Reference/Errors/InvalidArgument)
54+
- : Thrown in any of the following cases:
55+
- The `events` array is empty, omitted, or contains an unrecognized event name.
56+
- `contexts` or `userContexts` is provided but empty.
57+
- Both `contexts` and `userContexts` are provided in the same request.
58+
- A parameter value has an invalid type.
59+
60+
## Examples
61+
62+
### Subscribing to an event globally
63+
64+
With a [WebDriver BiDi connection](/en-US/docs/Web/WebDriver/How_to/Create_BiDi_connection) and an [active session](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/new), send the following message to subscribe to the `log.entryAdded` event for all contexts:
65+
66+
```json
67+
{
68+
"id": 2,
69+
"method": "session.subscribe",
70+
"params": {
71+
"events": ["log.entryAdded"]
72+
}
73+
}
74+
```
75+
76+
The browser responds with a subscription ID as follows:
77+
78+
```json
79+
{
80+
"id": 2,
81+
"type": "success",
82+
"result": {
83+
"subscription": "c7b7b3a2-1f4b-4b4e-8a1f-2a3b4c5d6e7f"
84+
}
85+
}
86+
```
87+
88+
### Subscribing to multiple events globally
89+
90+
With a [WebDriver BiDi connection](/en-US/docs/Web/WebDriver/How_to/Create_BiDi_connection) and an [active session](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/new), send the following message to subscribe to all events in the `log` module and a specific event from the `network` module:
91+
92+
```json
93+
{
94+
"id": 3,
95+
"method": "session.subscribe",
96+
"params": {
97+
"events": ["log", "network.beforeRequestSent"]
98+
}
99+
}
100+
```
101+
102+
The browser responds with a subscription ID as follows:
103+
104+
```json
105+
{
106+
"id": 3,
107+
"type": "success",
108+
"result": {
109+
"subscription": "e9d0a5c4-3h6d-6d6g-0c3h-4c5d6e7f8g9h"
110+
}
111+
}
112+
```
113+
114+
### Subscribing to events scoped to a tab
115+
116+
Suppose your automation has two tabs open — one for the homepage and another for the checkout page. To receive `log.entryAdded` events only from the Checkout tab, send the following message with that tab's context ID:
117+
118+
```json
119+
{
120+
"id": 4,
121+
"method": "session.subscribe",
122+
"params": {
123+
"events": ["log.entryAdded"],
124+
"contexts": ["9b4e2f1a-3c7d-4b8e-a2f5-6d1c9e3b7f4a"]
125+
}
126+
}
127+
```
128+
129+
The browser responds with a subscription ID as follows:
130+
131+
```json
132+
{
133+
"id": 4,
134+
"type": "success",
135+
"result": {
136+
"subscription": "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
137+
}
138+
}
139+
```
140+
141+
## Specifications
142+
143+
{{Specifications}}
144+
145+
## Browser compatibility
146+
147+
{{Compat}}
148+
149+
## See also
150+
151+
- [`session.unsubscribe`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/unsubscribe) command
152+
- [`session.new`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/new) command
153+
- [`session.end`](/en-US/docs/Web/WebDriver/Reference/BiDi/Modules/session/end) command

0 commit comments

Comments
 (0)