Skip to content

Commit

Permalink
feat: Support running cypress as node process (cypress-io#16505)
Browse files Browse the repository at this point in the history
This change checks if the environment variable
ELECTRON_RUN_AS_NODE is set. If set to true, the cypress
process is started as a normal Node.js process rather than an
electron process.

Consequently, the process does not require a display server to
run so starting a display server is skipped.
  • Loading branch information
Daniel Muller committed Jun 28, 2021
1 parent 20de3e5 commit d5c7765
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
15 changes: 14 additions & 1 deletion cli/lib/exec/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,22 @@ module.exports = {
stdioOptions.env.DISPLAY = process.env.DISPLAY
}

if (process.env.ELECTRON_RUN_AS_NODE === 'true') {
debug('passing ELECTRON_RUN_AS_NODE=true')
stdioOptions.env.ELECTRON_RUN_AS_NODE = 'true'

// Replace electron specific args with path to js entry point.
electronArgs.splice(
0,
// Electron's command line switches come before the "--" delimiter.
electronArgs.indexOf('--') + 1,
// Replace package path with entry point path
path.join(state.getBinaryPkgPath(path.dirname(executable)), '..', 'index.js'),
)
}

debug('spawning Cypress with executable: %s', executable)
debug('spawn args %o %o', electronArgs, _.omit(stdioOptions, 'env'))

const child = cp.spawn(executable, electronArgs, stdioOptions)

function resolveOn (event) {
Expand Down
4 changes: 4 additions & 0 deletions cli/lib/exec/xvfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ module.exports = {
},

isNeeded () {
if (process.env.ELECTRON_RUN_AS_NODE === 'true') {
return false // xvfb required for electron processes only.
}

if (os.platform() !== 'linux') {
return false
}
Expand Down
22 changes: 14 additions & 8 deletions packages/server/lib/ipc/ipc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const {
contextBridge,
ipcRenderer,
} = require('electron')
const debug = require('debug')('cypress:server:ipc')

contextBridge.exposeInMainWorld('ipc', {
on: (...args) => ipcRenderer.on(...args),
send: (...args) => ipcRenderer.send(...args),
})
try {
const {
contextBridge,
ipcRenderer,
} = require('electron')

contextBridge.exposeInMainWorld('ipc', {
on: (...args) => ipcRenderer.on(...args),
send: (...args) => ipcRenderer.send(...args),
})
} catch {
debug('running as a node process, not an electron process')
}
21 changes: 13 additions & 8 deletions packages/server/lib/modes/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-console, @cypress/dev/arrow-body-multiline-braces */
const _ = require('lodash')
const { app } = require('electron')
const la = require('lazy-ass')
const pkg = require('@packages/root')
const path = require('path')
Expand Down Expand Up @@ -1652,14 +1651,20 @@ module.exports = {
},

async run (options) {
// electron >= 5.0.0 will exit the app if all browserwindows are closed,
// this is obviously undesirable in run mode
// https://github.com/cypress-io/cypress/pull/4720#issuecomment-514316695
app.on('window-all-closed', () => {
debug('all BrowserWindows closed, not exiting')
})
try {
const { app } = require('electron')

// electron >= 5.0.0 will exit the app if all browserwindows are closed,
// this is obviously undesirable in run mode
// https://github.com/cypress-io/cypress/pull/4720#issuecomment-514316695
app.on('window-all-closed', () => {
debug('all BrowserWindows closed, not exiting')
})

await app.whenReady()
await app.whenReady()
} catch {
debug('running as a node process, not an electron process')
}

return this.ready(options)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"webpack": "4.43.0",
"ws": "5.2.3",
"xvfb": "cypress-io/node-xvfb#22e3783c31d81ebe64d8c0df491ea00cdc74726a",
"xvfb-maybe": "0.2.1"
"xvfb-maybe": "^0.2.1"
},
"files": [
"config",
Expand Down
24 changes: 24 additions & 0 deletions packages/server/test/e2e/5_headless_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ import Fixtures from '../support/helpers/fixtures'
describe('e2e headless', function () {
e2e.setup()

describe('ELECTRON_RUN_AS_NODE', () => {
beforeEach(function () {
process.env.ELECTRON_RUN_AS_NODE = 'true'
})

// cypress run --headless
e2e.it('tests in headless mode pass', {
spec: 'headless_spec.js',
config: {
env: {
'CI': process.env.CI,
'EXPECT_HEADLESS': '1',
},
},
headed: false,
snapshot: true,
browser: 'chrome',
})

after(function () {
process.env.ELECTRON_RUN_AS_NODE = ''
})
})

// cypress run --headless
e2e.it('tests in headless mode pass', {
spec: 'headless_spec.js',
Expand Down

0 comments on commit d5c7765

Please sign in to comment.