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

domtest

v0.10.34

Published

Helps to perform unit and integration testing on documents and elements

Downloads

499

Readme

DomTest

Build Status NPM version Bower version

Describe DOM Tests with ease: less code, less words.

  • MaskJS syntax
  • jQuery API
  • Works in browser and Node.js
  • Supports different drivers (runners):
  • Fully async

Test Suites

All statements are of the 4 types: traversers, events, actions and assertions.

Traversers

A traverser component creates elements scope (context) for nested components. The context is the jQuery-alike instance.

:star: Components also perform assertions after query to ensure the elements are found

  • find (selector)

    :exclamation: If no elements are found, the filter method is then called.

  • filter (selector)

  • children (selector)

  • parent (selector)

  • closest (selector)

find ('.foo') {
	// do assertions in this elements scope
	// e.g: check that that 2 elements are in the context
	eq length 2;
}

:tada: When selector has no whitespace '' are optional

find (section) {
	// ...
}

Events

An event component triggers native or custom events on current scope

trigger eventName (...args);

:star: Known event names don't need trigger keyword.

blur, focus, resize, scroll, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, submit, keydown, keypress, keyup.

:tada: When statement has no arguments () are optional.

find (button) {
	click;
}

:exclamation: Cheerio driver supports no events, as it tests only html AST in Node.js environment

Actions

An action component performs some action, calls javascript functions, mutates the dom, or runs a group of actions.

Keyboard

  • type (text)

    Text can contain meta key definitions in {}, e.g.:

    find ('input.foo') {
    	type ('Hello friends!{enter}');
    }
  • press (keyCombination)

    Key combination (hotkey): ctrl+f, alt+c+b, a+d e.g.

    find ('input.foo') {
    	press ('alt+f');
    }

Functions

A function component simply call the jQuery function.

call functionName (...args)

Select

select component selects a text range or an option, depends on a current context

find ('input.foo') {
	// selects everything
	select;
	// selects range
	select (0, 5);
	// finds text and selects it
	select ('foo');
}
find ('select.city') {
	// select an option by `value`, or `name` or `textContent`
	select ('London');
}

Assertions

An assertion component reads current contexts property or calls a function to compare the value with expected value.

:eyeglasses: Assertion components with aliases: equal ~ eq, notEqual ~ notEq, lessThan ~ lt, lessThanOrEqaul ~ lte, greaterThan ~ gt, greaterThanOrEqual ~ gte, deepEqual ~ deepEq, notDeepEqual ~ notDeepEq, has, hasNot

  • eq propertyName (expectedValue)
  • eq functionName (...args, expectedValue)

:tada: As the accent in this library is made for assertions, so when no event, action, traverser, etc is found, then we assume it is the eqaul component, so you can skip the equal keyword.

find ('button') {
	// should find one element
	eq length (1);
	// same
	eq length 1;
	// same
	length 1;
}
find ('.baz') {
	eq attr (name, 'Baz');
	// same
	eq attr name Baz;
	// same
	attr name Baz;

	// check class name
	eq hasClass ('baz', true);
	// same
	hasClass ('baz', true);
	// same
	hasClass baz;
}
Sample

Assume this DOM

<div class='foo baz' onclick="this.style.display = 'none'">Foo</div>
<input value='A' />

Test suite sample:

// finds all `.foo` elements and sets them for assertion scope
find ('.foo')  {
	// check text property
	text Foo;
	hasClass baz;
	// perform action
	click;
	// check visibility
	css display none;
	// or
	is hidden;
	// check bg color
	css ('background-color', 'red');
}
find ('input') {
	val A;
	press backspace;

	type Baz;
	val Baz;
}

Install

$ bower install domtest
# or
$ npm install domtest

API

  • DomTest()

    DomTest(Element: Node, MaskTestSuite | Array<MaskTestSuite>): Runner

    Create default DomLibRunner and immediately start the test.

  • DomTest.use(driver:string, settings:object): Runner

    Create the Test Runner. Available runners:

    • 'domlib'
    • 'jmask'
    • 'cheerio'
    • 'selenium'

    :exclamation: Note, that cheerio and selenium can be evaluated in Node.JS only, and domlib in browser only, jmask can be used in browser and Node.js

Runner

Runner implements IEventEmitter, IPromise

  • Runner::process(root:any, suite: MaskTestSuite | Array<MaskTestSuite>)

    • root is the root object, which runner uses for test. Refer to the particular Runner to find out the required root-type.
    • suite(s) is/are the tests to be run for the root object
Events
- `.on(event, handler)`

| Event | Data | Description |
|--------|-----------| ------------|
| `fail`  | `error: AssertionError` | Failed assertion |
| `success`| - | Successful assertion |
| `progress` | `runner:Runner, node: MaskNode, error:AssertionError` | On each step. `error` is null, when assertion is ok |
| `complete` | `errors:Array<AssertionError>` | Runner has finished all tests |

- `.off(event, handler)`

MaskTestSuite:string|MaskAST|function

  • string|MaskAST: Mask template with the components. See the Test Suite for supported components
  • function: Custom function

Runners

Domlib

Uses jQuery-like dom library to test HtmlElements in browser. (@default)

DomTest(document.body, `
	find ('button') {
		click;
		text ('Foo');
	}
`);
jMask

Uses jMask library to test MaskAST in browser or Node.js

var template = 'section > input placeholder="Foo";'
DomTest
	.use('jmask')
	.process(template, `
		find('input') {
			attr placeholder Foo;
		}
	`);
Cheerio

Uses cheerio module to test HTML in Node.js.

:exclamation: No actions are available here, as no listeners can be attached to plain HTML.

var template = '<section><input placeholder="Foo"></section>';
DomTest
	.use('cheerio')
	.process(template, `
		find('input') {
			attr placeholder Foo;
		}
	`);

DomTest
	.use('cheerio')
	.process('http://google.com', `
		find('input') {

		}
	`);
Selenium

Uses selenium-webdriver module to test the Web Pages

DomTest
	.use('selenium', Options)
	.process('https://developer.mozilla.org/en/', `
		find('input[name=q]') {
			type XmlHttpRequest;
			trigger submit;
		}
		await ('.result-list') {
			prop tag ul;
			children {
				notEq length 0;
				eq (1) {
					has text XmlHttpRequest;
				}
			}
		}
	`);

Options default

{
	name: 'Chrome',
	args: ['no-sandbox'],
	binaryPath: null,
}

For more information, please refer to sources: config.es6

:exclamation: you can override functions to set any webdriver option you need.


:copyright: MIT The AtmaJS Project