Skip to content

Commit 7fb3b43

Browse files
committed
Update Puppeteer tutorial to open popup with openPopup()
Updates the Puppeteer tutorial to use the `action.openPopup()` method to open the popup, and then `asPage()` to access the popup as a page target. A basic service worker has been added to the history sample to give us a context to call `action.openPopup()` in.
1 parent 85f721f commit 7fb3b43

File tree

5 files changed

+344
-276
lines changed

5 files changed

+344
-276
lines changed

api-samples/history/showHistory/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0FAp+xWiJpGBmsKPhGcqF4/gQN9F5tmXgEVYEHUHc8HcBIUcT+9w+jo4q2OtXa2ThqgEXsx2zcNZIWJ/5yXcofVry5E2/HKBuLWHNtYOlI1rhwc/CLujo0RHhzF7rIiYcMPQdBhzr6L0u5u9N29VUWjLozltquKRcUbjXNe4LT7+q/akhn5tvfvWHkQ9qC6mRjvGwGTFlh1A6+vWKKSVYx/J+IBHW+I2X5NlAxwG734OMYVWRWK487jf1wsWZ2jHRTqg9TB3htT+84r7+E3kFYMycow9+2EhvoI2k5VGhZw1tAJcpie1Poozc5u8CTrZ4sZ5LK4h59OCOxmejC048QIDAQAB",
55
"description": "Uses the chrome.history API to display in a popup the user's most visited pages.",
66
"permissions": ["history"],
7+
"background": {
8+
"service_worker": "service-worker.js"
9+
},
710
"action": {
811
"default_popup": "popup.html",
912
"default_icon": "clock.png"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
chrome.runtime.onInstalled.addListener(() => {
2+
console.log('Hello world!');
3+
});

functional-samples/tutorial.puppeteer/index.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
const puppeteer = require('puppeteer');
1717

1818
const EXTENSION_PATH = '../../api-samples/history/showHistory';
19-
const EXTENSION_ID = 'jkomgjfbbjocikdmilgaehbfpllalmia';
2019

2120
let browser;
21+
let worker;
2222

2323
beforeEach(async () => {
2424
browser = await puppeteer.launch({
@@ -27,6 +27,15 @@ beforeEach(async () => {
2727
pipe: true,
2828
enableExtensions: [EXTENSION_PATH]
2929
});
30+
31+
const workerTarget = await browser.waitForTarget(
32+
// Assumes that there is only one service worker created by the extension and its URL ends with service-worker.js.
33+
(target) =>
34+
target.type() === 'service_worker' &&
35+
target.url().endsWith('service-worker.js')
36+
);
37+
38+
worker = await workerTarget.worker();
3039
});
3140

3241
afterEach(async () => {
@@ -35,10 +44,22 @@ afterEach(async () => {
3544
});
3645

3746
test('one history item is visible', async () => {
47+
// Open a page to add a history item.
3848
const page = await browser.newPage();
39-
await page.goto(`chrome-extension://${EXTENSION_ID}/popup.html`);
49+
await page.goto('https://example.com');
50+
51+
// Open the extension popup.
52+
await worker.evaluate('chrome.action.openPopup();');
53+
54+
const popupTarget = await browser.waitForTarget(
55+
// Assumes that there is only one page with the URL ending with popup.html
56+
// and that is the popup created by the extension.
57+
(target) => target.type() === 'page' && target.url().endsWith('popup.html')
58+
);
59+
60+
const popup = await popupTarget.asPage();
4061

41-
const list = await page.$('ul');
62+
const list = await popup.$('ul');
4263
const children = await list.$$('li');
4364

4465
expect(children.length).toBe(1);

0 commit comments

Comments
 (0)