This helper library offer lots of friendly api while using JavaScript WebDriver
Install the module with: npm install webdriver-helper
to enable the friendly APIs.
You could setup the selenium webdriver like this. Remember to require 'webdriver-helper' to enable friendly apis.
var webdriver = require('selenium-webdriver');
require('chai').should();
// enable the friendly apis by requiring 'webdriver-helper'
require('webdriver-helper');
var builder = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({
browserName: 'firefox'
});
var browser = builder.build();
browser.get('http://localhost:9001');
Go to the path which is relative to current path.
browser.navigateTo('/demo.html?a=1&b=2#c=1');
browser.currentUrl(function(currUrl, url) {
currUrl.should.equal('http://localhost:9001/demo.html?a=1&b=2#c=1')
});
Get current url.
browser.navigateTo('/demo.html?a=1&b=2#c=1');
browser.currentUrl(function(currUrl, url) {
currUrl.should.equal('http://localhost:9001/demo.html?a=1&b=2#c=1');
url.protocol.should.equal('http:');
url.slashes.should.equal(true);
url.host.should.equal('localhost:9001');
url.port.should.equal('9001');
url.hostname.should.equal('localhost');
url.href.should.equal('http://localhost:9001/demo.html?a=1&b=2#c=1');
url.hash.should.equal('#c=1');
url.search.should.equal('?a=1&b=2');
url.query.should.equal('a=1&b=2');
url.pathname.should.equal('/demo.html');
url.path.should.equal('/demo.html?a=1&b=2');
});
Refresh current page.
Go forward to next page in history.
Go back to privious page in history.
Get title in current page.
browser.title(function (title) {
title.should.equal('your title');
});
Get current window.
Get current window's position.
browser.window().position(function (x, y) {
x.should.equal(0);
y.should.equal(0);
});
Set current window's position to (x, y) relative to screen.
browser.window().position(100, 100);
Get current window's size.
browser.window().size(function (width, height) {
width.should.equal(500);
height.should.equal(500);
});
Set current window's size with width
and height
.
browser.window().size(500, 500)
Maximize the current window to full screen.
Get all matched element by selector. every element could use the friendly api listed below.
Get count of all matched elements.
browser.elements('input').count(function (count) {
count.should.equal(6);
});
Get the element in matched elements according to index.
browser.elements('input').get(0, function (elem) {
elem.enter('hello world!');
// elem.click();
});
Get the status that all elements is initialized or not.
Initialize all elements and save inside. In initedHandler, initialized
property will be updated to true
. And you could change asyn-invoking to sync-invoking. For example,
browser.elements('input').init(function (elems) {
this.initialized.should.be.true;
this.count().should.equal(6);
this.get(0).enter('hello world');
this.get(1).attr('name').should.equal('textbox');
/\* ... \*/
});
Get first matched element by selector.
Click the element, the click
method is also used for browser.input()
, browser.dropdownlist()
and browser.link()
element.
Get value for the the matched element's value attribute. It could used in browser.input(selector).value()
as well.
browser.element('input#name').value(function (val) {
val.should.equal('your name');
});
Get value of the matched element's attrubute with attribute name attrName
.
browser.element('input[name="textbox"]').attr('name', function (attr) {
attr.should.equal('textbox');
});
Get css value of the matched element's css property with name propertyName
.
browser.element('input[name="textbox"]').css('font-family', function (property) {
property.should.equal('Georgia');
});
Get the innerText of the matched element.
browser.element('body').text(function (text) {
text.should.contain('hello world!');
});
Get the innerHTML of the matched element.
browser.element('body').html(function (html) {
html.should.contain('<p>hello world!</p>');
});
Get value for the matched element's status.
browser.element('input#btn').isEnabled(function (enabledStatus) {
enabledStatus.should.be.false;
});
Get value for visibility of element.
browser.element('#displayed').isDisplayed(function (displayed) {
displayed.should.be.false;
});
Get first matched input
element by selector, it includes textbox, checkbox, radio etc...
Enter text into matched textbox.
Get value of matched textbox
Check matched checkbox.
Uncheck matched checkbox.
Get the checked status.
browser.input('input#checkbox').isChecked(function (checked) {
checked.should.be.true;
})
Select matched radio button.
Get the selected status. Same as isChecked
.
Get first matched select
element by selector.
Select these options with the valus in arguments. If the dropdownlist couldn't be multi-selected, the option with the last value would be selected.
Get the value for matched dropdownlist.
For the multi-selected dropdownlist, it will return array of values.
browser.dropdownlist(selector).values(function (values) {
values.should.eql([2, 3]);
});
Use css selector to find the matched link
ONLY FOR LINK
browser.link(':contains("Partial Link Text")').click()
browser.exec('alert("hello world!");', function() {
browser.dialog().text(function (text) {
text.should.equal('hello world!');
});
});
Use args
browser.exec('alert(arguments[0][0]);', ['hello!'], function() {
browser.dialog().text(function (text) {
text.should.equal('hello!');
});
});
Use elements
browser.exec('alert(arguments[0][0].name);', browser.elements('input[type="checkbox"]');
browser.dialog().text(function (text) {
text.should.equal('checkbox');
});
browser.manage().timeouts().setScriptTimeout(5000);
browser.execAsync('var callback = arguments[arguments.length - 1];setTimeout(function(){ callback(10); }, 500);', function(num) {
num.should.equal(10);
});
Use args
browser.manage().timeouts().setScriptTimeout(5000);
browser.execAsync('var callback = arguments[arguments.length - 1];var str = arguments[0][0]; setTimeout(function(){ callback(str); }, 500);', ['hello world'], function(str) {
str.should.equal('hello world');
});
Use elements
browser.manage().timeouts().setScriptTimeout(5000);
browser.execAsync('var callback = arguments[arguments.length - 1];var elem = arguments[0][0]; setTimeout(function(){ callback(elem); }, 500);', browser.elements('input[type="checkbox"]'), function(element) {
element.attr('name', function(name) {
name.should.equal('checkbox');
});
});
Get the dialog which could be alert, confirm, prompt dialog.
Get the text on the dialog.
Accept(Ok) the confirm and prompt dialog.
Dismiss(Cancel) the alert, confirm and prompt dialog.
Enter text in prompt dialog.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
0.2.1 2013-10-20 Add exec
and execAsync
JavaScript support
0.2.0 2013-10-20 Add dialog behaviors
0.1.0 2013-10-17 MVP
Copyright (c) 2013 Wang Qiu
Licensed under the MIT license.