<i id='BTjmi'><tr id='BTjmi'><dt id='BTjmi'><q id='BTjmi'><span id='BTjmi'><b id='BTjmi'><form id='BTjmi'><ins id='BTjmi'></ins><ul id='BTjmi'></ul><sub id='BTjmi'></sub></form><legend id='BTjmi'></legend><bdo id='BTjmi'><pre id='BTjmi'><center id='BTjmi'></center></pre></bdo></b><th id='BTjmi'></th></span></q></dt></tr></i><div id='BTjmi'><tfoot id='BTjmi'></tfoot><dl id='BTjmi'><fieldset id='BTjmi'></fieldset></dl></div>

  1. <small id='BTjmi'></small><noframes id='BTjmi'>

    1. <legend id='BTjmi'><style id='BTjmi'><dir id='BTjmi'><q id='BTjmi'></q></dir></style></legend><tfoot id='BTjmi'></tfoot>
        <bdo id='BTjmi'></bdo><ul id='BTjmi'></ul>

      使用 Puppeteer 检测和测试 Chrome 扩展程序

      时间:2023-09-06
        • <legend id='O74hv'><style id='O74hv'><dir id='O74hv'><q id='O74hv'></q></dir></style></legend>
            <tbody id='O74hv'></tbody>
            <bdo id='O74hv'></bdo><ul id='O74hv'></ul>
            • <small id='O74hv'></small><noframes id='O74hv'>

              <i id='O74hv'><tr id='O74hv'><dt id='O74hv'><q id='O74hv'><span id='O74hv'><b id='O74hv'><form id='O74hv'><ins id='O74hv'></ins><ul id='O74hv'></ul><sub id='O74hv'></sub></form><legend id='O74hv'></legend><bdo id='O74hv'><pre id='O74hv'><center id='O74hv'></center></pre></bdo></b><th id='O74hv'></th></span></q></dt></tr></i><div id='O74hv'><tfoot id='O74hv'></tfoot><dl id='O74hv'><fieldset id='O74hv'></fieldset></dl></div>

              1. <tfoot id='O74hv'></tfoot>
                本文介绍了使用 Puppeteer 检测和测试 Chrome 扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有没有办法使用 Puppeteer 测试 Chrome 扩展程序?例如,扩展程序能否检测到 Chrome 以测试"模式启动以提供不同的 UI、检查内容脚本是否正常工作等?

                Is there a way to test a Chrome extension using Puppeteer? For example can an extension detect that Chrome was launched in "test" mode to provide different UI, check content scripts are working, etc?

                推荐答案

                puppeteer.launch() 中传递 --user-agent 是覆盖具有自定义值的浏览器 UA.然后,您的扩展程序可以在其后台页面中读回 navigator.userAgent 并识别 Chrome 是使用 Puppeteer 启动的.此时,您可以提供不同的代码路径来测试 crx 与正常操作.

                Passing --user-agent in puppeteer.launch() is a useful way to override the browser's UA with a custom value. Then, your extension can read back navigator.userAgent in its background page and identify that Chrome was launched with Puppeteer. At that point, you can provide different code paths for testing the crx vs. normal operation.

                puppeteer_script.js

                const puppeteer = require('puppeteer');
                
                const CRX_PATH = '/path/to/crx/folder/';
                
                puppeteer.launch({
                  headless: false, // extensions only supported in full chrome.
                  args: [
                    `--disable-extensions-except=${CRX_PATH}`,
                    `--load-extension=${CRX_PATH}`,
                    '--user-agent=PuppeteerAgent'
                  ]
                }).then(async browser => {
                  // ... do some testing ...
                  await browser.close();
                });
                

                扩展background.js

                chrome.runtime.onInstalled.addListener(details => {
                  console.log(navigator.userAgent); // "PuppeteerAgent"
                });
                


                或者,如果你想保留浏览器的原始 UA 字符串,那就很棘手了.


                Alternatively, if you wanted to preserve the browser's original UA string, it gets tricky.

                1. 启动 Chrome 并在 Puppeteer 中创建一个空白页面.
                2. 将其标题设置为自定义名称.
                3. 在后台脚本中检测标签的标题更新.
                4. 设置一个全局标志以供以后重复使用.

                background.js

                let LAUNCHED_BY_PUPPETEER = false; // reuse in other parts of your crx as needed.
                
                chrome.tabs.onUpdated.addListener((tabId, info, tab) => {
                  if (!LAUNCHED_BY_PUPPETEER && tab.title.includes('PuppeteerAgent')) {
                    chrome.tabs.remove(tabId);
                    LAUNCHED_BY_PUPPETEER = true;
                  }
                });
                

                puppeteer_script.js

                const puppeteer = require('puppeteer');
                
                const CRX_PATH = '/path/to/crx/folder/';
                
                puppeteer.launch({
                  headless: false, // extensions only supported in full chrome.
                  args: [
                    `--disable-extensions-except=${CRX_PATH}`,
                    `--load-extension=${CRX_PATH}`,
                  ]
                }).then(async browser => {
                  const page = await browser.newPage();
                  await page.evaluate("document.title = 'PuppeteerAgent'");
                
                  // ... do some testing ...
                
                  await browser.close();
                });
                

                注意:缺点是这种方法需要标签".manifest.json 中的权限.

                Note: The downside is that this approach requires the "tabs" permission in manifest.json.

                假设您想测试您的弹出页面 UI?一种方法是直接导航到它的 chrome-extension:// URL,然后使用 puppeteer 进行 UI 测试:

                Let's say you wanted to test your popup page UI? One way to do that would be to navigate to its chrome-extension:// URL directly, then use puppeteer to do the UI testing:

                // Can we navigate to a chrome-extension page? YES!
                const page = await browser.newPage();
                await page.goto('chrome-extension://ipfiboohojhbonenbbppflmpfkakjhed/popup.html');
                // click buttons, test UI elements, etc.
                

                要为测试创建稳定的扩展 ID,请查看:https://stackoverflow.com/a/23877974/274673

                To create a stable extension id for testing, check out: https://stackoverflow.com/a/23877974/274673

                这篇关于使用 Puppeteer 检测和测试 Chrome 扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:断言文件下载的 Testcafe 示例 下一篇:赛普拉斯获取 href 属性

                相关文章

                最新文章

                <small id='oXN6C'></small><noframes id='oXN6C'>

              2. <tfoot id='oXN6C'></tfoot>
              3. <legend id='oXN6C'><style id='oXN6C'><dir id='oXN6C'><q id='oXN6C'></q></dir></style></legend>

              4. <i id='oXN6C'><tr id='oXN6C'><dt id='oXN6C'><q id='oXN6C'><span id='oXN6C'><b id='oXN6C'><form id='oXN6C'><ins id='oXN6C'></ins><ul id='oXN6C'></ul><sub id='oXN6C'></sub></form><legend id='oXN6C'></legend><bdo id='oXN6C'><pre id='oXN6C'><center id='oXN6C'></center></pre></bdo></b><th id='oXN6C'></th></span></q></dt></tr></i><div id='oXN6C'><tfoot id='oXN6C'></tfoot><dl id='oXN6C'><fieldset id='oXN6C'></fieldset></dl></div>
                  <bdo id='oXN6C'></bdo><ul id='oXN6C'></ul>