Django项目Docker运行Selenium E2E测试报status 11错误如何解决
问题根因
你遇到的Process unexpectedly closed with status 11错误是Selenium Firefox容器在ARM架构(新Mac的M系列芯片)上的兼容性问题,旧Mac为x86_64架构,默认的selenium/standalone-firefox镜像在ARM设备上运行时会触发段错误,该问题和本地是否安装Selenium无关,也不是.gitignore遗漏配置导致的。
修复步骤
1. 修改docker-compose.yml的selenium镜像配置
改用适配ARM架构的Firefox镜像,同时增加共享内存配置避免Firefox运行崩溃:
version: '3.8' services: web: build: . command: python /my_project/manage.py runserver 0.0.0.0:8000 volumes: - .:/my_project ports: - 8000:8000 - 35729:35729 depends_on: - selenium selenium: image: seleniarm/standalone-firefox:latest ports: - 4444:4444 shm_size: 2g environment: - SE_NO_SANDBOX=true - SE_DISABLE_DEV_SHM_USAGE=true
seleniarm是社区维护的适配ARM架构的Selenium镜像系列,完全兼容官方接口。
2. 调整测试基类的浏览器初始化逻辑
修正参数传递写法,避免配置不生效:
...{Django/Selenium imports}... class FunctionalTest(StaticLiveServerTestCase): host = 'web' def setUp(self): options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') self.browser = webdriver.Remote( command_executor='http://selenium:4444/wd/hub', options=options ) self.browser.get(self.live_server_url) self.actions = ActionChains(self.browser)
desired_capabilities在新版本Selenium中已经废弃,直接传options参数即可,新增的两个运行参数是适配容器环境的必要配置。
3. 重新构建并启动容器
执行以下命令清理旧镜像缓存,重启服务:
docker-compose down -v docker-compose build --no-cache docker-compose up
补充说明
你完全不需要在本地安装Selenium或者浏览器驱动,所有运行依赖都已经封装在Docker容器中,本次问题完全是新旧设备架构差异导致的镜像兼容性问题。
内容的提问来源于stack exchange,提问作者juancoquet
相关产品推荐
相关产品推荐

