GitLab CI/CD下TestCafe测试编译超时及浏览器连接失败问题咨询
解决TestCafe在GitLab CI/CD中周期性连接失败的问题
一、优化代码导入及其他故障解决方案
1. 代码导入优化方案
针对你提供的测试文件导入代码,可从以下几点优化:
- 移除重复导入:当前代码同时使用
import和require导入了../helpers/shared,且testcafe的userVariables和ClientFunction也用require导入,统一改为import并删除重复项:// 删掉这两行重复导入 // const sharedHelpers = require('../helpers/shared'); // const { userVariables, ClientFunction } = require('testcafe'); // 改为统一的import import { userVariables, ClientFunction } from 'testcafe'; - 按需导入,剔除未使用项:检查当前测试文件实际用到的导入内容,删掉未使用的导出。比如
../helpers/locators/soqnaLocators中导入了大量定位器,若测试只用到其中几个,只保留需要的:// 原导入 import { bestAnswer, expertAnswer, // 删掉未使用的定位器 } from '../helpers/locators/soqnaLocators'; - 合并同模块导入:将同一模块的多次导入合并为一次,减少模块解析次数。
- 动态导入非核心依赖:对测试流程中非初始化阶段才用到的工具函数,使用动态导入延迟加载,降低初始编译压力:
// 比如在需要用到createUser时再导入 const { createUser } = await import('@chegg/testcafe-for-chegg-com/helper/identity/user');
2. 其他故障解决措施
除了导入优化,还可从CI环境和TestCafe配置入手:
- 降低并发执行数:当前并发数设为8,GitLab Runner资源可能不足以支撑,将TestCafe的
-c参数改为4或更低,比如在CI命令中设置testcafe chrome:headless --no-sandbox --disable-dev-shm-usage -c 4。 - 增加浏览器初始化超时:通过
--browser-init-timeout参数延长超时时间,比如设置为3分钟:testcafe ... --browser-init-timeout 180000。 - 提升Runner资源配置:在
.gitlab-ci.yml中为测试任务分配更多CPU和内存:test: script: - npm run test resources: limits: memory: 4G cpu: 2 - 缓存依赖文件:在CI中配置
cache缓存node_modules,减少每次构建的依赖安装时间,间接加快测试编译速度:cache: paths: - node_modules/
二、过度导入为何会影响浏览器连接?
TestCafe的测试执行流程分为代码编译和浏览器连接初始化两个关键阶段,过度导入会从以下方面干扰这个流程:
- 编译超时触发连接失败:过多的导入会大幅增加TestCafe解析、编译测试代码的时间,当编译时间超过浏览器初始化超时阈值(当前为2分钟)时,已启动的浏览器会因长时间未收到测试指令而断开连接,最终导致所有浏览器连接建立失败。
- 资源占用过高导致进程异常:大量导入会使编译后的测试代码体积剧增,TestCafe进程和浏览器进程的内存、CPU占用率飙升,在CI环境资源有限的情况下,容易出现进程卡顿、假死甚至崩溃,无法正常完成连接握手。
- 初始化延迟引发超时:编译阶段的延迟会导致TestCafe无法及时向浏览器发送启动指令,浏览器在等待超时后自动关闭,从而出现"无法建立浏览器连接"的错误。
错误信息参考
ERROR Cannot establish one or more browser connections. 8 of 8 browser connections have not been established: - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage - chrome:headless --no-sandbox --disable-dev-shm-usage Hints: - Tests took too long to compile (2.03 min). Ensure the test code has no excessive imports. - The host machine may not be powerful enough to handle the specified concurrency factor (8). Decrease the concurrency factor or allocate more computing resources to the host machine. - Increase the Browser Initialization Timeout if its value is too low (currently: 2 minutes for local browsers and 6 minutes for remote browsers). The timeout determines how long TestCafe waits for browsers to be ready. - The error can also be caused by network issues or remote device failure. Make sure that your network connection is stable and you can reach the remote device.
原测试文件导入代码参考
import { ANSWER_OBFUSCATION_TEST_ID, SOQnaTestIdsAnswerPreview, } from 'chegg-web-app/libs/qna-so/constants/soqnaTestIds'; import { questionBody, selectByClassName, selectById, selectByTestId, thumbsUp, } from '../helpers/locators'; import { answerTitleToolTip, bestAnswer, breadcrumbParentSubjectLink, breadcrumbQuestionLink, breadcrumbSubjectLink, breadcrumbSubjectQnALink, closeResubscribeModal, ctaBody, ctaButton, ctaHeader, ctaModal, expertAnswer, nextLink, nextLinkText, positiveRatingText, postAQuestionCard, previousLink, previousLinkText, soCtaButton, structuredAnswer, structuredAnswerPreview, transcribedImageText, transcriptionSkipLink, uaeAnswerPreviewHeader, uaeCloseCheckout, uaeFinalAnswerHeader, } from '../helpers/locators/soqnaLocators'; import { AuthPage } from '../helpers/pages'; import soqna, { SOQNAPage } from '../helpers/pages/soqna'; import { LanguageRequestHook, a11y, getPagePath, getPageUrl, goBack, reloadPage, rioLogger, setStyleAttribute, userLogInWithCredentials, verifyRioEventv2, } from '../helpers/shared'; import { accessSearchReturnedResult, answeredBy, expertVerified, getTheSolutionLink, getTotalCountSearchResults, greatMatchQuestionRenderer, navigateToSOQna, nonSubsAutomatedCTA, nonSubsCTA, verifyQuestionPreviewSERP, } from '../helpers/soqnaHelpers'; const sharedHelpers = require('../helpers/shared'); import config from '../config'; import { askExpertClickEvent, bestAnswerTooltipEvent, mathSolverEvent, nextQuestionEvent, previousQuestionEvent, questionNavbarEvent, questionStatusEvent, questionViewEvent, seeAnswerClickEvent, showTranscribedClickEvent, sqnaStepsViewEvent, viewFullAnswerClickEvent, viewFullSolutionViewEvent, } from '../helpers/soqnaRioEvents'; const { userVariables, ClientFunction } = require('testcafe'); import { CountryCode } from '@chegg/testcafe-for-chegg-com/helper/graphql/cartService/types'; import { SubscriptionType } from '@chegg/testcafe-for-chegg-com/helper/graphql/cdpService/types'; import { NonSubScriptionType, SubscriptionChange, } from '@chegg/testcafe-for-chegg-com/helper/identity/types'; import { createUser } from '@chegg/testcafe-for-chegg-com/helper/identity/user'; import { createUserForCheckout2 } from '../helpers/commerce'; import { enterTextAndSubmit, enterTextAndSubmitFromChat, } from '../helpers/qaauidEditorHelpers';
内容的提问来源于stack exchange,提问作者user2958388
相关产品推荐
相关产品推荐

