API Docs for: 1.0
Show:

WebDriverHelpers Class

Defined in: index.js:25

Provides several functions that make using WebDriver more convenient.

Constructor

WebDriverHelpers

(
  • webdriver
  • driver
)
Object

Defined in index.js:25

Parameters:

  • webdriver Object

    selenium-webdriver instance

  • driver Object

    browser driver instance

Returns:

Object: object with several convenience functions

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();
}

Methods

avoidStaleElement

()

Defined in index.js:150

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
)
Object

Defined in index.js:112

Sugar for: this.driver.findElement(this.webdriver.By.className(args))

Parameters:

  • args String

Returns:

Object: webdriver element

findElementInCollectionByText

(
  • collectionLocator
  • criteriaLocator
  • text
)
Function

Defined in index.js:235

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 String

    The text to search for.

Returns:

Function: A function that, when executed, returns a promise that will be resolved with the matching element.

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
)
Object

Defined in index.js:80

Sugar for: this.driver.findElement(this.webdriver.By.id(args))

Parameters:

  • args String

Returns:

Object: webdriver element

Example:

helpers.id('btnSubmit').click()

name

(
  • args
)
Object

Defined in index.js:69

Sugar for: this.driver.findElement(this.webdriver.By.name(args))

Parameters:

  • args String

Returns:

Object: webdriver element

populateElement

(
  • locator
  • strings
)

Defined in index.js:398

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 | array

    The target value(s) of into the form element(s)

populateElements

(
  • locatorValuePairs
)

Defined in index.js:192

Easily set values of form elements by passing a map of css selector / element value pairs.

Parameters:

  • locatorValuePairs Object

    key/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
)
Function

Defined in index.js:331

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 | array

    The values to populate into the text box(es)

Returns:

Function: A function which, when executed, populates the target text boxes

Example:

var populateFriends = function (names) {
  var locator = {className: "friends-mlText"};
  return helpers.populateTextBox (locator, names);
};

radioByValue

(
  • name
  • value
)

Defined in index.js:137

Selects a radio button element by value

Parameters:

  • name String

    The name of the radio button group

  • value String

    The value of the option to select

selectByValue

(
  • locator
  • value
)

Defined in index.js:123

Selects a drop-down option by value

Parameters:

  • locator webdriver.Locator | Object.

    The locator strategy to use when searching for the select element.

  • value String

    The value of the option to select

tagName

(
  • args
)
Object

Defined in index.js:93

Sugar for: this.driver.findElement(this.webdriver.By.tagName(args)

Parameters:

  • args String

Returns:

Object: webdriver element

Example:

var verifyOnCheckinPage = function () {
  helpers.tagName('h1').then(function (h1) {
      h1.getText().then(function (text) {
        expect(text).toBe("Check-In");
      });
  });
};