【问题标题】:Can Cypress.io test a chrome extension?Cypress.io 可以测试 chrome 扩展吗?
【发布时间】:2026-01-25 06:55:01
【问题描述】:

我正在尝试使用 cypress.io 测试我的 chrome 扩展

我安装了https://github.com/ejoubaud/cypress-browser-extension-plugin

context('visit extension', () => {
    beforeEach(() => {
      cy.visit('chrome-extension://fmnignchafknmcleldmndpkfkdohnngn/dashboard.html')
    })

    it('does nothing', () => {
        assert(true);
    });
});

它不起作用。页面内容:

Sorry, we could not load: chrome-extension://fmnignchafknmcleldmndpkfkdohnngn/dashboard.html

【问题讨论】:

    标签: cypress end-to-end


    【解决方案1】:

    您不需要任何额外的插件来加载浏览器扩展,假设您运行的是 Cypress >v4,下面的内容应该足以加载它。

    // cypress/plugins/index.js
    module.exports = (on, config) => {
      on('before:browser:launch', (browser, launchOptions) => {
        // supply the absolute path to an unpacked extension's folder
        // NOTE: extensions cannot be loaded in headless Chrome
        launchOptions.extensions.push('Users/jane/path/to/extension')
    
        return launchOptions
      })
    }
    

    在您的测试文件中,您可以访问任何“正常”网页,它应该适合您。例如:

    // test.spec.js
    describe('Navigation', () => {
      it('cy.visit() - visit a remote url', () => {
        cy.visit('https://en.wikipedia.org/wiki/Diabetes')
      })
    })
    

    此外,赛普拉斯无法访问诸如“chrome-extension://”之类的东西(或任何不是“http”或“https”的东西)。这是他们的设计。

    【讨论】:

    • 不幸的是,加载 chrome 扩展路径错误,因为它不是有效的 https 协议。
    • 是的,根据我的回答和赛普拉斯文档,您无法加载 chrome 扩展页面。您需要通过启动选项加载解压缩的扩展。
    最近更新 更多