Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Comment thread
flotwig marked this conversation as resolved.
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