mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
Update Puppeteer tutorial to open popup with openPopup() (#1613)
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.
This commit is contained in:
@@ -4,6 +4,9 @@
|
|||||||
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0FAp+xWiJpGBmsKPhGcqF4/gQN9F5tmXgEVYEHUHc8HcBIUcT+9w+jo4q2OtXa2ThqgEXsx2zcNZIWJ/5yXcofVry5E2/HKBuLWHNtYOlI1rhwc/CLujo0RHhzF7rIiYcMPQdBhzr6L0u5u9N29VUWjLozltquKRcUbjXNe4LT7+q/akhn5tvfvWHkQ9qC6mRjvGwGTFlh1A6+vWKKSVYx/J+IBHW+I2X5NlAxwG734OMYVWRWK487jf1wsWZ2jHRTqg9TB3htT+84r7+E3kFYMycow9+2EhvoI2k5VGhZw1tAJcpie1Poozc5u8CTrZ4sZ5LK4h59OCOxmejC048QIDAQAB",
|
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0FAp+xWiJpGBmsKPhGcqF4/gQN9F5tmXgEVYEHUHc8HcBIUcT+9w+jo4q2OtXa2ThqgEXsx2zcNZIWJ/5yXcofVry5E2/HKBuLWHNtYOlI1rhwc/CLujo0RHhzF7rIiYcMPQdBhzr6L0u5u9N29VUWjLozltquKRcUbjXNe4LT7+q/akhn5tvfvWHkQ9qC6mRjvGwGTFlh1A6+vWKKSVYx/J+IBHW+I2X5NlAxwG734OMYVWRWK487jf1wsWZ2jHRTqg9TB3htT+84r7+E3kFYMycow9+2EhvoI2k5VGhZw1tAJcpie1Poozc5u8CTrZ4sZ5LK4h59OCOxmejC048QIDAQAB",
|
||||||
"description": "Uses the chrome.history API to display in a popup the user's most visited pages.",
|
"description": "Uses the chrome.history API to display in a popup the user's most visited pages.",
|
||||||
"permissions": ["history"],
|
"permissions": ["history"],
|
||||||
|
"background": {
|
||||||
|
"service_worker": "service-worker.js"
|
||||||
|
},
|
||||||
"action": {
|
"action": {
|
||||||
"default_popup": "popup.html",
|
"default_popup": "popup.html",
|
||||||
"default_icon": "clock.png"
|
"default_icon": "clock.png"
|
||||||
|
|||||||
3
api-samples/history/showHistory/service-worker.js
Normal file
3
api-samples/history/showHistory/service-worker.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
chrome.runtime.onInstalled.addListener(() => {
|
||||||
|
console.log('Hello world!');
|
||||||
|
});
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
|
|
||||||
const EXTENSION_PATH = '../../api-samples/history/showHistory';
|
const EXTENSION_PATH = '../../api-samples/history/showHistory';
|
||||||
const EXTENSION_ID = 'jkomgjfbbjocikdmilgaehbfpllalmia';
|
|
||||||
|
|
||||||
let browser;
|
let browser;
|
||||||
|
let worker;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
browser = await puppeteer.launch({
|
browser = await puppeteer.launch({
|
||||||
@@ -27,6 +27,15 @@ beforeEach(async () => {
|
|||||||
pipe: true,
|
pipe: true,
|
||||||
enableExtensions: [EXTENSION_PATH]
|
enableExtensions: [EXTENSION_PATH]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const workerTarget = await browser.waitForTarget(
|
||||||
|
// Assumes that there is only one service worker created by the extension and its URL ends with service-worker.js.
|
||||||
|
(target) =>
|
||||||
|
target.type() === 'service_worker' &&
|
||||||
|
target.url().endsWith('service-worker.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
worker = await workerTarget.worker();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@@ -35,10 +44,22 @@ afterEach(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('one history item is visible', async () => {
|
test('one history item is visible', async () => {
|
||||||
|
// Open a page to add a history item.
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.goto(`chrome-extension://${EXTENSION_ID}/popup.html`);
|
await page.goto('https://example.com');
|
||||||
|
|
||||||
const list = await page.$('ul');
|
// Open the extension popup.
|
||||||
|
await worker.evaluate('chrome.action.openPopup();');
|
||||||
|
|
||||||
|
const popupTarget = await browser.waitForTarget(
|
||||||
|
// Assumes that there is only one page with the URL ending with popup.html
|
||||||
|
// and that is the popup created by the extension.
|
||||||
|
(target) => target.type() === 'page' && target.url().endsWith('popup.html')
|
||||||
|
);
|
||||||
|
|
||||||
|
const popup = await popupTarget.asPage();
|
||||||
|
|
||||||
|
const list = await popup.$('ul');
|
||||||
const children = await list.$$('li');
|
const children = await list.$$('li');
|
||||||
|
|
||||||
expect(children.length).toBe(1);
|
expect(children.length).toBe(1);
|
||||||
|
|||||||
581
functional-samples/tutorial.puppeteer/package-lock.json
generated
581
functional-samples/tutorial.puppeteer/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jest": "29.7.0",
|
"jest": "29.7.0",
|
||||||
"puppeteer": "24.8.1"
|
"puppeteer": "24.35.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "jest ."
|
"start": "jest ."
|
||||||
|
|||||||
Reference in New Issue
Block a user