GithubHelp home page GithubHelp logo

wildpeaks / 2016-package-electron-test-utils Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 14 KB

[ARCHIVED] Test utilities for DOM tests running in Electron

License: MIT License

JavaScript 100.00%
electron npm-package

2016-package-electron-test-utils's Introduction

Electron Test Utilities

Collection of helpers for DOM tests running in Electron.

Install:

npm install @wildpeaks/electron-test-utils --save-dev

Function "getBounds"

Calculates the bounding box of an HTMLElement in the page.

This is useful to check how CSS styles or text contents affect the size of known elements, or test things like whether important elements are above the fold or not.

Usage:

getBounds(HTMLElement element)

Example:

const {getBounds} = require('@wildpeaks/electron-test-utils');

const container = document.getElementById('application');
const bbox = getBounds(container);

Function "hasFocus"

Checks if an HTMLElement has focus.

Usage:

hasFocus(HTMLElement element)

Example:

const assert = require('assert');
const {hasFocus} = require('@wildpeaks/electron-test-utils');

const textfield = document.getElementById('username');
assert.ok(hasFocus(textfield), 'The text input does not have focus initially');
someFunctionBeingTested();
assert.ok(hasFocus(textfield), 'The text input has focus afterwards');

Function "simulateClick"

Simulates an arbitrary mouse click in the page.

This (along with screenshot) is also useful for running scenarios against Canvas-based (2D Canvas or WebGL) user interfaces.

Usage:

simulateClick(Object options)

Options:

  • x / y: (Integer) Coordinates in the page where the mouse is clicked

  • button: (String) Mouse button: left, middle or right

  • modifiers: (Array of String) Active special keys such as shift or ctrl. See the Electron API for the list of available modifiers.

Example:

const {spy} = require('sinon');
const {simulateClick} = require('@wildpeaks/electron-test-utils');

const onclick = spy();
const button = document.getElementById('mybutton');
button.addEventListener('click', onclick);

assert.strictEqual(onclick.called, false, 'The button has not been clicked yet');

simulateClick({
	x: 10,
	y: 20,
	button: 'left',
	modifiers: []
});

setTimeout(() => {
	assert.strictEqual(onclick.called, true, 'The button has been clicked');
});

Function "simulateKey"

Simulate an arbitrary keyboard key.

Usage:

simulateKey(Object options)

Options:

  • keyCode: (String) Key being sent, such as a, enter or tab. See the Electron API for the list of available key codes.

  • modifiers: (Array of String) Active special keys such as shift or ctrl. See the Electron API for the list of available modifiers.

Example:

const assert = require('assert');
const {simulateKey} = require('@wildpeaks/electron-test-utils');

const textfield = document.getElementById('username');
textfield.focus();

assert.strictEqual(textfield.value, '', 'Value before');

simulateKey({keyCode: 'H', modifiers: ['shift']});
simulateKey({keyCode: 'e'});
simulateKey({keyCode: 'l'});
simulateKey({keyCode: 'l'});
simulateKey({keyCode: 'o'});

setTimeout(() => {
	assert.strictEqual(textfield.value, 'Hello', 'Value after');
});

Function "screenshot"

Takes a screenshot of an HTMLElement.

Useful to compare against a reference screenshot (for visual regression tests) or simply save to file (to have an overview of key parts of an application).

However, remember that antialiasing and font rendering can differ depending on the operating system, e.g. the resulting image might look slightly different on Travis CI than on your development machine.

Usage:

screenshot(HTMLElement element, Function callback)

Callback parameters:

Example:

const fs = require('fs');
const {screenshot} = require('@wildpeaks/electron-test-utils');

screenshot(
	document.getElementById('application'),
	image => {
		fs.writeFile('screenshot.png', image.toPNG(), () => {
			console.log('Saved the screenshot');
		});
	}
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.