Skip to content

Commit c39e258

Browse files
committed
added elementHasNoChildren with docs and tests
1 parent fd67f9e commit c39e258

File tree

6 files changed

+138
-1
lines changed

6 files changed

+138
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
###*
2+
* Assert that the element identified by the selector doesn't have children nodes
3+
*
4+
* h3 Examples:
5+
*
6+
* browser
7+
* .url("http://www.github.com")
8+
* .assert.elementHasNoChildren("#list-of-tasks")
9+
*
10+
* @author maxgalbu
11+
* @param {String} selector - the element selector
12+
* @param {String} [msg] - output to identify the assertion
13+
###
14+
15+
util = require('util');
16+
17+
exports.assertion = (selector, msg = null) ->
18+
@message = msg || util.format('Testing if element <%s> doesn\'t have child nodes', selector);
19+
@expected = true;
20+
21+
this.pass = (value) =>
22+
return value == @expected;
23+
24+
this.value = (result) ->
25+
return result.value;
26+
27+
this.command = (callback) ->
28+
params = [selector];
29+
execute = (selector) ->
30+
elements = document.querySelectorAll(selector);
31+
if !elements.length
32+
return false;
33+
34+
element = elements[0];
35+
return element.children.length == 0;
36+
execcallback = (result) =>
37+
if callback
38+
callback.call(this, result);
39+
40+
return this.api.execute(execute, params, execcallback);
41+
42+
return;

docs/elementHasNoChildren.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
<!-- Start coffee/assertions/elementHasNoChildren.coffee -->
4+
5+
Assert that the element identified by the selector doesn't have children nodes
6+
### Examples:
7+
8+
browser
9+
.url("http://www.github.com")
10+
.assert.elementHasNoChildren("#list-of-tasks")
11+
12+
Author: maxgalbu
13+
14+
### Params:
15+
16+
* **String** *selector* - the element selector
17+
* **String** *[msg]* - output to identify the assertion
18+
19+
<!-- End coffee/assertions/elementHasNoChildren.coffee -->
20+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Assert that the element identified by the selector doesn't have children nodes
4+
*
5+
* h3 Examples:
6+
*
7+
* browser
8+
* .url("http://www.github.com")
9+
* .assert.elementHasNoChildren("#list-of-tasks")
10+
*
11+
* @author maxgalbu
12+
* @param {String} selector - the element selector
13+
* @param {String} [msg] - output to identify the assertion
14+
*/
15+
var util;
16+
17+
util = require('util');
18+
19+
exports.assertion = function(selector, msg) {
20+
if (msg == null) {
21+
msg = null;
22+
}
23+
this.message = msg || util.format('Testing if element <%s> doesn\'t have child nodes', selector);
24+
this.expected = true;
25+
this.pass = (function(_this) {
26+
return function(value) {
27+
return value === _this.expected;
28+
};
29+
})(this);
30+
this.value = function(result) {
31+
return result.value;
32+
};
33+
this.command = function(callback) {
34+
var execcallback, execute, params;
35+
params = [selector];
36+
execute = function(selector) {
37+
var element, elements;
38+
elements = document.querySelectorAll(selector);
39+
if (!elements.length) {
40+
return false;
41+
}
42+
element = elements[0];
43+
return element.children.length === 0;
44+
};
45+
execcallback = (function(_this) {
46+
return function(result) {
47+
if (callback) {
48+
return callback.call(_this, result);
49+
}
50+
};
51+
})(this);
52+
return this.api.execute(execute, params, execcallback);
53+
};
54+
};

tests/html/nochildren.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<ul class="myclass">
6+
</ul>
7+
8+
<div class="text">
9+
some text
10+
</div>
11+
</body>
12+
</html>

tests/runTests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ module.exports = {
1111
.end();
1212
},
1313

14+
"test elementHasNoChildren": function(browser) {
15+
return browser
16+
.url(baseurl+"/nochildren")
17+
.assert.elementHasNoChildren(".myclass")
18+
.assert.elementHasNoChildren(".text")
19+
.end();
20+
},
21+
1422
"test saveElementScreenshot": function(browser) {
1523
var imageFileName = "test.png";
1624

tests/setMocks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ monkeyPatchedMSC.mockHTMLResponse('/waitForAttribute', "html/attribute.html");
4242
monkeyPatchedMSC.mockHTMLResponse('/waitForText', "html/text.html");
4343
monkeyPatchedMSC.mockHTMLResponse('/waitForTitle', "html/title.html");
4444
monkeyPatchedMSC.mockHTMLResponse('/saveElementScreenshot', "html/screenshot.html");
45-
monkeyPatchedMSC.mockHTMLResponse('/children', "html/children.html");
45+
monkeyPatchedMSC.mockHTMLResponse('/children', "html/children.html");
46+
monkeyPatchedMSC.mockHTMLResponse('/nochildren', "html/nochildren.html");

0 commit comments

Comments
 (0)