Protractor-Jasmine-Matchers
Protractor-Jasmine-Matchers is a library which provides a light-weight matchers for the Protractor and Jasmine test frameworks.
Table of Contents
Getting Started
Installation
To use Protractor-Jasmine-Matchers in your project, run:
npm i --save-dev protractor-jasmine-matchers
Importing and Adding
Add the protractor-jasmine-matchers module to the your protractor.config.js
file in the onPrepare
function and wrap it into the beforeEach
function.
protractor.config.js
// importing module
const customMatchers = require('protractor-jasmine-matchers');
exports.config = {
onPrepare() {
// add matchers in the beforeEach hook
beforeEach(() => jasmine.addMatchers(customMatchers))
}
}
protractor.config.ts
// importing module
import * as customMatchers from 'protractor-jasmine-matchers'
exports.config = {
onPrepare() {
// add matchers in the beforeEach hook
beforeEach(() => jasmine.addMatchers(customMatchers))
}
}
Also you can add the protractor-jasmine-matchers module directly in your test.
// importing module
const customMatchers = require('protractor-jasmine-matchers');
describe('Test suite', () => {
// add matchers in the beforeEach hook
beforeEach(() => jasmine.addMatchers(customMatchers))
it('should verify', () => {
...
});
});
Also, if you are faced with the TypeScript compilation errors when running your tests, add the typeRoots entry in the tsconfig.json, with the right order:
{
"compilerOptions": {
"typeRoots": [
"./node_modules/protractor-jasmine-matchers",
"./node_modules/@types"
],
}
}
Usage
Example - asserting element's text with toHaveText
function that comes with the protractor-jasmine-matchers library.
Without the protractor-jasmine-matchers library:
expect(await element.getText()).toBe('Some text');
After adding the protractor-jasmine-matchers library:
await expect(element).toHaveText('Some text');
Example - asserting element's value with toHaveValue
function that comes with the protractor-jasmine-matchers library.
Without the protractor-jasmine-matchers library:
expect(await element.getAttribute('value')).toBe('Some value');
After adding the protractor-jasmine-matchers library:
await expect(element).toHaveValue('Some value');
API
toBeDisplayed - verifies that element is displayed on the page.
Example:
await expect(element).toBeDisplayed();
not.toBeDisplayed - it is the inverse of the toBeDisplayed
function.
Example:
await expect(element).not.toBeDisplayed();
toBePresent - verifies that element is present in the DOM.
Example:
await expect(element).toBePresent();
not.toBePresent - it is the inverse of the toBePresent
function.
Example:
await expect(element).not.toBePresent();
toContainText - verifies that element contains text.
Example:
await expect(element).toContainText('Expected text');
not.toContainText - it is the inverse of the toContainText
function.
Example:
await expect(element).not.toContainText('Expected text');
toHaveText - verifies element's text.
Example:
await expect(element).toHaveText('Expected text');
toContainValue - verifies that element contains value.
Example:
await expect(element).toContainValue('Expected value');
not.toContainValue - it is the inverse of the toContainValue
function.
Example:
await expect(element).not.toContainValue('Expected value');
toHaveValue - verifies element's value.
Example:
await expect(element).toHaveValue('Expected value');
toBeDisabled - verifies that element is disabled.
Example:
await expect(element).toBeDisabled();
not.toBeDisabled - it is the inverse of the toBeDisabled
function.
Example:
await expect(element).not.toBeDisabled();
toBeSelected - verifies that element is selected(checked).
Example:
await expect(element).toBeSelected();
not.toBeDisabled - it is the inverse of the toBeSelected
function.
Example:
await expect(element).not.toBeSelected();
toHaveLink - verifies element's link(href attribute).
Example:
await expect(element).toHaveLink('https://expected-link.com/');
toHaveClassName - verifies element's class names.
Example:
await expect(element).toHaveClass('expected-css-name');
toHaveLength - verifies element's length.
Example:
await expect(elements).toHaveLength(3);
License
Protractor-Jasmine-Matchers is MIT licensed.