Skip to content

Commit

Permalink
fixes #364 enable user agent to be changed in cypress.json (#1060)
Browse files Browse the repository at this point in the history
* fixes #364 enable user agent to be changed in cypress.json

* server: fixes failing tests
  • Loading branch information
brian-mann authored Dec 13, 2017
1 parent 5d76163 commit 9ab2855
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 14 deletions.
68 changes: 68 additions & 0 deletions packages/server/__snapshots__/user_agent_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
exports['e2e user agent passes on chrome 1'] = `Warning: Cypress can only record videos when using the built in 'electron' browser.

You have set the browser to: 'chrome'

A video will not be recorded when using this browser.
(Tests Starting)


user agent
is set on visits
is set on requests


2 passing


(Tests Finished)

- Tests: 2
- Passes: 2
- Failures: 0
- Pending: 0
- Duration: 10 seconds
- Screenshots: 0
- Video Recorded: false
- Cypress Version: 1.2.3


(All Done)

`

exports['e2e user agent passes on electron 1'] = `
Started video recording: /foo/bar/.projects/e2e/cypress/videos/abc123.mp4

(Tests Starting)


user agent
is set on visits
is set on requests


2 passing


(Tests Finished)

- Tests: 2
- Passes: 2
- Failures: 0
- Pending: 0
- Duration: 10 seconds
- Screenshots: 0
- Video Recorded: true
- Cypress Version: 1.2.3


(Video)

- Started processing: Compressing to 32 CRF
- Finished processing: /foo/bar/.projects/e2e/cypress/videos/abc123.mp4 (0 seconds)


(All Done)

`

23 changes: 13 additions & 10 deletions packages/server/lib/browsers/chrome.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,27 @@ module.exports = {
fs.writeFileAsync(extensionBg, str)
.return(extensionDest)

_getArgs: (browserArgs = []) ->
args = defaultArgs.concat(browserArgs)
_getArgs: (options = {}) ->
args = defaultArgs.concat(options.browserArgs)

if os.platform() is "linux"
args.push("--disable-gpu")
args.push("--no-sandbox")

if ua = options.userAgent
args.push("--user-agent=#{ua}")

if ps = options.proxyServer
args.push("--proxy-server=#{ps}")

if options.chromeWebSecurity is false
args.push("--disable-web-security")
args.push("--allow-running-insecure-content")

args

open: (browserName, url, options = {}, automation) ->
args = @_getArgs(options.browserArgs)
args = @_getArgs(options)

Promise.all([
## ensure that we have a chrome profile dir
Expand All @@ -97,13 +107,6 @@ module.exports = {
## by being the last one
args.push("--user-data-dir=#{dir}")

if ps = options.proxyServer
args.push("--proxy-server=#{ps}")

if options.chromeWebSecurity is false
args.push("--disable-web-security")
args.push("--allow-running-insecure-content")

log("launch in chrome: %s, %s", url, args)
utils.launch(browserName, url, args)
}
8 changes: 8 additions & 0 deletions packages/server/lib/browsers/electron.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,20 @@ module.exports = {

Promise
.try =>
if ua = options.userAgent
@_setUserAgent(win.webContents, ua)

if ps = options.proxyServer
@_setProxy(win.webContents, ps)
.then ->
win.loadURL(url)
.return(win)

_setUserAgent: (webContents, userAgent) ->
## set both because why not
webContents.setUserAgent(userAgent)
webContents.session.setUserAgent(userAgent)

_setProxy: (webContents, proxyServer) ->
new Promise (resolve) ->
webContents.session.setProxy({
Expand Down
6 changes: 4 additions & 2 deletions packages/server/lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ configKeys = toWords """
screenshotOnHeadlessFailure defaultCommandTimeout
testFiles execTimeout
trashAssetsBeforeHeadlessRuns pageLoadTimeout
viewportWidth requestTimeout
viewportHeight responseTimeout
userAgent requestTimeout
viewportWidth responseTimeout
viewportHeight
videoRecording
videoCompression
videoUploadOnPasses
Expand All @@ -57,6 +58,7 @@ defaults = {
morgan: true
baseUrl: null
socketId: null
userAgent: null
isTextTerminal: false
reporter: "spec"
reporterOptions: null
Expand Down
1 change: 1 addition & 0 deletions packages/server/lib/open_project.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ create = ->
.then (url) ->
openProject.getConfig()
.then (cfg) ->
options.userAgent = cfg.userAgent
options.proxyServer = cfg.proxyUrl
options.chromeWebSecurity = cfg.chromeWebSecurity

Expand Down
40 changes: 40 additions & 0 deletions packages/server/test/e2e/user_agent_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
e2e = require("../support/helpers/e2e")

onServer = (app) ->
app.get "/agent", (req, res) ->
agent = req.headers["user-agent"]

res.send("<html><span id='agent'>#{agent}</span></html>")

app.put "/agent", (req, res) ->
res.json({
userAgent: req.headers["user-agent"]
})

describe "e2e user agent", ->
e2e.setup({
servers: {
port: 4545
onServer: onServer
}
settings: {
userAgent: "foo bar baz agent"
baseUrl: "http://localhost:4545"
}
})

it "passes on chrome", ->
e2e.exec(@, {
browser: "chrome"
spec: "user_agent_spec.coffee"
snapshot: true
expectedExitCode: 0
})

it "passes on electron", ->
e2e.exec(@, {
browser: "electron"
spec: "user_agent_spec.coffee"
snapshot: true
expectedExitCode: 0
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe "user agent", ->
it "is set on visits", ->
cy.visit("/agent")
cy.get("#agent").should("contain", "foo bar baz agent")

it "is set on requests", ->
cy
.request("PUT", "/agent")
.its("body").should("deep.eq", {
userAgent: "foo bar baz agent"
})
5 changes: 3 additions & 2 deletions packages/server/test/support/helpers/e2e.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports = {
if options.hosts
args.push("--hosts=#{options.hosts}")

if options.debug
if options.headed
args.push("--headed")

if options.reporter
Expand All @@ -158,7 +158,8 @@ module.exports = {
if options.reporterOptions
args.push("--reporter-options=#{options.reporterOptions}")

if browser = (env.BROWSER or options.browser)
## prefer options if set, else use env
if browser = (options.browser or env.BROWSER)
args.push("--browser=#{browser}")

if options.config
Expand Down
12 changes: 12 additions & 0 deletions packages/server/test/unit/browsers/chrome_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ describe "lib/browsers/chrome", ->
args = chrome._getArgs()

expect(args).not.to.include("--no-sandbox")

it "adds user agent when options.userAgent", ->
args = chrome._getArgs({
userAgent: "foo"
})

expect(args).to.include("--user-agent=foo")

it "does not add user agent", ->
args = chrome._getArgs()

expect(args).not.to.include("--user-agent=foo")
10 changes: 10 additions & 0 deletions packages/server/test/unit/browsers/electron_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,22 @@ describe "lib/browsers/electron", ->
beforeEach ->
@sandbox.stub(menu, "set")
@sandbox.stub(electron, "_setProxy").resolves()
@sandbox.stub(electron, "_setUserAgent")

it "sets dev tools in menu", ->
electron._launch(@win, @url, @options)
.then ->
expect(menu.set).to.be.calledWith({withDevTools: true})

it "sets user agent if options.userAgent", ->
electron._launch(@win, @url, @options)
.then ->
expect(electron._setUserAgent).not.to.be.called
.then =>
electron._launch(@win, @url, {userAgent: "foo"})
.then =>
expect(electron._setUserAgent).to.be.calledWith(@win.webContents, "foo")

it "sets proxy if options.proxyServer", ->
electron._launch(@win, @url, @options)
.then ->
Expand Down
5 changes: 5 additions & 0 deletions packages/server/test/unit/config_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ describe "lib/config", ->
it "viewportHeight=660", ->
@defaults "viewportHeight", 660

it "userAgent=null", ->
@defaults("userAgent", null)

it "baseUrl=null", ->
@defaults "baseUrl", null

Expand Down Expand Up @@ -629,6 +632,7 @@ describe "lib/config", ->
env: { }
port: { value: 1234, from: "cli" },
hosts: { value: null, from: "default" }
userAgent: { value: null, from: "default" }
reporter: { value: "json", from: "cli" },
reporterOptions: { value: null, from: "default" },
baseUrl: { value: null, from: "default" },
Expand Down Expand Up @@ -685,6 +689,7 @@ describe "lib/config", ->
expect(cfg.resolved).to.deep.eq({
port: { value: 2020, from: "config" },
hosts: { value: null, from: "default" }
userAgent: { value: null, from: "default" }
reporter: { value: "spec", from: "default" },
reporterOptions: { value: null, from: "default" },
baseUrl: { value: "http://localhost:8080", from: "config" },
Expand Down

0 comments on commit 9ab2855

Please sign in to comment.