What is jasminewd2?
The jasminewd2 package is a bridge between Jasmine and WebDriverJS, allowing for seamless integration of asynchronous WebDriverJS commands in Jasmine tests. It provides utilities to handle promises and asynchronous operations in a more readable and manageable way.
What are jasminewd2's main functionalities?
Asynchronous Testing
This feature allows you to write tests that wait for promises to resolve before making assertions. The code sample demonstrates how to navigate to a URL and then check the page title, ensuring that the test waits for the navigation to complete.
describe('Asynchronous specs', function() {
it('should wait for the promise to resolve', function() {
return browser.get('http://www.example.com').then(function() {
expect(browser.getTitle()).toEqual('Example Domain');
});
});
});
Custom Matchers
This feature allows you to define custom matchers to extend Jasmine's built-in matchers. The code sample shows how to create a custom matcher `toBeWithinRange` that checks if a number falls within a specified range.
describe('Custom matchers', function() {
beforeEach(function() {
jasmine.addMatchers({
toBeWithinRange: function() {
return {
compare: function(actual, expected) {
var result = {};
result.pass = actual >= expected[0] && actual <= expected[1];
if (result.pass) {
result.message = 'Expected ' + actual + ' to be within range ' + expected;
} else {
result.message = 'Expected ' + actual + ' to be within range ' + expected + ' but it was not';
}
return result;
}
};
}
});
});
it('is within range', function() {
expect(100).toBeWithinRange([90, 110]);
});
});
Handling Asynchronous Setup and Teardown
This feature allows you to perform asynchronous operations in setup and teardown functions. The code sample demonstrates how to navigate to a URL before all tests and quit the browser after all tests, ensuring that these operations complete before proceeding.
describe('Asynchronous setup and teardown', function() {
beforeAll(function(done) {
browser.get('http://www.example.com').then(function() {
done();
});
});
afterAll(function(done) {
browser.quit().then(function() {
done();
});
});
it('should have a title', function() {
expect(browser.getTitle()).toEqual('Example Domain');
});
});
Other packages similar to jasminewd2
protractor
Protractor is an end-to-end test framework for Angular and AngularJS applications. It is built on top of WebDriverJS and integrates seamlessly with Jasmine, providing additional features like automatic waiting and Angular-specific locators. Compared to jasminewd2, Protractor offers a more comprehensive solution for testing Angular applications.
webdriverio
WebdriverIO is a popular WebDriver binding for Node.js. It supports various test frameworks, including Jasmine, Mocha, and Cucumber. WebdriverIO provides a rich set of features for writing and managing end-to-end tests, including support for asynchronous operations and custom commands. Compared to jasminewd2, WebdriverIO offers more flexibility in terms of test framework integration and additional utilities.
nightwatch
Nightwatch.js is an end-to-end testing framework built on top of Selenium WebDriver. It provides a simple syntax for writing tests and includes built-in support for running tests in parallel. Nightwatch.js also supports various test frameworks, including Mocha and Cucumber. Compared to jasminewd2, Nightwatch.js offers a more integrated solution with built-in test runner and assertion library.
jasminewd2
Adapter for Jasmine-to-WebDriverJS. Used by Protractor.
Important: There are two active branches of jasminewd.
- jasminewd1 is an adapter for Jasmine 1.3, and uses the package minijasminenode. It is published to npm as
jasminewd
. - jasminewd2 is an adapter for Jasmine 2.x, and uses the package jasmine. It is published to npm as
jasminewd2
.
Features
-
Automatically makes tests asynchronously wait until the WebDriverJS control flow is empty.
-
If a done
function is passed to the test, waits for both the control flow and until done is called.
-
If a test returns a promise, waits for both the control flow and the promise to resolve.
-
Enhances expect
so that it automatically unwraps promises before performing the assertion.
Installation
npm install jasminewd2
Usage
In your setup:
var JasmineRunner = require('jasmine');
var jrunner = new JasmineRunner();
var webdriver = require('selenium-webdriver');
global.driver = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({browserName: 'chrome'}).
build();
require('jasminewd2').init(driver.controlFlow(), webdriver);
jrunner.projectBaseDir = '';
jrunner.execute(['**/*_spec.js']);
In your tests:
describe('tests with webdriver', function() {
it('will wait until webdriver is done', function() {
driver.get('http://www.example.com');
var myElement = driver.findElement(webdriver.By.id('hello'));
expect(myElement.getText()).toEqual('hello world');
});
})
TypeScript
For the typings related to the changes in the global jasmine variables (e.g.
allowing it()
blocks to return a promise), we publish the package
@types/jasminewd2
. If you are writing tests using jasminewd (including
Protractor tests), be sure to include @types/jasminewd2
in your
devDependencies
, as these global type modifications are not bundled with
the jasminewd2
npm module.
jasminewd also exports one function directly: init
. Unfortunately, we do not
publish typings for this function. If you call this function directly (e.g. you
are a Protractor dev), you should simply do:
require('jasminewd2').init(controlFlow, webdriver);
async
functions / await
async
functions and the await
keyword are likely coming in ES2017 (ES8), and
available via several compilers. At the moment, they often break the WebDriver
control flow.
(GitHub issue). You can
still use them, but if you do then you will have to use await
/Promises for
almost all your synchronization. See spec/asyncAwaitAdapterSpec.ts
and
spec/asyncAwaitErrorSpec.ts
for examples.