Skip to content

Commit

Permalink
Nightwatch.js: Use plugin nightwatch-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
piecioshka committed May 14, 2019
1 parent 97a75d7 commit ac78a05
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
5 changes: 4 additions & 1 deletion nightwatch.conf.js
@@ -1,5 +1,5 @@
module.exports = {
"src_folders": ["test"],
"src_folders": ["test/e2e/"],

"detailed_output": false,

Expand All @@ -9,6 +9,9 @@ module.exports = {
"port": 9515
},

"custom_commands_path": ["node_modules/nightwatch-helpers/commands"],
"custom_assertions_path": ["node_modules/nightwatch-helpers/assertions"],

"test_settings": {
"default": {
"desiredCapabilities": {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -14,6 +14,7 @@
"license": "MIT",
"devDependencies": {
"chromedriver": "^74.0.0",
"nightwatch": "^1.0.19"
"nightwatch": "^1.0.19",
"nightwatch-helpers": "^1.2.0"
}
}
21 changes: 7 additions & 14 deletions test/test.js → test/e2e/basic.test.js
@@ -1,32 +1,25 @@
module.exports = {};

function test(name, cb) {
module.exports[name] = cb;
}
const { afterEach, beforeEach, test } = require('../helpers/')(module);

const PAGE_URL = 'https://piecioshka.pl/blog';
const PAGE_LOAD_DELAY = 3000;

async function setup(client) {
beforeEach(async (client) => {
console.log('beforeEach');
await client.url(PAGE_URL);
await client.waitForElementVisible('body', PAGE_LOAD_DELAY);
}
});

async function teardown(client) {
// ...
}
afterEach(() => {
console.log('afterEach');
});

test('Spr. czy istnieje avatar', async (client) => {
await setup(client);
await client.assert.elementPresent('#me img');
// await teardown(client);
});

test('Spr. czy jest odpowiednia liczba elementów w menu', async (client) => {
await setup(client);
await client.elements('css selector', '.menu-item', (result) => {
const menuSize = result.value.length;
client.assert.equal(menuSize, 10);
});
// await teardown(client);
});
7 changes: 7 additions & 0 deletions test/e2e/use-plugin.test.js
@@ -0,0 +1,7 @@
const { test } = require('../helpers/')(module);

test('Spr. czy istnieją wymagane paragrafy?', async (client) => {
await client.url('http://example.org');
await client.waitForElementVisible('body');
await client.assert.count('p', 2);
});
32 changes: 32 additions & 0 deletions test/helpers/index.js
@@ -0,0 +1,32 @@
function test(module) {
return (name, cb) => {
module.exports[name] = cb;
}
}

function aspectize(name, mod) {
return (cb) => {
const previous = mod.exports[name];
mod.exports[name] = (client) => {
if (typeof previous === 'function') {
previous(client);
}
cb(client);
};
}
}

function beforeEach(mod) {
return aspectize('beforeEach', mod);
}

function afterEach(mod) {
return aspectize('afterEach', mod);
}

module.exports = (mod) => ({
it: test(mod),
test: test(mod),
beforeEach: beforeEach(mod),
afterEach: afterEach(mod),
});

0 comments on commit ac78a05

Please sign in to comment.