Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
browser.assert.url should be able to test individual properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 21, 2012
1 parent 8293622 commit 2cf6e95
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
13 changes: 9 additions & 4 deletions doc/new-assertions.md
Expand Up @@ -74,10 +74,6 @@ Asserts that selected element has the focus.
Asserts that selected input field (text field, text area, etc) has the expected
value.

`browser.assert.pathname(expected, message)`

Assert that document URL has the expected pathname.

`browser.assert.prompted(messageShown, message)`

Assert that browser prompted with a given message.
Expand All @@ -102,6 +98,15 @@ Assert that text content of selected element(s) matche the expected value.

Asserts that current page has the expected URL.

The expected URL value can be a string, regular expression, or function just
like every other assertion. It can also be an object, in which case, individual
properties are matched against the URL.

For example:

browser.assert.url({ pathame: "/resource" });
browser.assert.url({ query: { name: "joedoe" } });


You can add more assertions by adding methods to the prototype of
`Browser.Assert`. These have access to the browser as a property, for example:
Expand Down
20 changes: 13 additions & 7 deletions lib/zombie/assert.coffee
Expand Up @@ -2,6 +2,7 @@

assert = require("assert")
{ isRegExp } = require("util")
URL = require("url")


# Used to assert that actual matches expected value, where expected may be a function or a string.
Expand All @@ -11,7 +12,7 @@ assertMatch = (actual, expected, message)->
else if typeof(expected) == "function"
assert expected(actual), message
else
assert.equal actual, expected, message
assert.deepEqual actual, expected, message


class Assert
Expand All @@ -25,10 +26,6 @@ class Assert
message ||= "Expected cooking #{name} to have the value '#{expected}', found '#{actual}'"
assertMatch actual, expected, message

# Assert that document URL has the expected pathname.
pathname: (expected, message)->
assertMatch @browser.location.pathname, expected, message

# Asserts that browser was redirected when retrieving the current page.
redirected: (message)->
assert @browser.redirected, message
Expand All @@ -42,8 +39,17 @@ class Assert
assert.equal @browser.statusCode, 200, message

# Asserts that current page has the expected URL.
url: (url, message)->
assert.equal @browser.location, url, message
#
# Expected value can be a String, RegExp, Function or an object, in which case
# object properties are tested against the actual URL (e.g. pathname, host,
# query).
url: (expected, message)->
if typeof(expected) == "string" || isRegExp(expected) || typeof(expected) == "function"
assertMatch @browser.location.href, expected, message
else
url = URL.parse(@browser.location.href, true)
for key, value of expected
assertMatch url[key], value, message


# -- Document contents --
Expand Down
11 changes: 5 additions & 6 deletions lib/zombie/browser.coffee
Expand Up @@ -49,12 +49,6 @@ class Browser extends EventEmitter
@hooks = new Hooks()


# Open tabs.
Object.defineProperty this, "tabs",
value: createTabs(this)



# -- Console/Logging --

# Shared by all windows.
Expand Down Expand Up @@ -148,6 +142,10 @@ class Browser extends EventEmitter

# -- Tabs/Windows --

# Open tabs.
tabs = createTabs(this)
Object.defineProperty this, "tabs", value: tabs

# The active browser window
active = null
# Window becomes inactive
Expand Down Expand Up @@ -1006,6 +1004,7 @@ class Browser extends EventEmitter
# Version number. We get this from package.json.
Browser.VERSION = JSON.parse(File.readFileSync("#{__dirname}/../../package.json")).version


# -- Global options --

# These defaults are used in any new browser instance.
Expand Down
12 changes: 6 additions & 6 deletions test/iframe_test.coffee
Expand Up @@ -143,7 +143,7 @@ describe "IFrame", ->
@browser.clickLink "self", done

it "should open link", ->
@browser.assert.pathname "/target/_self"
@browser.assert.url pathname: "/target/_self"

it "should open link in same window", ->
assert.equal @browser.tabs.index, 0
Expand All @@ -158,7 +158,7 @@ describe "IFrame", ->
@browser.clickLink "blank", done

it "should open link", ->
@browser.assert.pathname "/target/_blank"
@browser.assert.url pathname: "/target/_blank"

it "should open link in new window", ->
assert.equal @browser.tabs.length, 2
Expand All @@ -177,7 +177,7 @@ describe "IFrame", ->
@browser.wait done

it "should open link", ->
@browser.assert.pathname "/target/_top"
@browser.assert.url pathname: "/target/_top"

it "should open link in top window", ->
assert.equal @browser.tabs.length, 1
Expand All @@ -198,7 +198,7 @@ describe "IFrame", ->
assert.equal @browser.window.frames["child"].location.pathname, "/target/_parent"

it "should open link in child window", ->
@browser.assert.pathname "/iframe/top"
@browser.assert.url pathname: "/iframe/top"
assert.equal @browser.tabs.length, 1


Expand All @@ -213,7 +213,7 @@ describe "IFrame", ->
.then(done, done)

it "should open link", ->
@browser.assert.pathname "/target/new-window"
@browser.assert.url pathname: "/target/new-window"

it "should open link in new window", ->
assert.equal @browser.tabs.length, 2
Expand All @@ -233,7 +233,7 @@ describe "IFrame", ->
.then(done, done)

it "should open link", ->
@browser.assert.pathname "/target/existing-window"
@browser.assert.url pathname: "/target/existing-window"

it "should open link in existing window", ->
assert.equal @browser.tabs.length, 2
Expand Down

0 comments on commit 2cf6e95

Please sign in to comment.