Skip to content

Commit

Permalink
Replace @cypress/xvfb
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Jan 6, 2025
1 parent a31e04d commit 1bb1f31
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 73 deletions.
81 changes: 55 additions & 26 deletions lib/support/xvfb.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
import { promisify } from 'node:util';
import { execa } from 'execa';
import get from 'lodash.get';
import Xvfb from '@cypress/xvfb';
import { xvfbDisplay } from '../video/defaults.js';
import { getViewPort } from '../support/getViewPort.js';
import { isAndroidConfigured } from '../android/index.js';

function buildXvfbArguments({ display, screen = 0, size, silent }) {
function buildXvfbCommand({ display, screen = 0, size, silent }) {
return {
displayNum: display,
silent,
reuse: true,
xvfb_args: ['-ac', '-nolisten', 'tcp', '-screen', screen, `${size}x24`]
command: 'Xvfb',
args: [
`:${display}`,
'-ac',
'-nolisten',
'tcp',
'-screen',
screen,
`${size}x24`
],
silent
};
}

async function startXvfb({ size, options }) {
export async function startXvfb({ size, options }) {
// If using Firefox, add some extra width to avoid issues
let xvfbSize = size;
if (options.browser === 'firefox') {
const extraSizeInFirefox = 200;
const viewPort = size.split('x');
xvfbSize = Number(viewPort[0]) + extraSizeInFirefox + 'x' + viewPort[1];
const [width, height] = size.split('x');
xvfbSize = `${Number(width) + extraSizeInFirefox}x${height}`;
}
const xvfbArguments = buildXvfbArguments({
display: get(options, 'xvfbParams.display', xvfbDisplay),

const display = get(options, 'xvfbParams.display', xvfbDisplay);
const silent = options.verbose >= 2 ? false : true;

const { command, args } = buildXvfbCommand({
display,
size: xvfbSize,
silent: options.verbose >= 2 ? false : true
silent
});

const xvfbProcess = execa(command, args, {
stdio: silent ? 'ignore' : 'inherit',
detached: true
});
const xvfb = new Xvfb(xvfbArguments);
const start = promisify(xvfb.start.bind(xvfb));
await start();
return xvfb;

const waitToSettle = get(options, 'xvfbParams.waitToSettle', 500);
await new Promise(resolve => {
setTimeout(resolve, waitToSettle);
});

// Export for the browser
process.env.DISPLAY = `:${display}`;

return {
display,
process: xvfbProcess
};
}
export async function stopXvfb(xvfbSession) {
if (!xvfbSession || !xvfbSession.process) {
return;
}

async function stopXvfb(xvfb) {
const stop = promisify(xvfb.stop.bind(xvfb));
return stop();
try {
xvfbSession.process.kill('SIGTERM');
await xvfbSession.process;
} catch {
// Just swallow
}
}

/**
* Create a new XVFB instance
* @class
*/
export class XVFB {
constructor(options) {
this.options = options;
this.xvfbSession = undefined;
}

async start() {
// This is the fix for the current use of ENV in Docker
// we should do a better fix for that
const useXvfb = get(this.options, 'xvfb', false);

if (
(useXvfb === true || useXvfb === 'true') &&
!isAndroidConfigured(this.options)
Expand All @@ -64,6 +92,7 @@ export class XVFB {
async stop() {
if (this.xvfbSession) {
await stopXvfb(this.xvfbSession);
this.xvfbSession = undefined;
}
}
}
46 changes: 0 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"type": "module",
"types": "./types/scripting.d.ts",
"dependencies": {
"@cypress/xvfb": "1.2.4",
"@devicefarmer/adbkit": "3.3.8",
"@sitespeed.io/chromedriver": "131.0.6778-69",
"@sitespeed.io/edgedriver": "131.0.2903-112",
Expand Down

0 comments on commit 1bb1f31

Please sign in to comment.