puppeteer/assets/js/7b4b5de5.91b782e9.js

2 lines
5.1 KiB
JavaScript
Raw Normal View History

/*! For license information please see 7b4b5de5.91b782e9.js.LICENSE.txt */
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[32996],{10931:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>p,contentTitle:()=>a,default:()=>h,frontMatter:()=>s,metadata:()=>i,toc:()=>c});var o=t(85893),r=t(11151);const s={},a="Chrome Extensions",i={id:"guides/chrome-extensions",title:"Chrome Extensions",description:"Puppeteer can be used for testing Chrome Extensions.",source:"@site/../docs/guides/chrome-extensions.md",sourceDirName:"guides",slug:"/guides/chrome-extensions",permalink:"/next/guides/chrome-extensions",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{},sidebar:"docs",previous:{title:"PDF generation",permalink:"/next/guides/pdf-generation"},next:{title:"Cookies",permalink:"/next/guides/cookies"}},p={},c=[];function d(e){const n={a:"a",admonition:"admonition",code:"code",h1:"h1",p:"p",pre:"pre",...(0,r.a)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(n.h1,{id:"chrome-extensions",children:"Chrome Extensions"}),"\n",(0,o.jsx)(n.p,{children:"Puppeteer can be used for testing Chrome Extensions."}),"\n",(0,o.jsx)(n.admonition,{type:"caution",children:(0,o.jsx)(n.p,{children:"Extensions are not available in chrome-headless-shell (headless: 'shell'),\nalso known as the old headless mode."})}),"\n",(0,o.jsx)(n.admonition,{type:"note",children:(0,o.jsxs)(n.p,{children:["See ",(0,o.jsx)(n.a,{href:"https://developer.chrome.com/docs/extensions/how-to/test/end-to-end-testing",children:"https://developer.chrome.com/docs/extensions/how-to/test/end-to-end-testing"})," for more details."]})}),"\n",(0,o.jsxs)(n.p,{children:["The following is code for getting a handle to the\n",(0,o.jsx)(n.a,{href:"https://developer.chrome.com/extensions/background_pages",children:"background page"})," of\nan extension whose source is located in ",(0,o.jsx)(n.code,{children:"./my-extension"}),":"]}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-ts",children:"import puppeteer from 'puppeteer';\nimport path from 'path';\n\nconst pathToExtension = path.join(process.cwd(), 'my-extension');\nconst browser = await puppeteer.launch({\n args: [\n `--disable-extensions-except=${pathToExtension}`,\n `--load-extension=${pathToExtension}`,\n ],\n});\nconst backgroundPageTarget = await browser.waitForTarget(\n target => target.type() === 'background_page'\n);\nconst backgroundPage = await backgroundPageTarget.page();\n// Test the background page as you would any other page.\nawait browser.close();\n"})}),"\n",(0,o.jsx)(n.admonition,{type:"note",children:(0,o.jsx)(n.p,{children:"Chrome Manifest V3 extensions have a background ServiceWorker of type\n'service_worker', instead of a page of type 'background_page'."})}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-ts",children:"import puppeteer from 'puppeteer';\nimport path from 'path';\n\nconst pathToExtension = path.join(process.cwd(), 'my-extension');\nconst browser = await puppeteer.launch({\n args: [\n `--disable-extensions-except=${pathToExtension}`,\n `--load-extension=${pathToExtension}`,\n ],\n});\n\nconst workerTarget = await browser.waitForTarget(\n // Assumes that there is only one service worker created by the extension and its URL ends with background.js.\n target =>\n target.type() === 'service_worker' && target.url().endsWith('background.js')\n);\n\nconst worker = await workerTarget.worker();\n\n// Open a popup (available for Canary channels).\nawait worker.evaluate('chrome.action.openPopup();');\n\nconst popupTarget = await browser.waitForTarget(\n // Assumes that there is only one page with the URL ending with popup.html and that is the popup created by the extension.\n target => target.type() === 'page' && target.url().endsWith('popup.html')\n);\n\nconst popupPage = popupTarget.asPage();\n\n// Test the popup page as you would any other page.\n\nawait browser.close();\n"})}),"\n",(0,o.jsx)(n.admonition,{type:"note",children:(0,o.jsx)(n.p,{children:"It is not yet possible to test extension content scripts."})})]})}function h(e={}){const{wrapper:n}={...(0,r.a)(),...e.componen