Skip to content

Commit

Permalink
fix: some small fixes + tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
forsti0506 committed Feb 27, 2021
1 parent 79d0b47 commit ade839f
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 43 deletions.
3 changes: 1 addition & 2 deletions bin/a11y-sitechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { entry } from '../lib/a11y-sitechecker';

import { setupAxeConfig, setupConfig } from '../lib/utils/setup-config';

// Here we're using Commander to specify the CLI options
commander
.version(pkg.version)
.usage('[options] <paths>')
.option('-j, --json', 'Output results as JSON. Otherwise output is displayed on the console')
.option('--config <string>', 'Provide a config.json')
.option(
'-T, --threshold <number>',
'--threshold <number>',
'permit this number of errors, warnings, or notices, otherwise fail with exit code 2',
'0',
)
Expand Down
8 changes: 4 additions & 4 deletions lib/a11y-sitechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ export async function entry(
process.exit(0);
}
}
} catch (error) {
} catch (err) {
// Handle any errors
console.error(error.message);
console.error(error.stackTrace);
process.exit(1);
error(err.message);
debug(err.stackTrace);
throw err;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Config {
analyzeClicks?: boolean;
analyzeClicksWithoutNavigation?: boolean;
threshold: number;
timeout: number;
}

interface LoginStep {
Expand Down
17 changes: 12 additions & 5 deletions lib/utils/login.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Page } from 'puppeteer';
import { error, saveScreenshot, waitForHTML } from './helper-functions';
import { debug, error, saveScreenshot, waitForHTML } from './helper-functions';
import * as chalk from 'chalk';
import { Config } from '../models/config';

export async function executeLogin(url: string, page: Page, config: Config): Promise<void> {
export async function executeLogin(url: string, page: Page, config: Config): Promise<number> {
if (!config.login) {
return;
debug('No Login credentials specified');
return 0;
}
let failedLoads = 0;
let failed = true;
while (failedLoads < 3 && failed) {
failed = false;
try {
try {
debug('Navigating to url: ' + url);
await page.goto(url, { waitUntil: 'networkidle2' });
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: config.timeout });
} catch (e) {
error(e);
}
Expand All @@ -29,17 +31,22 @@ export async function executeLogin(url: string, page: Page, config: Config): Pro

for (const step of config.login) {
for (const input of step.input) {
await page.waitForSelector(input.selector);
await page.waitForSelector(input.selector, { timeout: config.timeout });
debug('Waited for selector ' + input.selector);
await page.type(input.selector, input.value);
}
debug('Clicking submit: ' + step.submit);
await page.click(step.submit);
}
try {
await page.waitForNavigation({ waitUntil: 'networkidle2' });
debug('Navigation finished: ' + page.url());
await waitForHTML(page);
await saveScreenshot(page, config.imagesPath, 'afterLogin.png', config.saveImages);
} catch (e) {
// eslint-disable-next-line prettier/prettier
console.log(chalk.red('No Navigation after Login. Please check if it\'s working as expected!'));
}
debug('Finished Login Script: ' + url);
return 1;
}
20 changes: 14 additions & 6 deletions lib/utils/setup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { Spec } from 'axe-core';
import * as fs from 'fs';
import { error, setDebugMode } from './helper-functions';
import { Config } from '../models/config';
import { OptionValues } from 'commander';

export function setupConfig(commander): Config {
const config: Config = { json: true, resultsPath: 'results', axeConfig: {}, threshold: 0, imagesPath: 'images' };
config.json = commander.json;
export function setupConfig(options: OptionValues): Config {
const config: Config = {
json: true,
resultsPath: 'results',
axeConfig: {},
threshold: 0,
imagesPath: 'images',
timeout: 30,
};
config.json = options.json;
if (!config.threshold) {
config.threshold = parseInt(commander.threshold);
config.threshold = parseInt(options.threshold);
}
if (commander.config) {
if (options.config) {
try {
const configFile = JSON.parse(fs.readFileSync(commander.config).toString('utf-8'));
const configFile = JSON.parse(fs.readFileSync(options.config).toString('utf-8'));
if (typeof configFile.json === 'boolean') {
config.json = configFile.json;
}
Expand Down
27 changes: 15 additions & 12 deletions package-lock.json

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

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "a11y-sitechecker",
"version": "1.6.4",
"version": "1.8.0",
"description": "",
"repository": {
"type": "git",
Expand All @@ -11,9 +11,8 @@
"dependencies": {
"@axe-core/puppeteer": "^4.1.1",
"chalk": "^4.1.0",
"commander": "^6.2.1",
"commander": "^7.1.0",
"jsdom": "^16.4.0",
"odiff": "^1.4.2",
"prettyjson": "^1.2.1",
"puppeteer": "^5.5.0",
"uuid": "^8.3.2"
Expand All @@ -26,7 +25,7 @@
"@semantic-release/github": "^7.2.0",
"@semantic-release/npm": "^7.0.9",
"@semantic-release/release-notes-generator": "^9.0.1",
"@types/jasmine": "^3.6.3",
"@types/jasmine": "^3.6.4",
"@types/uuid": "^8.3.0",
"@types/jsdom": "^16.2.5",
"@types/node": "^14.14.20",
Expand Down
24 changes: 24 additions & 0 deletions tests/a11y-sitechecker-bin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as process from 'child_process';

import 'jasmine';

describe('a11y-sitechecker-bin', function () {
it('should be the same url', async (done) => {
const url = 'forsti.eu';
const job = process.spawn('node', ['./dist/bin/a11y-sitechecker.js', url, '--config=config.json']);
job.stdout.on('data', (data) => {
expect(data.toString()).toContain(
'#############################################################################################',
);
done();
});

job.stderr.on('data', (data) => {
console.error(`\n${data}`);
});

job.on('exit', function (code, signal) {
console.log('child process exited with ' + `code ${code} and signal ${signal}`);
});
});
});
32 changes: 24 additions & 8 deletions tests/a11y-sitechecker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import 'jasmine';

import { setupAxeConfig, setupConfig } from '../lib/utils/setup-config';
import { entry } from '../dist';

describe('a11y-sitechecker-bin', function () {
describe('a11y-sitechecker', function () {
let config;
beforeEach(function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
spyOn(process, 'exit').and.stub();
spyOn(process, 'exit').withArgs(2).and.throwError('Exit with error 2');
const optionValues = {
json: true,
config: './tests/config.json',
};

config = setupConfig(optionValues);
});

it('testcall', async (done) => {
const commander = { json: true, config: './tests/config.json' };
const config = setupConfig(commander);
it('should be the same url', async (done) => {
config.threshold = 1000;
if (config.axeConfig) config.axeConfig.locale = 'de';
const axeConfig = setupAxeConfig(config);

entry(config, axeConfig, 'www.forsti.eu').then(
entry(config, axeConfig, 'forsti.eu', true).then((e) => {
expect(e.url).toBe('forsti.eu');
done();
});
});

it('should exit with code 2', async (done) => {
config.threshold = 0;
if (config.axeConfig) config.axeConfig.locale = 'de';
const axeConfig = setupAxeConfig(config);

entry(config, axeConfig, 'forsti.eu', true).then(
(e) => {
done();
},
(error) => {
console.log(error);
expect(error.message).toContain('Exit with error 2');
done();
},
);
});
Expand Down
5 changes: 3 additions & 2 deletions tests/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"json": false,
"json": true,
"imagesPath": "tests/images",
"axeConfig": {
"locale": "de"
},
Expand All @@ -12,7 +13,7 @@
},
"debugMode": true,
"urlsToAnalyze": [
"http://www.forsti.eu"
"https://forsti.eu"
],
"analyzeClicksWithoutNavigation": false
}
75 changes: 75 additions & 0 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { setupConfig } from '../lib/utils/setup-config';
import 'jasmine';
import { executeLogin } from '../lib/utils/login';
import * as puppeteer from 'puppeteer';
import { Config } from '../lib/models/config';
import { Browser } from 'puppeteer';

describe('login', function () {
let config: Config;
let browser: Browser;
beforeEach(async function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1200000;
const optionValues = {
json: true,
config: './tests/config.json',
};
config = setupConfig(optionValues);
config.timeout = 15000;
browser = await puppeteer.launch(config.launchOptions);
const page = (await browser.pages())[0];
await page.setViewport({
width: 1920,
height: 1080,
});
});

it('should escape if no login parameter', async (done) => {
executeLogin('https://forsti.eu', (await browser.pages())[0], config).then((retCode) =>
expect(retCode).toBe(0),
);
done();
});

it('should escape if no login parameter', async (done) => {
config.login = [
{
submit: 'test',
input: [
{
selector: 'test',
value: 'test',
},
],
},
];

await executeLogin('https://forsti.eu', (await browser.pages())[0], config).catch((err) => {
expect(err.message).toContain('waiting for selector `test` failed:');
done();
});
});

it('should not be able to login', async (done) => {
config.login = [
{
submit: '#wp-submit',
input: [
{
selector: '#user_login',
value: 'test',
},
{
selector: '#user_pass',
value: 'test',
},
],
},
];

await executeLogin('https://forsti.eu/wp-admin', (await browser.pages())[0], config).then((r) => {
expect(r).toBe(1);
done();
});
});
});

0 comments on commit ade839f

Please sign in to comment.