Skip to content

Commit

Permalink
Firefox headless (#6336)
Browse files Browse the repository at this point in the history
* firefox headless

* update errors

* fix video recording in ff headless

* fix thos types
  • Loading branch information
flotwig authored Feb 5, 2020
1 parent 2fba6fa commit 11efc4b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cli/__snapshots__/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports['shows help for run --foo 1'] = `
-e, --env <env> sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json
--group <name> a named group for recorded runs in the Cypress Dashboard
-k, --key <record-key> your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.
--headed displays the browser instead of running headlessly (defaults to true for Chrome-family browsers)
--headed displays the browser instead of running headlessly (defaults to true for Firefox and Chromium-family browsers)
--headless hide the browser instead of running headed (defaults to true for Electron)
--no-exit keep the browser open after tests finish
--parallel enables concurrent runs and automatic load balancing of specs across multiple machines or processes
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const descriptions = {
forceInstall: 'force install the Cypress binary',
global: 'force Cypress into global mode as if its globally installed',
group: 'a named group for recorded runs in the Cypress Dashboard',
headed: 'displays the browser instead of running headlessly (defaults to true for Chrome-family browsers)',
headed: 'displays the browser instead of running headlessly (defaults to true for Firefox and Chromium-family browsers)',
headless: 'hide the browser instead of running headed (defaults to true for Electron)',
key: 'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
parallel: 'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
Expand Down
8 changes: 1 addition & 7 deletions packages/server/lib/browsers/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@ import _ from 'lodash'
import os from 'os'
import path from 'path'
import extension from '@packages/extension'
import { FoundBrowser } from '@packages/launcher'
import appData from '../util/app_data'
import fs from '../util/fs'
import { CdpAutomation } from './cdp_automation'
import * as CriClient from './cri-client'
import protocol from './protocol'
import utils from './utils'
import { Browser } from './types'

// TODO: this is defined in `cypress-npm-api` but there is currently no way to get there
type CypressConfiguration = any

type Browser = FoundBrowser & {
majorVersion: number
isHeadless: boolean
isHeaded: boolean
}

const debug = debugModule('cypress:server:browsers:chrome')

const LOAD_EXTENSION = '--load-extension='
Expand Down
6 changes: 5 additions & 1 deletion packages/server/lib/browsers/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import urlUtil from 'url'
import FirefoxProfile from 'firefox-profile'
import firefoxUtil from './firefox-util'
import utils from './utils'
import { Browser } from '@packages/launcher'
import { Browser } from './types'

const debug = Debug('cypress:server:browsers:firefox')

Expand Down Expand Up @@ -137,6 +137,10 @@ export async function open (browser: Browser, url, options: any = {}) {
],
})

if (browser.isHeadless) {
defaultLaunchOptions.args.push('-headless')
}

debug('firefox open %o', options)

const ps = options.proxyServer
Expand Down
7 changes: 7 additions & 0 deletions packages/server/lib/browsers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FoundBrowser } from '@packages/launcher'

export type Browser = FoundBrowser & {
majorVersion: number
isHeadless: boolean
isHeaded: boolean
}
5 changes: 3 additions & 2 deletions packages/server/lib/errors.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ getMsgByType = (type, arg1 = {}, arg2, arg3) ->
when "CHROME_WEB_SECURITY_NOT_SUPPORTED"
"""
Your project has set the configuration option: `chromeWebSecurity: false`
This option will not have an effect in #{_.capitalize(arg1)}. Tests that rely on web security being disabled will not run as expected.
"""
when "BROWSER_NOT_FOUND_BY_NAME"
Expand All @@ -126,8 +126,9 @@ getMsgByType = (type, arg1 = {}, arg2, arg3) ->
A video will not be recorded when using this mode.
"""
when "CANNOT_RECORD_VIDEO_FOR_THIS_BROWSER"
## TODO: can this error be removed? what other family of browsers would we support....?
"""
Warning: Cypress can only record videos when using an Electron or Chrome-family browser.
Warning: Cypress can only record videos when using Firefox, Electron, or a Chromium-family browser.
You have set the browser to: '#{arg1}'
Expand Down
16 changes: 8 additions & 8 deletions packages/server/lib/modes/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,23 +458,23 @@ const getDefaultBrowserOptsByFamily = (browser, project, writeVideoFrame) => {
}

if (browser.family === 'chromium') {
return getChromeProps(browser.isHeaded, project, writeVideoFrame)
return getChromeProps(writeVideoFrame)
}

if (browser.family === 'firefox') {
return getFirefoxProps(browser.isHeaded, project, writeVideoFrame)
return getFirefoxProps(project, writeVideoFrame)
}

return {}
}

const getFirefoxProps = (isHeaded, project, writeVideoFrame) => {
debug('setting Firefox properties %o', { isHeaded })
const getFirefoxProps = (project, writeVideoFrame) => {
debug('setting Firefox properties')

return _
.chain({})
.tap((props) => {
if (isHeaded && writeVideoFrame) {
if (writeVideoFrame) {
const onScreencastFrame = (data) => {
writeVideoFrame(data)
}
Expand All @@ -487,10 +487,10 @@ const getFirefoxProps = (isHeaded, project, writeVideoFrame) => {
.value()
}

const getChromeProps = (isHeaded, project, writeVideoFrame) => {
const getChromeProps = (writeVideoFrame) => {
const shouldWriteVideo = Boolean(writeVideoFrame)

debug('setting Chrome properties %o', { isHeaded, shouldWriteVideo })
debug('setting Chrome properties %o', { shouldWriteVideo })

return _
.chain({})
Expand Down Expand Up @@ -652,7 +652,7 @@ const browserCanBeRecorded = (browser) => {
return true
}

if (browser.family === 'firefox' && browser.isHeaded) {
if (browser.family === 'firefox') {
return true
}

Expand Down
1 change: 0 additions & 1 deletion packages/server/test/e2e/2_headless_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('e2e headless', function () {
// cypress run --headless
e2e.it('tests in headless mode pass', {
spec: 'headless_spec.js',
browser: ['chrome', 'electron'],
config: {
env: {
'CI': process.env.CI,
Expand Down

1 comment on commit 11efc4b

@cypress-bot

This comment was marked as outdated.

Please sign in to comment.