Skip to content

Commit 1c06afd

Browse files
author
John Doherty
committed
Added 'helpers' object that exposes a collection of selenium helper methods
1 parent 0161bf2 commit 1c06afd

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

Diff for: README.MD

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ The following variables are available within the ```Given()```, ```When()``` and
9898
| `selenium` | the raw [selenium-webdriver](http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/) module, providing access to static properties/methods |
9999
| `page` | collection of **page** objects loaded from disk and keyed by filename |
100100
| `shared` | collection of **shared** objects loaded from disk and keyed by filename |
101+
| `helpers` | a collection of helper methods _things selenium does not provide but really should!_ |
101102
| `by` | the selenium [By](http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_By.html) class used to locate elements on the page |
102103
| `until` | the selenium [until](http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/until.html) class used to wait for elements/events |
103104
| `expect` | instance of [chai expect](http://chaijs.com/api/bdd/) to ```expect('something').to.equal('something')``` |

Diff for: runtime/helpers.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module.exports = {
2+
3+
/**
4+
* returns a promise that is called when the url has loaded and the body element is present
5+
* @param {string} url to load
6+
* @returns {Promise}
7+
* @example
8+
* helpers.loadPage('http://www.google.com');
9+
*/
10+
loadPage: function(url, waitInSeconds) {
11+
12+
// use either passed in timeout or global 10 seconds default
13+
var timeout = (waitInSeconds) ? (waitInSeconds * 1000) : DEFAULT_TIMEOUT;
14+
15+
// load the url and wait for it to complete
16+
return driver.get(url).then(function() {
17+
18+
// now wait for the body element to be present
19+
return driver.wait(until.elementLocated(by.css('body')), timeout);
20+
});
21+
},
22+
23+
/***
24+
* returns the value of an attribute on an element
25+
* @param {string} css selector used to find the element
26+
* @param {string} attribute name to retrieve
27+
* @returns {string} the value of the attribute or empty string if not found
28+
* @example
29+
* helpers.getAttributeValue('body', 'class');
30+
*/
31+
getAttributeValue: function (selector, attributeName) {
32+
33+
// get the element from the page
34+
return driver.findElement(by.css(selector)).then(function(attributeValue) {
35+
return attributeValue;
36+
})
37+
.catch(function(){
38+
return '';
39+
});
40+
},
41+
42+
/**
43+
* returns list of elements matching a query selector who's inner text mathes param
44+
* @param {string} css selector used to get list of elements
45+
* @param {string} inner text to match (does not have to be visible)
46+
* @returns {Promise}
47+
* @example
48+
* helpers.getElementsContainingText('nav[role="navigation"] ul li a', 'Safety Boots')
49+
*/
50+
getElementsContainingText: function(cssSelector, textToMatch) {
51+
52+
// method to execute within the DOM to find elements containing text
53+
function findElementsContainingText(query, content) {
54+
55+
var results = []; // array to hold results
56+
57+
// workout which property to use to get inner text
58+
var txtProp = ('textContent' in document) ? 'textContent' : 'innerText';
59+
60+
// get the list of elements to inspect
61+
var elements = document.querySelectorAll(query);
62+
63+
for (var i=0, l=elements.length; i<l; i++) {
64+
if (elements[i][txtProp] === content){
65+
results.push(elements[i]);
66+
}
67+
}
68+
69+
return results;
70+
};
71+
72+
// grab matching elements
73+
return driver.findElements(by.js(findElementsContainingText, cssSelector, textToMatch));
74+
},
75+
76+
/**
77+
* returns first elements matching a query selector who's inner text matches textToMatch param
78+
* @param {string} css selector used to get list of elements
79+
* @param {string} inner text to match (does not have to be visible)
80+
* @returns {Promise}
81+
* @example
82+
* helpers.getFirstElementContainingText('nav[role="navigation"] ul li a', 'Safety Boots').click();
83+
*/
84+
getFirstElementContainingText: function(cssSelector, textToMatch){
85+
return helpers.getElementsContainingText(cssSelector, textToMatch).then(function(elements){
86+
return elements[0];
87+
});
88+
}
89+
90+
};

Diff for: runtime/world.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var fs = require('fs-plus'),
1818
assert = require("chai").assert,
1919
reporter = require('cucumber-html-reporter');
2020

21-
var DEFAULT_TIMEOUT = 10 * 1000; // 10 second default
21+
global.DEFAULT_TIMEOUT = 10 * 1000; // 10 second default
2222

2323
/**
2424
* create the selenium browser based on global var set in index.js
@@ -131,6 +131,8 @@ function World() {
131131
}
132132
};
133133

134+
// add helpers
135+
global.helpers = require('../runtime/helpers.js');
134136
}
135137

136138
// export the "World" required by cucubmer to allow it to expose methods within step def's

0 commit comments

Comments
 (0)