Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- added 'attr', 'attrAny', 'attrAll', and 'tagName' tests

- added tests and documentation for the above
- incremented version
- updated README
  • Loading branch information...
commit 476780b1e1e15d2734cfa2e216ace6af696c381f 1 parent e987fce
@brianmhunt authored
View
19 README.md
@@ -29,6 +29,21 @@ Casper's Tester you can use (in this case with [Mocha][] and Chai):
</thead>
<tbody>
<tr>
+ <td><a href='casper-chai/blob/master/build/casper-chai.md#attr'>attr(attr_name)</a></td>
+ <td>True when one selector has the given attribute
+ </td>
+ </tr>
+ <tr>
+ <td><a href='casper-chai/blob/master/build/casper-chai.md#attrany'>attrAny(attr_name)</a></td>
+ <td>True when any selector has the given attribute
+ </td>
+ </tr>
+ <tr>
+ <td><a href='casper-chai/blob/master/build/casper-chai.md#attrall'>attrAll(attr_name)</a></td>
+ <td>True when all elements matching selector have the given attribute
+ </td>
+ </tr>
+ <tr>
<td><a href='casper-chai/blob/master/build/casper-chai.md#fieldvalue'>fieldValue(value)</a></td>
<td>
the named input provided has the given value
@@ -55,6 +70,10 @@ Casper's Tester you can use (in this case with [Mocha][] and Chai):
<td>the current Title matches</td>
</tr>
<tr>
+ <td><a href='casper-chai/blob/master/build/casper-chai.md#tagname'>tagName(valid_tags)</a></td>
+ <td>all elements matching the selectors are one of the given tags</td>
+ </tr>
+ <tr>
<td><a href='casper-chai/blob/master/build/casper-chai.md#textindom'>textInDOM</a></td>
<td>the text can be found in the DOM</td>
</tr>
View
153 build/casper-chai.js
@@ -1,4 +1,4 @@
-/* casper-chai version 0.1.5 */
+/* casper-chai version 0.1.6 */
// -- from: lib/casper-chai.coffee -- \\
@@ -17,36 +17,16 @@
var casperChai;
casperChai = function(_chai, utils) {
- var assert, methods, properties, _addMethod, _addProperty, _exprAsFunction, _matches;
+ var assert, methods, properties, _addMethod, _addProperty, _exprAsFunction, _get_attrs, _matches;
properties = [];
methods = [];
assert = _chai.assert;
- /*
- Utilities
- ---------
- */
-
_addProperty = function(name, func) {
return _chai.Assertion.addProperty(name, func);
};
_addMethod = function(name, method) {
return _chai.Assertion.addMethod(name, method);
};
- /*
- @@@@ _exprAsFunction
-
- Given an expression, turn it in to something that can be
- evaluated remotely.
-
- `expr` may be
-
- 1. a bare string e.g. "false" or "return true";
-
- 2. a function string e.g. "function () { return true }"
-
- 3. an actual function e.g. function () { return 'hello' }
- */
-
_exprAsFunction = function(expr) {
var fn;
if (_.isFunction(expr)) {
@@ -66,15 +46,6 @@
}
return fn;
};
- /*
- @@@@ _matches
-
- Returns true if a `against` matches `value`. The `against` variable
- can be a string or regular expression.
-
- If `isEqualFallback` is true then we also try `_.isEqual`.
- */
-
_matches = function(against, value, isEqualFallback) {
var regex;
if (isEqualFallback == null) {
@@ -94,6 +65,23 @@
}
return regex.test(value);
};
+ _get_attrs = function(selector, attr) {
+ var attrs, fn;
+ fn = function(selector, _attr) {
+ var _casper_attrs, _casper_chai_elements;
+ _casper_chai_elements = __utils__.findAll(selector);
+ _casper_attrs = [];
+ Array.prototype.forEach.call(_casper_chai_elements, function(e) {
+ return _casper_attrs.push(e.getAttribute(_attr));
+ });
+ return _casper_attrs;
+ };
+ attrs = casper.evaluate(fn, {
+ _selector: selector,
+ _attr: attr
+ });
+ return attrs;
+ };
/*
Chai Tests
----------
@@ -102,6 +90,60 @@
*/
/*
+ @@@@ attr(attribute_name)
+
+ True when the attribute `attribute_name` on `selector` is true.
+
+ If the selector matches more than one element with the attribute set, this
+ will fail. In those cases [attrAll](#attrall) or [attrAny](#attrany).
+
+
+ ```javascript
+ expect("#my_header").to.have.attr('class')
+ ```
+ */
+
+ _addMethod('attr', function(attr) {
+ var attr_v, attrs, selector;
+ selector = this._obj;
+ attrs = _get_attrs(selector, attr);
+ assert.equal(attrs.length, 1, "Expected " + selector + " to have one match, but it had " + attrs.length);
+ attr_v = attrs[0];
+ return this.assert(attr_v, "Expected selector " + selector + " to have attribute " + attr + ", but it did not", ("Expected selector " + selector + " to not have attribute " + attr + ", ") + ("but it was " + attr_v));
+ });
+ /*
+ @@@@ attrAny(attribute_name)
+
+ True when an attribute is set on at least one of the given selectors.
+
+ ```javascript
+ expect("div.menu li").to.have.attrAny('selected')
+ ```
+ */
+
+ _addMethod('attrAny', function(attr) {
+ var attrs, selector;
+ selector = this._obj;
+ attrs = _get_attrs(selector, attr);
+ return this.assert(_.any(attrs), ("Expected one element matching selector " + selector + " to have attribute") + ("" + attr + " set, but none did"), ("Expected no elements matching selector " + selector + " to have attribute") + ("" + attr + " set, but at least one did"));
+ });
+ /*
+ @@@@ attrAll(attribute_name)
+
+ True when an attribute is set on all of the given selectors.
+
+ ```javascript
+ expect("div.menu li").to.have.attrAll('class')
+ ```
+ */
+
+ _addMethod('attrAll', function(attr) {
+ var attrs, selector;
+ selector = this._obj;
+ attrs = _get_attrs(selector, attr);
+ return this.assert(_.all(attrs), ("Expected all elements matching selector " + selector + " to have attribute") + ("" + attr + " set, but one did not have it"), ("Expected one elements matching selector " + selector + " to not have ") + (" attribute " + attr + " set, but they all had it"));
+ });
+ /*
@@@@ fieldValue
@@ -176,9 +218,11 @@
[`trueOnRemote`](#trueonremote).
```javascript
- expect("return 123").to.matchOnRemote(123)<br/>
+ expect("return 123").to.matchOnRemote(123)
"typeof jQuery".should.not.matchOnRemote('undefined')
+
+ "123.toString()".should.matchOnRemote(/\d+/)
```
or an example in CoffeeScript
@@ -229,6 +273,49 @@
return this.assert(_matches(matcher, currentUrl), 'expected url #{exp} to match #{this}, but it did not', 'expected url #{exp} to not match #{this}, but it did');
});
/*
+ @@@@ tagName
+
+ All elements matching the given selector are one of the given tag names.
+
+ In other words, given a selector, all tags must be one of the given tags.
+ Note that those tags need not appear in the selector.
+
+ ```javascript
+ ".menuItem".has.tagName('li')
+
+ "menu li *".has.tagName(['a', 'span'])
+ ```
+ */
+
+ _addMethod('tagName', function(ok_names) {
+ var diff, selector, tagnames, _get_tagnames;
+ selector = this._obj;
+ if (_.isString(ok_names)) {
+ ok_names = [ok_names];
+ } else if (!_.isArray(ok_names)) {
+ assert.ok(false, "tagName must be a string or list, it was " + typeof ok_names);
+ }
+ _get_tagnames = function(selector) {
+ var fn, _tagnames;
+ fn = function(selector, _attr) {
+ var _casper_chai_elements, _casper_tags;
+ _casper_chai_elements = __utils__.findAll(selector);
+ _casper_tags = [];
+ Array.prototype.forEach.call(_casper_chai_elements, function(e) {
+ return _casper_tags.push(e.tagName.toLowerCase());
+ });
+ return _casper_tags;
+ };
+ _tagnames = casper.evaluate(fn, {
+ _selector: selector
+ });
+ return _tagnames;
+ };
+ tagnames = _get_tagnames(selector);
+ diff = _.difference(tagnames, ok_names);
+ return this.assert(diff.length === 0, ("Expected " + selector + " to have only tags " + ok_names + ", but there was") + ("tag(s) " + diff + " appeared"), ("Expected " + selector + " to have tags other than " + ok_names + ", but ") + "those were the only tags that appeared");
+ });
+ /*
@@@@ textInDOM
The given text can be found in the phantom browser's DOM.
@@ -261,7 +348,7 @@
var selector, text;
selector = this._obj;
text = casper.fetchText(selector);
- return this.assert(_matches(matcher, text), "expected '" + selector + "' to match " + matcher + ", but it did not", "expected '" + selector + "' to not match " + matcher + ", but it did");
+ return this.assert(_matches(matcher, text), "expected '" + selector + "' to match " + matcher + ", but it was \"" + text + "\"", "expected '" + selector + "' to not match " + matcher + ", but it did");
});
/*
@@@@ trueOnRemote
View
39 build/casper-chai.md
@@ -14,6 +14,31 @@ Chai Tests
----------
The following are the tests that are added onto Chai Assertion.
+#### attr(attribute_name)
+
+True when the attribute `attribute_name` on `selector` is true.
+
+If the selector matches more than one element with the attribute set, this
+will fail. In those cases [attrAll](#attrall) or [attrAny](#attrany).
+
+
+```javascript
+expect("#my_header").to.have.attr('class')
+```
+#### attrAny(attribute_name)
+
+True when an attribute is set on at least one of the given selectors.
+
+```javascript
+expect("div.menu li").to.have.attrAny('selected')
+```
+#### attrAll(attribute_name)
+
+True when an attribute is set on all of the given selectors.
+
+```javascript
+expect("div.menu li").to.have.attrAll('class')
+```
#### fieldValue
@@ -53,7 +78,7 @@ expression. The evaluation is the same as for
[`trueOnRemote`](#trueonremote).
```javascript
-expect("return 123").to.matchOnRemote(123)<br/>
+expect("return 123").to.matchOnRemote(123)
"typeof jQuery".should.not.matchOnRemote('undefined')
@@ -80,6 +105,18 @@ True when the current URL matches the given string or regular expression
```javascript
expect(/https:\/\//).to.matchCurrentUrl;
```
+#### tagName
+
+All elements matching the given selector are one of the given tag names.
+
+In other words, given a selector, all tags must be one of the given tags.
+Note that those tags need not appear in the selector.
+
+```javascript
+".menuItem".has.tagName('li')
+
+"menu li *".has.tagName(['a', 'span'])
+```
#### textInDOM
The given text can be found in the phantom browser's DOM.
View
2  build/casper-chai.min.js
@@ -1 +1 @@
-(function(){var t;t=function(t,e){var i,s,r,n,o,u,c;r=[];s=[];i=t.assert;o=function(e,i){return t.Assertion.addProperty(e,i)};n=function(e,i){return t.Assertion.addMethod(e,i)};u=function(t){var e;if(_.isFunction(t)){e=t}else if(_.isString(t)){if(/^\s*function\s+/.test(t)){e=t}else{if(t.indexOf("return ")===-1){e="function () { return "+t+" }"}else{e="function () { "+t+" }"}}}else{this.assert(false,"Expression "+t+" must be a function, or a string")}return e};c=function(t,e,i){var s;if(i==null){i=false}if(typeof t==="string"){s=new RegExp("^"+t+"$")}else if(_.isRegExp(t)){s=t}else if(i){if(toString.call(e)==="[object RuntimeArray]"){e=_.toArray(e)}return _.isEqual(t,e)}else{throw new Error("Test received "+t+", but expected string",+" or regular expression.")}return s.test(e)};n("fieldValue",function(t){var e,i,s;s=this._obj;if(_.isString(s)){}else{expect(s).to.be.inDOM}e=function(t){return __utils__.getFieldValue(t)};i=casper.evaluate(e,{selector:s});return this.assert(i===t,"expected field(s) "+s+" to have value "+t+", "+("but it was "+i),"expected field(s) "+s+" to not have value "+t+", "+"but it was")});o("inDOM",function(){var t;t=this._obj;return this.assert(casper.exists(t),"expected selector #{this} to be in the DOM, but it was not","expected selector #{this} to not be in the DOM, but it was")});o("loaded",function(){var t;t=this._obj;return this.assert(casper.resourceExists(t),"expected resource #{this} to exist, but it does not","expected resource #{this} to not exist, but it does")});n("matchOnRemote",function(t){var e,i,s;e=this._obj;i=u(e);s=casper.evaluate(i);return this.assert(c(t,s,true),"expected "+this._obj+" ("+i+" = "+s+") to match "+t,"expected "+this._obj+" ("+i+") to not match "+t+", but it did")});o("matchTitle",function(){var t,e;t=this._obj;e=casper.getTitle();return this.assert(c(t,e),"expected title #{this} to match #{exp}, but it did not","expected title #{this} to not match #{exp}, but it did")});o("matchCurrentUrl",function(){var t,e;e=this._obj;t=casper.getCurrentUrl();return this.assert(c(e,t),"expected url #{exp} to match #{this}, but it did not","expected url #{exp} to not match #{this}, but it did")});o("textInDOM",function(){var t,e;e=this._obj;t=casper.evaluate(function(){return document.body.textContent||document.body.innerText});return this.assert(t.indexOf(e)!==-1,"expected text #{this} to be in the document, but it was not","expected text #{this} to not be in the document, but it was found")});n("textMatch",function(t){var e,i;e=this._obj;i=casper.fetchText(e);return this.assert(c(t,i),"expected '"+e+"' to match "+t+", but it did not","expected '"+e+"' to not match "+t+", but it did")});o("trueOnRemote",function(){var t,e,i;t=this._obj;e=u(t);i=casper.evaluate(e);return this.assert(i,"expected expression "+this._obj+" to be true, but it was "+i,"expected expression "+this._obj+" to not be true, but itw as "+i)});return o("visible",function(){var t;t=this._obj;expect(t).to.be.inDOM;return this.assert(casper.visible(t),"expected selector #{this} to be visible, but it was not","expected selector #{this} to not be, but it was")})};if(typeof require==="function"&&typeof exports==="object"&&typeof module==="object"){module.exports=t}else if(typeof define==="function"&&define.amd){define(function(){return t})}else{chai.use(t)}}).call(this);
+(function(){var t;t=function(t,e){var r,i,s,n,o,a,u,c;s=[];i=[];r=t.assert;o=function(e,r){return t.Assertion.addProperty(e,r)};n=function(e,r){return t.Assertion.addMethod(e,r)};a=function(t){var e;if(_.isFunction(t)){e=t}else if(_.isString(t)){if(/^\s*function\s+/.test(t)){e=t}else{if(t.indexOf("return ")===-1){e="function () { return "+t+" }"}else{e="function () { "+t+" }"}}}else{this.assert(false,"Expression "+t+" must be a function, or a string")}return e};c=function(t,e,r){var i;if(r==null){r=false}if(typeof t==="string"){i=new RegExp("^"+t+"$")}else if(_.isRegExp(t)){i=t}else if(r){if(toString.call(e)==="[object RuntimeArray]"){e=_.toArray(e)}return _.isEqual(t,e)}else{throw new Error("Test received "+t+", but expected string",+" or regular expression.")}return i.test(e)};u=function(t,e){var r,i;i=function(t,e){var r,i;i=__utils__.findAll(t);r=[];Array.prototype.forEach.call(i,function(t){return r.push(t.getAttribute(e))});return r};r=casper.evaluate(i,{_selector:t,_attr:e});return r};n("attr",function(t){var e,i,s;s=this._obj;i=u(s,t);r.equal(i.length,1,"Expected "+s+" to have one match, but it had "+i.length);e=i[0];return this.assert(e,"Expected selector "+s+" to have attribute "+t+", but it did not","Expected selector "+s+" to not have attribute "+t+", "+("but it was "+e))});n("attrAny",function(t){var e,r;r=this._obj;e=u(r,t);return this.assert(_.any(e),"Expected one element matching selector "+r+" to have attribute"+(""+t+" set, but none did"),"Expected no elements matching selector "+r+" to have attribute"+(""+t+" set, but at least one did"))});n("attrAll",function(t){var e,r;r=this._obj;e=u(r,t);return this.assert(_.all(e),"Expected all elements matching selector "+r+" to have attribute"+(""+t+" set, but one did not have it"),"Expected one elements matching selector "+r+" to not have "+(" attribute "+t+" set, but they all had it"))});n("fieldValue",function(t){var e,r,i;i=this._obj;if(_.isString(i)){}else{expect(i).to.be.inDOM}e=function(t){return __utils__.getFieldValue(t)};r=casper.evaluate(e,{selector:i});return this.assert(r===t,"expected field(s) "+i+" to have value "+t+", "+("but it was "+r),"expected field(s) "+i+" to not have value "+t+", "+"but it was")});o("inDOM",function(){var t;t=this._obj;return this.assert(casper.exists(t),"expected selector #{this} to be in the DOM, but it was not","expected selector #{this} to not be in the DOM, but it was")});o("loaded",function(){var t;t=this._obj;return this.assert(casper.resourceExists(t),"expected resource #{this} to exist, but it does not","expected resource #{this} to not exist, but it does")});n("matchOnRemote",function(t){var e,r,i;e=this._obj;r=a(e);i=casper.evaluate(r);return this.assert(c(t,i,true),"expected "+this._obj+" ("+r+" = "+i+") to match "+t,"expected "+this._obj+" ("+r+") to not match "+t+", but it did")});o("matchTitle",function(){var t,e;t=this._obj;e=casper.getTitle();return this.assert(c(t,e),"expected title #{this} to match #{exp}, but it did not","expected title #{this} to not match #{exp}, but it did")});o("matchCurrentUrl",function(){var t,e;e=this._obj;t=casper.getCurrentUrl();return this.assert(c(e,t),"expected url #{exp} to match #{this}, but it did not","expected url #{exp} to not match #{this}, but it did")});n("tagName",function(t){var e,i,s,n;i=this._obj;if(_.isString(t)){t=[t]}else if(!_.isArray(t)){r.ok(false,"tagName must be a string or list, it was "+typeof t)}n=function(t){var e,r;e=function(t,e){var r,i;r=__utils__.findAll(t);i=[];Array.prototype.forEach.call(r,function(t){return i.push(t.tagName.toLowerCase())});return i};r=casper.evaluate(e,{_selector:t});return r};s=n(i);e=_.difference(s,t);return this.assert(e.length===0,"Expected "+i+" to have only tags "+t+", but there was"+("tag(s) "+e+" appeared"),"Expected "+i+" to have tags other than "+t+", but "+"those were the only tags that appeared")});o("textInDOM",function(){var t,e;e=this._obj;t=casper.evaluate(function(){return document.body.textContent||document.body.innerText});return this.assert(t.indexOf(e)!==-1,"expected text #{this} to be in the document, but it was not","expected text #{this} to not be in the document, but it was found")});n("textMatch",function(t){var e,r;e=this._obj;r=casper.fetchText(e);return this.assert(c(t,r),"expected '"+e+"' to match "+t+', but it was "'+r+'"',"expected '"+e+"' to not match "+t+", but it did")});o("trueOnRemote",function(){var t,e,r;t=this._obj;e=a(t);r=casper.evaluate(e);return this.assert(r,"expected expression "+this._obj+" to be true, but it was "+r,"expected expression "+this._obj+" to not be true, but itw as "+r)});return o("visible",function(){var t;t=this._obj;expect(t).to.be.inDOM;return this.assert(casper.visible(t),"expected selector #{this} to be visible, but it was not","expected selector #{this} to not be, but it was")})};if(typeof require==="function"&&typeof exports==="object"&&typeof module==="object"){module.exports=t}else if(typeof define==="function"&&define.amd){define(function(){return t})}else{chai.use(t)}}).call(this);
View
147 lib/casper-chai.coffee
@@ -97,7 +97,31 @@ casperChai = (_chai, utils) ->
# console.log("RegExp", regex, "value", value)
return regex.test(value)
-
+
+ #
+ # _get_attrs
+ # ~~~~~~~~~~
+ #
+ # Return all values of `attr` on `selector` in the DOM.
+ #
+ #
+ #
+ _get_attrs = (selector, attr) ->
+ fn = (selector, _attr) ->
+ _casper_chai_elements = __utils__.findAll(selector)
+ _casper_attrs = []
+ Array.prototype.forEach.call(_casper_chai_elements, (e) ->
+ _casper_attrs.push(e.getAttribute(_attr))
+ )
+ return _casper_attrs
+
+ attrs = casper.evaluate(fn,
+ _selector: selector
+ _attr: attr
+ )
+
+ return attrs
+
###
Chai Tests
@@ -105,8 +129,79 @@ casperChai = (_chai, utils) ->
The following are the tests that are added onto Chai Assertion.
###
+
+
+
+
+ ###
+ @@@@ attr(attribute_name)
+
+ True when the attribute `attribute_name` on `selector` is true.
+
+ If the selector matches more than one element with the attribute set, this
+ will fail. In those cases [attrAll](#attrall) or [attrAny](#attrany).
+
+
+ ```javascript
+ expect("#my_header").to.have.attr('class')
+ ```
+ ###
+ _addMethod 'attr', (attr) ->
+ selector = @_obj
+
+ attrs = _get_attrs(selector, attr)
+
+ assert.equal(attrs.length, 1,
+ "Expected #{selector} to have one match, but it had #{attrs.length}")
+
+ attr_v = attrs[0]
+
+ @assert(attr_v,
+ "Expected selector #{selector} to have attribute #{attr}, but it did not",
+ "Expected selector #{selector} to not have attribute #{attr}, " +
+ "but it was #{attr_v}"
+ )
+
+ ###
+ @@@@ attrAny(attribute_name)
+
+ True when an attribute is set on at least one of the given selectors.
+
+ ```javascript
+ expect("div.menu li").to.have.attrAny('selected')
+ ```
+ ###
+ _addMethod 'attrAny', (attr) ->
+ selector = @_obj
+ attrs = _get_attrs(selector, attr)
+
+ @assert(_.any(attrs),
+ "Expected one element matching selector #{selector} to have attribute" +
+ "#{attr} set, but none did",
+ "Expected no elements matching selector #{selector} to have attribute" +
+ "#{attr} set, but at least one did"
+ )
+
+ ###
+ @@@@ attrAll(attribute_name)
+
+ True when an attribute is set on all of the given selectors.
+
+ ```javascript
+ expect("div.menu li").to.have.attrAll('class')
+ ```
+ ###
+ _addMethod 'attrAll', (attr) ->
+ selector = @_obj
+ attrs = _get_attrs(selector, attr)
+
+ @assert(_.all(attrs),
+ "Expected all elements matching selector #{selector} to have attribute" +
+ "#{attr} set, but one did not have it",
+ "Expected one elements matching selector #{selector} to not have " +
+ " attribute #{attr} set, but they all had it"
+ )
-
###
@@@@ fieldValue
@@ -253,6 +348,52 @@ casperChai = (_chai, utils) ->
)
###
+ @@@@ tagName
+
+ All elements matching the given selector are one of the given tag names.
+
+ In other words, given a selector, all tags must be one of the given tags.
+ Note that those tags need not appear in the selector.
+
+ ```javascript
+ ".menuItem".has.tagName('li')
+
+ "menu li *".has.tagName(['a', 'span'])
+ ```
+ ###
+ _addMethod 'tagName', (ok_names) ->
+ selector = @_obj
+
+ if _.isString(ok_names)
+ ok_names = [ok_names]
+ else if not _.isArray(ok_names)
+ assert.ok(false, "tagName must be a string or list, it was " +
+ typeof ok_names)
+
+ _get_tagnames = (selector) ->
+ fn = (selector, _attr) ->
+ _casper_chai_elements = __utils__.findAll(selector)
+ _casper_tags = []
+ Array.prototype.forEach.call(_casper_chai_elements, (e) ->
+ _casper_tags.push(e.tagName.toLowerCase())
+ )
+ return _casper_tags
+
+ _tagnames = casper.evaluate(fn, _selector: selector)
+ return _tagnames
+
+ tagnames = _get_tagnames(selector)
+
+ diff = _.difference(tagnames, ok_names)
+
+ @assert(diff.length == 0,
+ "Expected #{selector} to have only tags #{ok_names}, but there was" +
+ "tag(s) #{diff} appeared",
+ "Expected #{selector} to have tags other than #{ok_names}, but " +
+ "those were the only tags that appeared",
+ )
+
+ ###
@@@@ textInDOM
The given text can be found in the phantom browser's DOM.
@@ -285,7 +426,7 @@ casperChai = (_chai, utils) ->
selector = @_obj
text = casper.fetchText(selector)
@assert(_matches(matcher, text),
- "expected '#{selector}' to match #{matcher}, but it did not",
+ "expected '#{selector}' to match #{matcher}, but it was \"#{text}\"",
"expected '#{selector}' to not match #{matcher}, but it did"
)
View
2  package.json
@@ -7,7 +7,7 @@
"casperjs",
"phantomjs"
],
- "version": "0.1.5",
+ "version": "0.1.6",
"author": "Brian M Hunt <brianmhunt@gmail.com>",
"license": "MIT",
"repository": {
View
62 test/common.coffee
@@ -8,6 +8,67 @@ describe "Casper-Chai addons to Chai", ->
before ->
casper.open "simple.html" # local file
+ describe "the attr method", ->
+ it "matches 'class' by id", ->
+ casper.then ->
+ expect("#waldo").to.have.attr('class')
+
+ it "matches class by id", ->
+ casper.then ->
+ expect("#waldo").to.not.have.attr('classless')
+
+ it 'returns false where attribute is empty', ->
+ casper.then ->
+ expect("#waldo").to.not.have.attr("data-empty")
+
+ it 'fails when more than one attribute is returned', ->
+ casper.then ->
+ # two div classes have 'class'; explicitly fail - use attrAny or
+ # attrAll
+ failed = false
+ try
+ expect("div").to.not.have.attr('class')
+ catch err
+ failed = true
+
+ assert.ok(failed, "Expected failure, but `expect` succeeded.")
+
+ describe "the attrAny method", ->
+ it "finds one div with an 'id' attribute", ->
+ casper.then ->
+ expect('div').to.have.attrAny("id")
+
+ it "finds divs with an 'class' attribute", ->
+ casper.then ->
+ # all divs have the class attribute
+ expect('div').to.have.attrAny("class")
+
+ it "finds no divs with an 'data-empty' attribute", ->
+ casper.then ->
+ expect('div').to.not.have.attrAny("data-empty")
+
+ describe "the attrAll method", ->
+ it "finds not all divs have an 'id' attribute", ->
+ casper.then ->
+ expect('div').to.not.have.attrAll('id')
+
+ it "finds that all divs have a 'class' attribute", ->
+ casper.then ->
+ expect("div").to.have.attrAll('class')
+
+ describe "the tagName method", ->
+ it "finds that the '.says' tags are all 'divs'", ->
+ casper.then ->
+ expect(".says").to.have.tagName('div')
+
+ it "finds that the .tagged tags are divs and blockquotes", ->
+ casper.then ->
+ expect(".says").to.have.tagName(['div', 'blockquote'])
+
+ it "finds that the children of blockquotes are em and small tags", ->
+ casper.then ->
+ expect("blockquote *").to.have.tagName(['em', 'small'])
+
describe "the inDOM property", ->
it "matches a selector in the DOM", ->
casper.then ->
@@ -204,7 +265,6 @@ describe "Casper-Chai addons to Chai", ->
expect(remote_value).to.equal("The Titleeee")
describe "test for loaded", ->
-
it "checks for jQuery when it is not loaded", ->
casper.then ->
expect(-> typeof jQuery).to.matchOnRemote("undefined")
View
7 test/simple.html
@@ -6,12 +6,13 @@
<body>
<h1 id="header_1">A Header</h1>
- <blockquote>
- “Do what you can, with what you have, where you are.”
+ <blockquote class='tagged'>
+ “Do what you can, with what <em>you</em> have, where you are.”
<small>THEODORE ROOSEVELT.</small>
</blockquote>
- <div id='waldo' class='says' style='display: none'>Simon says</div>
+ <div id='waldo' class='says tagged' style='display: none'
+ data-empty=''>Simon says</div>
<div class='says'>Also says</div>
Please sign in to comment.
Something went wrong with that request. Please try again.