Skip to content

Commit 8ba693f

Browse files
author
John Doherty
committed
added html attribute helpers and updated docs
1 parent 6787c6c commit 8ba693f

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

README.MD

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# selenium-cucumber-js [![Shippable branch](https://img.shields.io/shippable/5818b23bbd56670e00037040/master.svg)](https://app.shippable.com/projects/5818b23bbd56670e00037040) [![npm](https://img.shields.io/npm/dt/selenium-cucumber-js.svg)](https://www.npmjs.com/package/selenium-cucumber-js)
1+
# selenium-cucumber-js [![Shippable branch](https://img.shields.io/shippable/5818b23bbd56670e00037040/master.svg)](https://app.shippable.com/projects/5818b23bbd56670e00037040) [![npm](https://img.shields.io/npm/dt/selenium-cucumber-js.svg)](https://www.npmjs.com/package/selenium-cucumber-js) [![Twitter Follow](https://img.shields.io/twitter/follow/CambridgeMVP.svg?style=social&label=Twitter&style=plastic)](https://twitter.com/CambridgeMVP)
22

33
JavaScript browser automation framework using official [selenium-webdriver](http://seleniumhq.github.io/selenium/docs/api/javascript/ "view webdriver js documentation") and [cucumber-js](https://github.com/cucumber/cucumber-js "view cucumber js documentation").
44

@@ -12,6 +12,7 @@ JavaScript browser automation framework using official [selenium-webdriver](http
1212
* [Step definitions](#step-definitions)
1313
* [Page objects](#page-objects)
1414
* [Shared objects](#shared-objects)
15+
* [Helpers](#helpers)
1516
* [Visual Comparison](#visual-comparison)
1617
* [Before/After hooks](#beforeafter-hooks)
1718
* [Reports](#reports)
@@ -233,6 +234,36 @@ module.exports = function () {
233234
};
234235
```
235236

237+
### Helpers
238+
239+
`selenium-cucumber-js` contains a few helper methods to make working with selenium a bit easier, those methods are:
240+
241+
```js
242+
// Load a URL, returning only when the <body> tag is present
243+
helpers.loadPage('http://www.google.com');
244+
245+
// get the value of a HTML attribute
246+
helpers.getAttributeValue('body', 'class');
247+
248+
// get a list of elements matching a query selector who's inner text matches param.
249+
helpers.getElementsContainingText('nav[role="navigation"] ul li a', 'Safety Boots')
250+
251+
// get first elements matching a query selector who's inner text matches textToMatch param
252+
helpers.getFirstElementContainingText('nav[role="navigation"] ul li a', 'Safety Boots').click();
253+
254+
// click element(s) that are not visible (useful in situations where a menu needs a hover before a child link appears)
255+
helpers.clickHiddenElement('nav[role="navigation"] ul li a','Safety Boots');
256+
257+
// wait until a HTML attribute equals a particular value
258+
helpers.waitUntilAttributeEquals('html', 'data-busy', 'false', 5000);
259+
260+
// wait until a HTML attribute exists
261+
helpers.waitUntilAttributeExists('html', 'data-busy', 5000);
262+
263+
// wait until a HTML attribute no longer exists
264+
helpers.waitUntilAttributeDoesNotExists('html', 'data-busy', 5000);
265+
```
266+
236267
### Visual Comparison
237268

238269
The `selenium-cucumber-js` framework uses [Applitools Eyes](https://applitools.com/) to add visual checkpoints to your JavaScript Selenium tests. It takes care of getting screenshots of your application from the underlying WebDriver, sending them to the Applitools Eyes server for validation and failing the test when differences are detected. To preform visual comparisons within your tests, obtain an [Applitools Eyes](https://applitools.com/) API Key and assign it to the `eye_key` property of the `selenium-cucumber-js.json` config file in the root of your project.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-cucumber-js",
3-
"version": "1.5.5",
3+
"version": "1.5.6",
44
"description": "JavaScript browser automation framework using official selenium-webdriver and cucumber-js",
55
"main": "index.js",
66
"scripts": {

runtime/helpers.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,87 @@ module.exports = {
134134
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
135135
* @returns {Promise} resolves if attribute eventually equals, otherwise rejects
136136
* @example
137-
* helpers.waitUntilAttributeEquals('html', 'data-busy', 'false', 5);
137+
* helpers.waitUntilAttributeEquals('html', 'data-busy', 'false', 5000);
138138
*/
139139
waitUntilAttributeEquals: function(elementSelector, attributeName, attributeValue, waitInMilliseconds) {
140140

141141
// use either passed in timeout or global default
142142
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
143143

144+
// readable error message
145+
var timeoutMessage = attributeName + ' does not equal ' + attributeValue + ' after ' + waitInMilliseconds + ' milliseconds';
146+
144147
// repeatedly execute the test until it's true or we timeout
145148
return driver.wait(function() {
146149

147-
// get the html attribute value using another helper method
150+
// get the html attribute value using helper method
148151
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
149152

150153
// inspect the value
151154
return value === attributeValue;
152155
});
153156

154-
}, timeout);
157+
}, timeout, timeoutMessage);
158+
},
159+
160+
/**
161+
* Waits until a HTML attribute exists
162+
* @param {string} elementSelector - HTML element CSS selector
163+
* @param {string} attributeName - name of the attribute to inspect
164+
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
165+
* @returns {Promise} resolves if attribute exists within timeout, otherwise rejects
166+
* @example
167+
* helpers.waitUntilAttributeExists('html', 'data-busy', 5000);
168+
*/
169+
waitUntilAttributeExists: function(elementSelector, attributeName, waitInMilliseconds) {
170+
171+
// use either passed in timeout or global default
172+
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
173+
174+
// readable error message
175+
var timeoutMessage = attributeName + ' does not exists after ' + waitInMilliseconds + ' milliseconds';
176+
177+
// repeatedly execute the test until it's true or we timeout
178+
return driver.wait(function() {
179+
180+
// get the html attribute value using helper method
181+
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
182+
183+
// attribute exists if value is not null
184+
return value !== null;
185+
});
186+
187+
}, timeout, timeoutMessage);
188+
},
189+
190+
/**
191+
* Waits until a HTML attribute no longer exists
192+
* @param {string} elementSelector - HTML element CSS selector
193+
* @param {string} attributeName - name of the attribute to inspect
194+
* @param {integer} waitInMilliseconds - number of milliseconds to wait for page to load
195+
* @returns {Promise} resolves if attribute is removed within timeout, otherwise rejects
196+
* @example
197+
* helpers.waitUntilAttributeDoesNotExists('html', 'data-busy', 5000);
198+
*/
199+
waitUntilAttributeDoesNotExists: function(elementSelector, attributeName, waitInMilliseconds) {
200+
201+
// use either passed in timeout or global default
202+
var timeout = waitInMilliseconds || DEFAULT_TIMEOUT;
203+
204+
// readable error message
205+
var timeoutMessage = attributeName + ' still exists after ' + waitInMilliseconds + ' milliseconds';
206+
207+
// repeatedly execute the test until it's true or we timeout
208+
return driver.wait(function() {
209+
210+
// get the html attribute value using helper method
211+
return helpers.getAttributeValue(elementSelector, attributeName).then(function(value) {
212+
213+
// attribute exists if value is not null
214+
return value === null;
215+
});
216+
217+
}, timeout, timeoutMessage);
155218
}
156219

157220
};

0 commit comments

Comments
 (0)