首页 > 解决方案 > Docker 容器中的 Headless Cypress 分辨率始终为 1280x1024

问题描述

当我在官方 docker 容器(图像)中运行我的测试时,cypress/included:7.5.0浏览器总是只有 1280x1024。在主机上运行相同的配置、相同的规格等时,我得到了所需的 1920x1080。

我的 cypress.json:

{
    "viewportHeight": 1080,
    "viewportWidth": 1920,
    "chromeWebSecurity": false,
    "baseUrl": "http://phoenix:4000",
    "watchForFileChanges": false,
    "requestTimeout": 40000,
    "defaultCommandTimeout": 40000,
    "pageLoadTimeout": 80000,
    "video": false,
    "env": {
        "codeCoverage": {
            "url": "http://phoenix:4000/__coverage__",
            "expectBackendCoverageOnly": false
        }
    }

此外,我添加了一些类似的配置,您可以在此处找到它的解释:https ://docs.cypress.io/api/plugins/browser-launch-api#See-all-Chrome-browser-switches

我的插件/index.js:

module.exports = (on, config) => {
    on('before:browser:launch', (browser, launchOptions) => {
        if (browser.name === 'chrome' && browser.isHeadless) {
            launchOptions.args.push('--window-size=1920,1080');
            launchOptions.args.push('--force-device-scale-factor=1');
            launchOptions.args.push('--start-fullscreen');
        }
        return launchOptions;
    });

    require('@cypress/code-coverage/task')(on, config);
    return config;
};

为什么从 docker 容器 ( ./node_modules/.bin/cypress run --browser chrome) 中运行时仍然获得 1280x1024 分辨率?

标签: dockercypressresolution

解决方案


我明白了,我需要先启动我自己的 Xvfb 服务器: Xvfb -screen 0 1920x1080x24 :99 & export DISPLAY=:99 && ./node_modules/.bin/cypress run --browser chrome && pkill Xvfb 来源:https ://docs.cypress.io/guides/continuous-integration/introduction#Xvfb


推荐阅读