Skip to content

Commit f85300c

Browse files
committed
fix custom commands and assertions to work from page object's sections
1 parent 1bf1389 commit f85300c

21 files changed

+649
-48
lines changed

coffee/assertions/elementHasChildren.coffee

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
util = require('util');
2222

23+
#=include ../getMultipleSelectors.coffee
24+
2325
exports.assertion = (selector, children_selectors = "", msg = null) ->
2426
@message = msg;
2527
if !@message
@@ -36,17 +38,20 @@ exports.assertion = (selector, children_selectors = "", msg = null) ->
3638
return !!result.value;
3739

3840
this.command = (callback) ->
41+
selector = getMultipleSelectors(selector)
42+
children_selectors = getMultipleSelectors(children_selectors)
3943
params = [selector, children_selectors];
4044
execute = (selector, children_selectors) ->
41-
elements = document.querySelectorAll(selector);
42-
if !elements.length
43-
return false;
45+
#=include ../getElementFromSelector.coffee
4446

45-
element = elements[0];
47+
element = getElementFromSelector(selector);
48+
if !element
49+
return false
50+
4651
if !children_selectors
4752
return element.children.length != 0;
4853
else
49-
children = element.querySelectorAll(children_selectors);
54+
children = getElementFromSelector(children_selectors, return_all: true, parent_element: element);
5055
return children.length != 0;
5156
execcallback = (result) =>
5257
if callback

coffee/assertions/elementHasNoChildren.coffee

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
util = require('util');
1616

17+
#=include ../getMultipleSelectors.coffee
18+
1719
exports.assertion = (selector, msg = null) ->
1820
@message = msg || util.format('Testing if element <%s> doesn\'t have child nodes', selector);
1921
@expected = true;
@@ -25,13 +27,14 @@ exports.assertion = (selector, msg = null) ->
2527
return result.value;
2628

2729
this.command = (callback) ->
30+
selector = getMultipleSelectors(selector)
2831
params = [selector];
2932
execute = (selector) ->
30-
elements = document.querySelectorAll(selector);
31-
if !elements.length
32-
return false;
33+
#=include ../getElementFromSelector.coffee
3334

34-
element = elements[0];
35+
element = getElementFromSelector(selector);
36+
if !element
37+
return false
3538
return element.children.length == 0;
3639
execcallback = (result) =>
3740
if callback

coffee/commands/jqueryClick.coffee

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
* @param {Function} [callback] - function that will be called after the element is clicked
1212
###
1313

14+
#=include ../getMultipleSelectors.coffee
15+
1416
module.exports.command = (selector, callback) ->
17+
selector = getMultipleSelectors(selector)
1518
params = [selector];
1619

1720
execute = (selector) ->
18-
element = $(selector);
21+
#=include ../getElementFromSelector.coffee
22+
23+
element = getElementFromSelector(selector, jquery: true);
1924
element[0].click() if element.length
2025
return true;
2126
execcallback = =>

coffee/commands/jqueryElement.coffee

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
* @param {Function} callback - function that will be called with the element as argument
1616
###
1717

18+
#=include ../getMultipleSelectors.coffee
19+
1820
module.exports.command = (selector, callback) ->
21+
selector = getMultipleSelectors(selector)
1922
params = [selector];
2023

2124
execute = (selector) ->
22-
return $(selector).get(0);
25+
#=include ../getElementFromSelector.coffee
26+
27+
element = getElementFromSelector(selector, jquery: true);
28+
return element.get(0);
2329
execcallback = (result) =>
2430
if callback
2531
callback.call(this, result.value);

coffee/commands/setSelect2Data.coffee

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
* @param {Function} [callback] - function that will be called after the element's value has been set
1212
###
1313

14+
#=include ../getMultipleSelectors.coffee
15+
1416
module.exports.command = (selector, data, callback) ->
17+
selector = getMultipleSelectors(selector)
1518
params = [selector, data];
1619

1720
execute = (selector, data) ->
18-
$(selector).select2("data", data);
19-
$(selector).trigger("change");
21+
#=include ../getElementFromSelector.coffee
22+
23+
element = getElementFromSelector(selector, jquery: true);
24+
element.select2("data", data);
25+
element.trigger("change");
2026
return true;
2127
execcallback = (result) =>
2228
if callback

coffee/commands/setSelect2Value.coffee

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
* @param {Function} [callback] - function that will be called after the element's value has been set
1212
###
1313

14+
#=include ../getMultipleSelectors.coffee
15+
1416
module.exports.command = (selector, value, callback) ->
17+
selector = getMultipleSelectors(selector)
1518
params = [selector, value];
1619

1720
execute = (selector, value) ->
18-
$(selector).select2("val", value);
19-
$(selector).trigger("change");
21+
#=include ../getElementFromSelector.coffee
22+
23+
element = getElementFromSelector(selector, jquery: true);
24+
element.select2("val", value);
25+
element.trigger("change");
2026
return true;
2127
execcallback = (result) =>
2228
if callback

coffee/commands/setValueAndTrigger.coffee

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
* @param {Function} [callback] - function that will be called after the change event is triggered
1212
###
1313

14+
#=include ../getMultipleSelectors.coffee
15+
1416
module.exports.command = (selector, value, callback) ->
17+
selector = getMultipleSelectors(selector)
1518
params = [selector, value];
1619

1720
execute = (selector, value) ->
18-
$(selector).val(value);
19-
$(selector).trigger("change");
21+
#=include ../getElementFromSelector.coffee
22+
23+
element = getElementFromSelector(selector, jquery: true);
24+
element.val(value);
25+
element.trigger("change");
2026
return true;
2127
execcallback = (result) =>
2228
if callback
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#The param "selector" can be an array of selectors, or a string.
2+
#If there's an array i get the parent element, then use jQuery.find()
3+
#or element.querySelectorAll() to find the actual element
4+
getElementFromSelector = (selector, options = {jquery: false}) ->
5+
if Array.isArray(selector)
6+
section_selector = selector[0]
7+
selector = selector[1]
8+
9+
if options.jquery
10+
return $(section_selector).find(selector)
11+
else
12+
section_element = document.querySelectorAll(section_selector)
13+
if !section_element.length
14+
return null;
15+
16+
section_element = section_element[0]
17+
if options.parent_element
18+
section_element = parent_element
19+
20+
elements = section_element.querySelectorAll(selector)
21+
if elements.length
22+
if options.return_all
23+
return elements
24+
return elements[0]
25+
else
26+
if options.jquery
27+
return $(selector)
28+
else
29+
elements = document.querySelectorAll(selector)
30+
if elements.length
31+
if options.return_all
32+
return elements
33+
return elements[0]
34+
35+
return null

coffee/getMultipleSelectors.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#The param "selector" that is passed to a custom command or assertion
2+
#can be an array of selector, or a string.
3+
#It's an array when a custom command is called from a section, and
4+
#this array cannot be used straight away in a command, because nightwatch
5+
#or selenium encode it in JSON, but the array itself has circular references
6+
#that json doesn't like. So I simply extract the selectors for each item
7+
#of the array and return it
8+
getMultipleSelectors = (selector) ->
9+
if Array.isArray(selector)
10+
section_selector = selector[0].selector
11+
selector = selector[1].selector
12+
return [section_selector,selector]
13+
else
14+
return selector

gulpfile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var gulp = require("gulp"),
33
coffeescript = require("gulp-coffee"),
44
plumber = require("gulp-plumber"),
55
markdox = require("gulp-markdox"),
6-
rename = require("gulp-rename");
6+
rename = require("gulp-rename"),
7+
include = require("gulp-include");
78

89
gulp.task("default", ["assertions", "commands"]);
910

@@ -15,6 +16,7 @@ gulp.task("assertions", function() {
1516
gulputil.log(error.stack);
1617
this.emit("end");
1718
}))
19+
.pipe(include())
1820
.pipe(coffeescript({bare: true}))
1921
.pipe(gulp.dest("js/assertions/"));
2022
});
@@ -27,6 +29,7 @@ gulp.task("commands", function() {
2729
gulputil.log(error.stack);
2830
this.emit("end");
2931
}))
32+
.pipe(include())
3033
.pipe(coffeescript({bare: true}))
3134
.pipe(gulp.dest("js/commands/"));
3235
});
@@ -48,6 +51,7 @@ gulp.task("docs", function() {
4851
});
4952

5053
gulp.task("watch", function() {
54+
gulp.watch('coffee/*.coffee', ['assertions', 'commands']);
5155
gulp.watch('coffee/assertions/**', ['assertions']);
5256
gulp.watch('coffee/commands/**', ['commands']);
5357

0 commit comments

Comments
 (0)