Skip to content

Commit

Permalink
Allow passing qs parameter to cy.visit to set querystring (#5040)
Browse files Browse the repository at this point in the history
* allow passing qs param to cy.visit

* add types

* qs -> params, merge with url, throw errors on invalid type, add tests

* params -> qs

* cleanup

* fix test
  • Loading branch information
flotwig authored Sep 17, 2019
1 parent 8cde36e commit 9f717fe
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,11 @@ declare namespace Cypress {
* })
*/
auth: Auth

/**
* Query parameters to append to the `url` of the request.
*/
qs: object
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/driver/src/cy/commands/navigation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ module.exports = (Commands, Cypress, cy, state, config) ->
onLoad: ->
})

if !_.isUndefined(options.qs) and not _.isObject(options.qs)
$utils.throwErrByPath("visit.invalid_qs", { args: { qs: String(options.qs) }})

if options.retryOnStatusCodeFailure and not options.failOnStatusCode
$utils.throwErrByPath("visit.status_code_flags_invalid")

Expand Down Expand Up @@ -550,6 +553,9 @@ module.exports = (Commands, Cypress, cy, state, config) ->
if baseUrl = config("baseUrl")
url = $Location.qualifyWithBaseUrl(baseUrl, url)

if qs = options.qs
url = $Location.mergeUrlWithParams(url, qs)

cleanup = null

## clear the current timeout
Expand Down Expand Up @@ -610,7 +616,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
existing = $utils.locExisting()

## TODO: $Location.resolve(existing.origin, url)

if $Location.isLocalFileUrl(url)
return specifyFileByRelativePath(url, options._log)

Expand Down
3 changes: 2 additions & 1 deletion packages/driver/src/cypress/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ module.exports = {
invalid_1st_arg: "#{cmd('visit')} must be called with a URL or an options object containing a URL as its 1st argument"
invalid_method: "#{cmd('visit')} was called with an invalid method: '{{method}}'. Method can only be GET or POST."
invalid_headers: "#{cmd('visit')} requires the 'headers' option to be an object."
invalid_qs: "#{cmd('visit')} requires the 'qs' option to be an object, but received: '{{qs}}'"
no_duplicate_url: """
#{cmd('visit')} must be called with only one URL. You specified two URLs:
Expand Down Expand Up @@ -1090,7 +1091,7 @@ module.exports = {
#{cmd('request')} will automatically get and set cookies and enable you to parse responses.
"""

specify_file_by_relative_path: """
#{cmd('visit')} failed because the 'file://...' protocol is not supported by Cypress.
Expand Down
5 changes: 5 additions & 0 deletions packages/driver/src/cypress/location.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class $Location
url = new UrlParse(url, existing.origin)
url.toString()

@mergeUrlWithParams = (url, params) ->
url = new UrlParse(url, null, true)
url.set("query", _.merge(url.query || {}, params))
url.toString()

@normalize = (url) ->
## A properly formed URL will always have a trailing
## slash at the end of it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,11 @@ describe "src/cy/commands/navigation", ->

it "does not support file:// protocol", (done) ->
Cypress.config("baseUrl", "")

cy.on "fail", (err) ->
expect(err.message).to.contain("cy.visit() failed because the 'file://...' protocol is not supported by Cypress.")
done()

cy.visit("file:///cypress/fixtures/generic.html")

## https://github.com/cypress-io/cypress/issues/1727
Expand Down Expand Up @@ -619,6 +619,15 @@ describe "src/cy/commands/navigation", ->
})
cy.contains('"user-agent":"something special"')

it "can send querystring params", ->
qs = { "foo bar": "baz quux" }

cy
.visit("http://localhost:3500/dump-qs", { qs })
.then ->
cy.contains(JSON.stringify(qs))
cy.url().should('eq', 'http://localhost:3500/dump-qs?foo%20bar=baz%20quux')

describe "can send a POST request", ->
it "automatically urlencoded using an object body", ->
cy.visit("http://localhost:3500/post-only", {
Expand Down Expand Up @@ -1060,6 +1069,23 @@ describe "src/cy/commands/navigation", ->
headers: "quux"
})

[
"foo",
null,
false,
].forEach (qs) =>
str = String(qs)

it "throws when qs is #{str}", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.visit() requires the 'qs' option to be an object, but received: '#{str}'"
done()

cy.visit({
url: "http://foobarbaz",
qs
})

it "throws when failOnStatusCode is false and retryOnStatusCodeFailure is true", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.visit() was invoked with { failOnStatusCode: false, retryOnStatusCodeFailure: true }."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ describe "src/cypress/location", ->
obj = Location.create(urls.signin)
expect(obj.toString()).to.eq("http://localhost:2020/signin")

context ".mergeUrlWithParams", ->
beforeEach ->
@url = (str, expected, params) ->
url = Location.mergeUrlWithParams(str, params)
expect(url).to.eq(expected)

it "merges params into a URL", ->
@url "http://example.com/a", "http://example.com/a?foo=bar", { foo: 'bar' }

it "overrides existing queryparams", ->
@url "http://example.com/a?foo=quux", "http://example.com/a?foo=bar", { foo: 'bar' }

it "appends and overrides existing queryparams", ->
@url "http://example.com/a?foo=quux", "http://example.com/a?foo=bar&baz=quuz", { foo: 'bar', baz: 'quuz' }

context ".normalize", ->
beforeEach ->
@url = (source, expected) ->
Expand Down
3 changes: 3 additions & 0 deletions packages/driver/test/support/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ niv.install("react-dom@15.6.1")
app.all '/dump-method', (req, res) ->
res.send("<html><body>request method: #{req.method}</body></html>")

app.all '/dump-qs', (req, res) ->
res.send("<html><body>it worked!<br>request querystring:<br>#{JSON.stringify(req.query)}</body></html>")

app.post '/post-only', (req, res) ->
res.send("<html><body>it worked!<br>request body:<br>#{JSON.stringify(req.body)}</body></html>")

Expand Down

4 comments on commit 9f717fe

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f717fe Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/linux-x64/circle-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-152340/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-152327/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f717fe Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/win32-ia32/appveyor-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-27478343/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.0/win32-ia32/appveyor-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-27478343/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f717fe Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/win32-x64/appveyor-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-27478343/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.0/win32-x64/appveyor-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-27478343/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f717fe Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/darwin-x64/circle-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-152352/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f717fe7e8b73313ee23c5aa3c92e504fb899337-152354/cypress.tgz

Please sign in to comment.