You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BrowserStack+Cypress本地测试:无法加载本地HTML文件报错求助

问题:BrowserStack上Cypress无法加载本地HTML文件

我有一个前端项目,构建后输出单个HTML文件index.html。本地用Cypress测试时,直接通过cy.visit('./dist/index.html')即可验证视觉效果,无需HTTP服务托管,Cypress能自动识别,测试完全正常。

但在BrowserStack上执行相同测试时,始终无法加载本地HTML文件。按文档使用HTTP服务(如cy.visit('http://localhost:8080'))的测试可正常运行,但加载本地文件时持续出现以下错误:

`cy.visit()` failed trying to load: ./dist/index.html The response we received from your web server was: > 502: Bad Gateway This was considered a failure because the status code was not `2xx`. If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

完整错误栈信息:

`cy.visit()` failed trying to load:

./dist/index.html

The response we received from your web server was:

  > 502: Bad Gateway

This was considered a failure because the status code was not `2xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`
    at <unknown> (http://localhost:29070/__cypress/runner/cypress_runner.js:147661:84)
    at visitFailedByErr (http://localhost:29070/__cypress/runner/cypress_runner.js:147069:12)
    at <unknown> (http://localhost:29070/__cypress/runner/cypress_runner.js:147644:13)
    at tryCatcher (http://localhost:29070/__cypress/runner/cypress_runner.js:18744:23)
    at Promise._settlePromiseFromHandler (http://localhost:29070/__cypress/runner/cypress_runner.js:16679:31)
    at Promise._settlePromise (http://localhost:29070/__cypress/runner/cypress_runner.js:16736:18)
    at Promise._settlePromise0 (http://localhost:29070/__cypress/runner/cypress_runner.js:16781:10)
    at Promise._settlePromises (http://localhost:29070/__cypress/runner/cypress_runner.js:16857:18)
    at _drainQueueStep (http://localhost:29070/__cypress/runner/cypress_runner.js:13451:12)
    at _drainQueue (http://localhost:29070/__cypress/runner/cypress_runner.js:13444:9)
    at ../../node_modules/bluebird/js/release/async.js.Async._drainQueues (http://localhost:29070/__cypress/runner/cypress_runner.js:13460:5)
    at Async.drainQueues (http://localhost:29070/__cypress/runner/cypress_runner.js:13330:14)
From Your Spec Code:
    at eval (webpack:///./cypress/e2e/visual-test.cy.js:22:7)
at Array.forEach (<anonymous>)
    at Context.eval (webpack:///./cypress/e2e/visual-test.cy.js:17:13)

简化后的测试代码:

cy.visit(`./test/output/banner/index.html?seek=${time}`);
cy.get('#stage > :nth-child(2)').should('be.visible');
cy.compareSnapshot(`snapshot of page`);

已尝试的解决方案:

  • 将index.html复制到项目不同位置(根目录、Cypress测试文件旁、Cypress配置文件旁),无效;
  • 在YML文件中配置folder ./dist和folder dist/,并在connection_settings中指定local_config_file,无效;
  • 手动运行BrowserStackLocal二进制文件,在connection_settings.local_identifier中指定其标识符,或移除该标识符,均无效;
  • 直接运行./BrowserStackLocal --key <MY_KEY>,在BrowserStack实时会话中仍无法访问file:///index.html。

内容的提问来源于stack exchange,提问作者Ardeshir Izadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 04:32:02