npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-page-object

v1.1.1

Published

Create Page Objects for acceptance tests

Downloads

10

Readme

Ember-page-object npm version Build Status

A base Page Object class with helper functions and generators to help implement the Page Object pattern for your tests. Option to run assertions in your Page Objects. For some additional reading on Page Objects and how they can help your tests, see this blog post.

Usage

ember install ember-page-object
ember generate page-object signup

Generated Page Object:

// tests/page-objects/signup.js

import PageObject from '../page-object';

export default class SignupPage extends PageObject {
}

Customizing your Page Object

The base Page Object wraps several of Ember's acceptance test helpers, allowing for function chaining. It also provides some of it's own helper functions which are documented here. To flesh out your page object, extend the base Page Object and use the built in helpers.

// tests/page-objects/signup.js

import PageObject from '../page-object';

export default class SignupPage extends PageObject {
  fillInEmail(text) {
    return this.fillIn('#email', text);
  }

  fillInPassword(text) {
    return this.fillIn('#password', text);
  }

  clickSignUp() {
    return this.click('#sign-up');
  }
}
// tests/acceptance/signup-test.js
import SignupPage from '../page-objects/signup';

// other test code

test('user can sign up', function(assert) {
  assert.expect(1);

  new SignupPage({ assert })
    .visit('/signup')
    .fillInEmail('[email protected]')
    .fillInPassword('password')
    .clickSignUp()
    .assertURL('/profile', 'user is redirected to their profile after signup');
});

Data Attributes

If you want to use data attributes to decouple your tests from your application's presentation as described in this blog post, you can overwrite the toSelector function in your page object. The toSelector function is a hook that allows you to modify the selector that is passed in to the various helper functions.

By default, toSelector does not modify the selector at all. Below is a simple example of modifying toSelector so helper functions target the data-auto-id data attribute during tests.

  {{! signup template}}

  {{input value=email data-auto-id="signup-email"}}
  {{input value=password data-auto-id="signup-password"}}
  <button data-auto-id="signup-button">Submit</button>

  {{! other code}}
// tests/page-objects/base.js

import PageObject from '../page-object';

export default class BasePageObject extends PageObject {
  toSelector(rawSelector) {
    return `[data-auto-id="${rawSelector}"]`;
  }
}
// tests/page-objects/signup.js

import BasePageObject from './base';

export default class SignupPageObject extends BasePageObject {
  clickSignUp() {
    return this.click('signup-button');
  }

  fillInEmail(email) {
    return this.fillIn('signup-email', email);
  }

  fillInPassword(password) {
    return this.fillIn('signup-password', password);
  }
}
// tests/acceptance/signup-test.js
import SignupPage from '../page-objects/signup';

// other test code

test('user can sign up', function(assert) {
  assert.expect(1);

  new SignupPage({ assert })
    .visit('/signup')
    .fillInEmail('[email protected]')
    .fillInPassword('password')
    .clickSignUp()
    .assertURL('/profile', 'user is redirected to their profile after signup');
});

For this example to work, you'll also have to extend the TextField component, which drives the {{input}} helper, and add an attribute binding for the data-auto-id attribute. You many also want to do this for other components (eg. Ember.LinkComponent and Ember.TextArea).

// app/initializers/data-attribute.js

import Ember from 'ember';

export default {
  name: 'data-attribute',

  initialize() {
    const attributeName = 'data-auto-id';

    Ember.TextField.reopen({
      attributeBindings: [attributeName]
    });

    Ember.LinkComponent.reopen({
      attributeBindings: [attributeName]
    });

    Ember.TextArea.reopen({
      attributeBindings: [attributeName]
    });
  }
};

Helpers

Page Objects extending from the base PageObject have access to a number of helper functions. All functions return the page object instance to allow function chaining.

Wrapped Test Helpers

Assertions

Debugging

Wrapped Test Helpers

andThen()

andThen(callback)

Wraps Ember's andThen() test helper.

export default class SignupPage extends PageObject {
  assertSignupFailure(errorMessage) {
    return this.andThen(() => {
      this.assertHasText('.flash-warning', errorMessage);
      this.assertURL('/signup');
    });
  }
}

click()

click(selector = '')

Wraps Ember's click() test helper.

export default class SignupPage extends PageObject {
  clickSignUp() {
    return this.click('#sign-up');
  }
}

fillIn()

fillIn(selector = '', text = '')

Wraps Ember's fillIn() test helper.

export default class SignupPage extends PageObject {
  fillInEmail(text) {
    return this.fillIn('#email', text);
  }
}

Assertions

assertURL()

assertURL(url = '', message = '')

Asserts that the passed in url is the current url. Accepts an optional assertion message.

test('"/profile" redirects to "/sign-in" when user is not signed in', function(assert) {
  assert.expect(1);

  new ProfilePage({ assert })
    .visit('/profile')
    .assertURL('/sign-in', 'user is redirected to "/sign-in"');
});

assertHasClass()

assertHasClass(selector = '', class = '', message = '')

Asserts the element matching the selector has the passed in class. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertInvalidEmail() {
    return this.assertHasClass('#email', '.is-invalid');
  }
}

assertNotHasClass()

assertNotHasClass(selector = '', class = '', message = '')

Asserts the element matching the selector does not have the passed in class. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertSubmitEnabled() {
    return this.assertNotHasClass('#submit-button', '.disabled');
  }
}

assertHasText()

assertHasText(selector = '', text = '', message = '')

Asserts the element matching the selector contains the passed in text. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertFlashMessage(text) {
    return this.assertHasText('.flash', text, `flash message with text: ${text} is displayed`);
  }
}

assertNotHasText()

assertNotHasText(selector = '', text = '', message = '')

Asserts the element matching the selector does not contain the passed in text. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertNotAdminUI() {
    return this.assertNotHasText('.banner', 'Admin', 'admin UI is not visible');
  }
}

assertPresent()

assertPresent(selector = '', message = '')

Asserts an element matching the selector can be found. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertSignedOutNav() {
    return this.assertPresent('.nav .sign-in-button', 'nav bar displays sign in button');
  }
}

assertNotPresent()

assertNotPresent(selector = '', message = '')

Asserts an element matching the selector is not found. Accepts an optional assertion message.

export default class SignupPage extends PageObject {
  assertSignedInNav() {
    return this.assertNotPresent('.nav .sign-in-button', 'nav bar does not display sign in button');
  }
}

Debugging

embiggen()

embiggen()

Embiggens the testing container for easier inspection.

test('a test that is being debugged', function(assert) {
  assert.expect(1);

  new PageObject({ assert })
    .visit('/')
    .doStuff()
    .embiggen()
    .pause()
    .breakingCode()
});

debugger()

debugger()

Throws a breakpoint via debugger within a PageObject chain.

test('a test that is being debugged', function(assert) {
  assert.expect(1);

  new PageObject({ assert })
    .visit('/')
    .doStuff()
    .debugger()
    .breakingCode()
});

pause()

pause()

Pauses a test so you can look around within a PageObject chain.

test('a test that is being debugged', function(assert) {
  assert.expect(1);

  new PageObject({ assert })
    .visit('/')
    .doStuff()
    .embiggen()
    .pause()
    .breakingCode()
});

Installation

  • git clone this repository
  • npm install
  • bower install

Running

  • ember server
  • Visit your app at http://localhost:4200.

Running Tests

  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.