blob: 3cd3399f98e1e03790dcef1f8822fa383ba8c09e [file] [log] [blame] [view]
Zijie He0dc9dce2024-07-11 19:34:291# Instruction of using PWA via CDP
2
3
4## Terms
5
6[Chrome DevTools Protocol (i.e. CDP)](https://chromedevtools.github.io/devtools-protocol/) allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers.
7
8[Progressive Web App (i.e. PWA)](https://web.dev/explore/progressive-web-apps) are web apps built and enhanced with modern APIs to provide enhanced capabilities while still reaching any web user on any device with a single codebase.
9
10CDP + PWA provides CDP to control the behavior of the browser to debug and test PWA.
11
12
13## Demo
14
15The preferred way of running CDP is to [use puppeteer](https://developer.chrome.com/docs/puppeteer), but any testing framework that can issue devtools commands will work. This document won’t cover too much detail about the puppeteer itself, but only focuses on how to use it to access PWA commands.
16
17
18#### Prerequisites
19
20The demo needs npm and nodejs, puppeteer is installed by npm. An apt-based installation script looks like.
21
22```
23sudo apt install npm nodejs
24npm i puppeteer --save
25```
26
27Demo uses Chrome dev channel (or google-chrome-unstable in some contexts) on linux. Eventually the required changes will be released to the Chrome production (M128 or upper), and regular Chrome stable channel (or google-chrome in some contexts) can be used.
28
29
30#### Important Caveat - uninstall when you are done!
31
32This 'actually' installs the web app on your system - to ensure this is properly cleaned up after your test is complete, please make sure you uninstall the web app in the test!
33
34TODO(https://crbug.com/339457135): Add ability to mock & clean up app installations as a command-line flag.
35
36
37#### Basic Ideas
38
39(May skip this section if you are familiar with puppeteer and CDP.)
40
41Create a browser instance via puppeteer.launch,
42
43
44```
45const browser = await puppeteer.launch({
46 // No need to use unstable once the PWA implementations roll to prod.
47 executablePath:
48 '/usr/bin/google-chrome-unstable',
49 headless: false,
50 args: ['--window-size=700,700', '--window-position=10,10'],
51 // Use pipe to allow executing high privilege commands.
52 pipe: true,
53});
54```
55
56
57Create a browser CDP session,
58
59
60```
61const browserSession = await browser.target().createCDPSession();
62```
63
64
65Send CDP commands via CDP session. This function also logs the I/O to the console, but only the `session.send` matters.
66
67
68```
69async function send(session, msg, param) {
70 if (session == null) {
71 session = browserSession;
72 }
73 console.log(' >>> Sending: ', msg, ': ', JSON.stringify(param));
74 let result, error;
75 try {
76 result = await session.send(msg, param);
77 } catch (e) {
78 error = e;
79 }
80 console.log(' <<< Response: ', trim(JSON.stringify(result)));
81 console.log(' <<< Error: ', error);
82}
83```
84
85
86Create a page CDP session,
87
88
89```
90async function current_page_session() {
91 return (await browser.pages()).pop().createCDPSession();
92}
93```
94
95
96
97#### Demo2.mjs
98
99The demo installs a webapp https://developer.chrome.com/, launches it, inspects its states, opens it into its own app window, and uninstalls it.
100
Hzj_jie63723e12024-08-28 17:54:16101The source code is at [cdp/demo2.mjs](cdp/demo2.mjs); run with the command
102`nodejs docs/webapps/cdp/demo2.mjs`.
Zijie He0dc9dce2024-07-11 19:34:29103
104A video of running demo2.mjs on chrome canary channel on linux can be found at https://youtu.be/G4wmhSCXhH4.
105
106
107#### Access via WebSocket
108
109Though its less preferred, directly using WebSocket is also possible. It requires more effort to manage the I/O. More details wont be discussed here, [cdp/demo.mjs](cdp/demo.mjs) is an example.
110
111
112## Supported APIs
113
114[See the source code.](https://source.chromium.org/search?q=domain%5CsPWA$%20f:browser_protocol.pdl%20-f:devtools-frontend&ssfr=1)
115