Permalink
Browse files

Added a couple more assertions for classes.

  • Loading branch information...
1 parent 8979326 commit 3fd719c0d319dfe61407637294d1d57a87644994 @assaf committed Nov 30, 2012
Showing with 35 additions and 3 deletions.
  1. +14 −0 doc/new-assertions.md
  2. +21 −3 lib/zombie/assert.coffee
View
@@ -33,6 +33,10 @@ The following assertions are available:
Assert the named attribute of the selected element(s) has the expected value.
Fails if no elements found.
+`browser.assert.className(selector, className, message)`
+
+Asserts that selected element(s) has the that and only that class name.
+
`browser.assert.cookie(name, expected, message)`
Asserts that a cookie with the given name has the expected value.
@@ -61,6 +65,11 @@ value of the expression matches the expected value.
Asserts that the global (window) property has the expected value.
+`browser.assert.hasClass(selector, className, message)`
+
+Asserts that selected element(s) has the expected class name (it may have many
+other class names).
+
`browser.assert.hasFocus(selector, message)`
Asserts that selected element has the focus.
@@ -70,6 +79,11 @@ Asserts that selected element has the focus.
Asserts that selected input field (text field, text area, etc) has the expected
value.
+`browser.assert.hasNoClass(selector, className, message)`
+
+Asserts that selected element(s) does not have the expected class name (it may
+have many other class names).
+
`browser.assert.prompted(messageShown, message)`
Assert that browser prompted with a given message.
@@ -64,8 +64,7 @@ class Assert
# Assert that element matching selector exists.
element: (selector, message)->
- element = @browser.query(selector)
- assert selector, message || "Could not find element '#{selector}'"
+ @elements(selector, exactly: 1, message)
# Assert how many elements matching selector exist.
#
@@ -82,7 +81,7 @@ class Assert
if count.exactly
count = count.exactly
if typeof(count) == "number"
- message ||= "Expected #{count.exactly} elements matching '#{selector}', found #{elements.length}"
+ message ||= "Expected #{count} elements matching '#{selector}', found #{elements.length}"
assert.equal elements.length, count, message
else
if count.atLeast
@@ -102,6 +101,25 @@ class Assert
assert ~classNames.indexOf(expected),
message || "Expected element '#{selector}' to have class #{expected}, found #{classNames.join(", ")}"
+ # Asserts the selected element(s) doest not have the expected CSS class.
+ hasNoClass: (selector, expected, message)->
+ elements = @browser.queryAll(selector)
+ assert elements.length > 0, "Expected selector '#{selector}' to return one or more elements"
+ for element in elements
+ classNames = element.className.split(/\s+/)
+ assert classNames.indexOf(expected) == -1,
+ message || "Expected element '#{selector}' to not have class #{expected}, found #{classNames.join(", ")}"
+
+ # Asserts the selected element(s) has the expected class names.
+ className: (selector, expected, message)->
+ elements = @browser.queryAll(selector)
+ assert elements.length > 0, "Expected selector '#{selector}' to return one or more elements"
+ expected = expected.split(/\s+/).sort().join(" ")
+ for element in elements
+ actual = element.className.split(/\s+/).sort().join(" ")
+ assertMatch actual, expected,
+ message || "Expected element '#{selector}' to have class #{expected}, found #{actual}"
+
# Asserts the selected element(s) has the expected value for the named style
# property.
style: (selector, style, expected, message)->

0 comments on commit 3fd719c

Please sign in to comment.