expect-protractor

expect-protractor is a library which provides a light-weight matchers for the Protractor and Jasmine test frameworks.

Usage no npm install needed!

<script type="module">
  import expectProtractor from 'https://cdn.skypack.dev/expect-protractor';
</script>

README

expect-protractor

expect-protractor is a library which provides a light-weight matchers for the Protractor and Jasmine test frameworks.

Table of Contents

Getting Started

Installation

To use expect-protractor in your project, run:

npm i -D expect-protractor

Importing and Adding

Add the expect-protractor 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('expect-protractor');

exports.config = {
  onPrepare() {
    // add matchers in the beforeEach hook
    beforeEach(() => jasmine.addMatchers(customMatchers));
  },
};

protractor.config.ts

// importing module
import * as customMatchers from 'expect-protractor';

exports.config = {
  onPrepare() {
    // add matchers in the beforeEach hook
    beforeEach(() => jasmine.addMatchers(customMatchers));
  },
};

Also you can add the expect-protractor module directly in your test.

// importing module
const customMatchers = require('expect-protractor');

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/expect-protractor", "node_modules/@types"]
  }
}

Usage

Example - asserting element's text with toHaveText function that comes with the expect-protractor library.

Without the expect-protractor library:

expect(await element.getText()).toBe('Some text');

After adding the expect-protractor library:

await expect(element).toHaveText('Some text');

Example - asserting element's value with toHaveValue function that comes with the expect-protractor library.

Without the expect-protractor library:

expect(await element.getAttribute('value')).toBe('Some value');

After adding the expect-protractor library:

await expect(element).toHaveValue('Some value');

API

toBeDisplayed - verifies that element is displayed on the page.

Example:

await expect(element).toBeDisplayed();

toBePresent - verifies that element is present in the DOM.

Example:

await expect(element).toBePresent();

toContainText - verifies that element contains text.

Example:

await expect(element).toContainText('text');

toHaveText - verifies element's text.

Example:

await expect(element).toHaveText('Expected text');

toContainValue - verifies that element contains value.

Example:

await expect(element).toContainValue('value');

toHaveValue - verifies element's value.

Example:

await expect(element).toHaveValue('Expected value');

toHaveAttribute - verifies element has an attribute .

Example:

await expect(element).toHaveAttribute('expected-attribute');

toHaveCssValue - verifies element has an css value .

Example:

await expect(element).toHaveCssValue({ property: 'color', value: '#000000' });

toContainCssValue - verifies element contains an css value .

Example:

await expect(element).toContainCssValue({ property: 'color', value: '#0' });

toBeDisabled - verifies that element is disabled.

Example:

await expect(element).toBeDisabled();

toBeSelected - verifies that element is selected(checked).

Example:

await expect(element).toBeSelected();

toHaveLink - verifies element's link(href attribute).

Example:

await expect(element).toHaveLink('https://expected-link.com/');

toContainLink - verifies element contains link(href attribute).

Example:

await expect(element).toContainLink('link.com');

toContainClass - verifies element contains class names.

Example:

await expect(element).toContainClass('expected-css-name');

toHaveCount - verifies element's length.

Example:

await expect(elements).toHaveCount(3);

toBeClickable - verifies element is clickable.

Example:

await expect(element).toBeClickable();

toHaveTitle - verifies title.

Example:

await expect(browser).toHaveTitle('Expected title');

toContainTitle - verifies that title contains text.

Example:

await expect(browser).toContainTitle('title');

toHaveUrl - verifies current url.

Example:

await expect(browser).toHaveUrl('https://expected.com/');

toContainUrl - verifies that current url contains text.

Example:

await expect(browser).toContainUrl('.com');

toHaveWindowsCount - verifies window tabs count.

Example:

await expect(browser).toHaveWindowsCount(2);

toHaveWindowsCountMoreThan - verifies window tabs count greater than.

Example:

await expect(browser).toHaveWindowsCountMoreThan(3);

toHaveWindowsCountLessThan - verifies window tabs count less than.

Example:

await expect(browser).toHaveWindowsCountLessThan(2);

License

expect-protractor is MIT licensed.