WebDriverHelpers Class
Provides several functions that make using WebDriver more convenient.
Constructor
WebDriverHelpers
-
webdriver
-
driver
Parameters:
-
webdriver
Objectselenium-webdriver instance
-
driver
Objectbrowser driver instance
Returns:
Example:
var webdriver = require('selenium-webdriver'),
WebDriverHelpers = require('webdriver-helpers'),
driver,
helpers;
driver = require('./drivers/selenium-server.js')(webdriver, {
browserName: 'chrome',
seleniumProtocol: 'WebDriver',
'chrome.switches': ['--window-size=1366,768'] // this is being ignored
});
driver.manage().timeouts().setScriptTimeout(5000);
driver.manage().timeouts().implicitlyWait(5000);
helpers = new WebDriverHelpers(webdriver, driver);
function login (email, password) {
helpers.populateElements({
'#login-email': email,
'#login-password': password
});
helpers.id('login-submit-btn').click();
}
Item Index
Methods
avoidStaleElement
()
Pauses for a short amount of time, giving webdriver time to re-render the page, if needed.
Example:
var authenticate = function () {
var name = 'Teacher User',
email = 'teacher@example.com',
password = 'password',
deferred = webdriver.promise.defer(),
i = 0;
helpers.populateElements({
'#login-email': email,
'#login-password': password
});
helpers.id('login-buttons-password').click();
helpers.avoidStaleElement();
helpers.id('display-name-link').getText()
.then(function (text) {
var expected = name;
if (text.indexOf(expected) !== 0) {
deferred.reject(new Error(text + ' did not contain ' + expected));
} else {
deferred.resolve();
}
});
return deferred.promise;
};
className
-
args
Sugar for: this.driver.findElement(this.webdriver.By.className(args))
Parameters:
-
args
String
Returns:
findElementInCollectionByText
-
collectionLocator
-
criteriaLocator
-
text
Find first element in a group who's text matches parameter.
Parameters:
-
collectionLocator
webdriver.Locator | Object.The locator for the group of elements to search
-
criteriaLocator
webdriver.Locator | Object.The locator for the element who's text should be compared, relative to collection
-
text
StringThe text to search for.
Returns:
Example:
var findPersonByName = function (nameOfPerson) {
var collectionLocator = {
'css':'div.people > .person'
},
criteriaLocator = {
'className':'name'
};
return findElementInCollectionByText(
collectionLocator,
criteriaLocator,
nameOfPerson
);
};
//...can be called like:
it("can click person",
function (done) {
authenticate().
then(findPersonByName("Jeff Winger")).
then(clickPerson).
then(finish(done), error);
});
//...and will match:
<div class="people">
<div class="person">
<p class="name">Jeff Winger</p>
<p class="pic">...</p>
</div>
</div>
id
-
args
Sugar for: this.driver.findElement(this.webdriver.By.id(args))
Parameters:
-
args
String
Returns:
Example:
helpers.id('btnSubmit').click()
name
-
args
Sugar for: this.driver.findElement(this.webdriver.By.name(args))
Parameters:
-
args
String
Returns:
populateElement
-
locator
-
strings
Sets the value of a form element.
Supports the following form elements: input - text, radio, email, password textarea select
Parameters:
-
locator
webdriver.Locator | Object.The locator strategy to use when searching for the form element.
-
strings
String | arrayThe target value(s) of into the form element(s)
populateElements
-
locatorValuePairs
Easily set values of form elements by passing a map of css selector / element value pairs.
Parameters:
-
locatorValuePairs
Objectkey/value pairs consisting of a css string locator for a form element and a string or array of strings to populate the element with.
Example:
// populate a radio button group, a select box, a text
// input and a textarea
var helpers = new WebDriverHelpers(webdriver, driver);
helpers.populateElements({
'input[name="gender"][value="M"]': 'M',
'#country': "United States",
'#age': '34',
'#comment': "example msg"
});
populateTextBox
-
locator
-
strings
Populate textbox(es) with string value(s). If 'strings' is an array, locator is assumed to match a group of textboxes which all have the same name.
This function performs a new find for each subsequent textbox in order to support dynamically added inputs. For example, when a second input field is added once a value is entered into the first input.
Parameters:
-
locator
webdriver.Locator | Object.The locator strategy to use when locating the target input box(es)
-
strings
String | arrayThe values to populate into the text box(es)
Returns:
Example:
var populateFriends = function (names) {
var locator = {className: "friends-mlText"};
return helpers.populateTextBox (locator, names);
};
radioByValue
-
name
-
value
Selects a radio button element by value
Parameters:
-
name
StringThe name of the radio button group
-
value
StringThe value of the option to select
selectByValue
-
locator
-
value
Selects a drop-down option by value
Parameters:
-
locator
webdriver.Locator | Object.The locator strategy to use when searching for the select element.
-
value
StringThe value of the option to select
tagName
-
args
Sugar for: this.driver.findElement(this.webdriver.By.tagName(args)
Parameters:
-
args
String
Returns:
Example:
var verifyOnCheckinPage = function () {
helpers.tagName('h1').then(function (h1) {
h1.getText().then(function (text) {
expect(text).toBe("Check-In");
});
});
};