Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate screenshots during tests #266

Merged
merged 2 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ production-key.pem
build/
dev/
dll/
screenshots/

*.zip
*.xpi
Expand Down
4 changes: 2 additions & 2 deletions features/settings-ui.feature
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ Feature: Wallet UI Settings
When I navigate to the general settings screen
And I open General Settings language selection dropdown
And I select Japanese language
Then I should see Japanese language as selected
Then The Japanese language should be selected
When I refresh the page
Then I should see Japanese language as selected
Then The Japanese language should be selected

@it-3
Scenario: Yoroi Settings Screen / Terms of Use in Default English(IT-3)
Expand Down
57 changes: 56 additions & 1 deletion features/step_definitions/common-steps.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,77 @@
// @flow

import { BeforeAll, Given, After, AfterAll } from 'cucumber';
import { Before, BeforeAll, Given, After, AfterAll, setDefinitionFunctionWrapper } from 'cucumber';
import { getMockServer, closeMockServer } from '../support/mockServer';
import { buildFeatureData, getFeatureData, getFakeAddresses } from '../support/mockDataBuilder';
import i18nHelper from '../support/helpers/i18n-helpers';

const { promisify } = require('util');
const fs = require('fs');
const rimraf = require('rimraf');

const screenshotsDir = './screenshots/';

/** We need to keep track of our progress in testing to give unique names to screenshots */
const testProgress = {
scenarioName: '',
lineNum: 0, // we need this to differentiate scenarios with multiple "examples"
step: 0
};

BeforeAll(() => {
rimraf.sync(screenshotsDir);
fs.mkdirSync(screenshotsDir);

getMockServer({});
});

AfterAll(() => {
closeMockServer();
});

Before((scenario) => {
// cleanup scenario name so it is folder-name friendly
testProgress.scenarioName = scenario.pickle.name.replace(/[^0-9a-z_ ]/gi, '');
testProgress.lineNum = scenario.sourceLocation.line;
testProgress.step = 0;
});


After(async function () {
await this.driver.quit();
});

const writeFile = promisify(fs.writeFile);

/** Wrap every step to take screenshots for UI-based testing */
setDefinitionFunctionWrapper((fn, _, pattern) => {
if (!pattern) {
return fn;
}
return async function (...args) {
const ret = await fn.apply(this, args);

// Regex patterns contain non-ascii characters.
// We want to remove this to get a filename-friendly string
const cleanString = pattern.toString().replace(/[^0-9a-z_ ]/gi, '');

if (cleanString.includes('I should see')) {
// path logic
const dir = `${screenshotsDir}/${testProgress.scenarioName}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const path = `${dir}/${testProgress.step}_${testProgress.lineNum}-${cleanString}.png`;

const screenshot = await this.driver.takeScreenshot();
await writeFile(path, screenshot, 'base64');
}

testProgress.step += 1;
return ret;
};
});

Given(/^I am testing "([^"]*)"$/, feature => {
buildFeatureData(feature);
});
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/general-settings-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Then(/^I should see secondary menu (.*) item disabled$/, async function (buttonN
await this.waitUntilText(buttonSelector, label);
});

Then(/^I should see Japanese language as selected$/, async function () {
Then(/^The Japanese language should be selected$/, async function () {
this.driver.wait(async () => {
const activeLanguage = await i18n.getActiveLanguage(this.driver);
return activeLanguage === 'ja-JP';
Expand Down
Loading