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

Sdk 577 a11y stability #32

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
110 changes: 71 additions & 39 deletions nightwatch/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const eventHelper = require('../src/utils/eventHelper');
const localTunnel = new LocalTunnel();
const testObservability = new TestObservability();
const accessibilityAutomation = new AccessibilityAutomation();
const TestHubHandler = require('../src/testHub/testHubHandler');
const testHubHandler = new TestHubHandler();
const testHubUtils = require('../src/testHub/utils');

const nightwatchRerun = process.env.NIGHTWATCH_RERUN_FAILED;
const nightwatchRerunFile = process.env.NIGHTWATCH_RERUN_REPORT_FILE;
Expand Down Expand Up @@ -40,6 +43,46 @@ const handleScreenshotUpload = async (data) => {
}
};

const setupAccessibility = async (settings) => {
if (helper.isCucumberTestSuite()) { return }

try {
accessibilityAutomation.configure(settings);
if (helper.isAccessibilitySession()) {
if (accessibilityAutomation._user && accessibilityAutomation._key) {
const [jwtToken, testRunId] = await accessibilityAutomation.createAccessibilityTestRun();
process.env.BS_A11Y_JWT = jwtToken;
process.env.BS_A11Y_TEST_RUN_ID = testRunId;
if (helper.isAccessibilitySession()) {
accessibilityAutomation.setAccessibilityCapabilities(settings);
}
}
}
} catch (error) {
Logger.error(`Could not configure or launch accessibility automation - ${error}`);
}
};

const setupTestObservability = async (settings) => {
if (helper.isCucumberTestSuite()) { return }

try {
testObservability.configure(settings);
if (helper.isTestObservabilitySession()) {
settings.globals['customReporterCallbackTimeout'] = CUSTOM_REPORTER_CALLBACK_TIMEOUT;
if (testObservability._user && testObservability._key) {
await testObservability.launchTestSession();
}
if (process.env.BROWSERSTACK_RERUN === 'true' && process.env.BROWSERSTACK_RERUN_TESTS && process.env.BROWSERSTACK_RERUN_TESTS!=='null') {
const specs = process.env.BROWSERSTACK_RERUN_TESTS.split(',');
await helper.handleNightwatchRerun(specs);
}
}
} catch (error) {
Logger.error(`Could not configure or launch test observability - ${error}`);
}
};

module.exports = {

reporter: async function(results, done) {
Expand Down Expand Up @@ -77,7 +120,7 @@ module.exports = {
registerEventHandlers(eventBroadcaster) {

eventBroadcaster.on('TestCaseStarted', async (args) => {
if (!helper.isTestObservabilitySession()) {
if (!testHubUtils.shouldProcessEventForTestHub()) {
return;
}
try {
Expand Down Expand Up @@ -133,7 +176,7 @@ module.exports = {
});

eventBroadcaster.on('TestCaseFinished', async (args) => {
if (!helper.isTestObservabilitySession()) {
if (!testHubUtils.shouldProcessEventForTestHub()) {
return;
}
try {
Expand All @@ -156,7 +199,7 @@ module.exports = {
});

eventBroadcaster.on('TestStepStarted', async (args) => {
if (!helper.isTestObservabilitySession()) {
if (!testHubUtils.shouldProcessEventForTestHub()) {
return;
}
try {
Expand Down Expand Up @@ -189,7 +232,7 @@ module.exports = {
});

eventBroadcaster.on('TestStepFinished', async (args) => {
if (!helper.isTestObservabilitySession()) {
if (!testHubUtils.shouldProcessEventForTestHub()) {
return;
}
try {
Expand Down Expand Up @@ -293,47 +336,35 @@ module.exports = {
}

try {
testObservability.configure(settings);
if (helper.isTestObservabilitySession()) {
if (helper.isCucumberTestSuite(settings)) {
cucumberPatcher();
process.env.CUCUMBER_SUITE = 'true';
settings.test_runner.options['require'] = path.resolve(__dirname, 'observabilityLogPatcherHook.js');
}
settings.globals['customReporterCallbackTimeout'] = CUSTOM_REPORTER_CALLBACK_TIMEOUT;
if (testObservability._user && testObservability._key) {
await testObservability.launchTestSession();
}
testHubHandler.configure(settings);
if (testHubUtils.shouldProcessEventForTestHub()) {
await testHubHandler.launchBuild();
cucumberPatcher();
if (process.env.BROWSERSTACK_RERUN === 'true' && process.env.BROWSERSTACK_RERUN_TESTS && process.env.BROWSERSTACK_RERUN_TESTS!=='null') {
const specs = process.env.BROWSERSTACK_RERUN_TESTS.split(',');
await helper.handleNightwatchRerun(specs);
}
}
} catch (error) {
Logger.error(`Could not configure or launch test observability - ${error}`);
}

try {
accessibilityAutomation.configure(settings);
if (helper.isAccessibilitySession()) {
if (accessibilityAutomation._user && accessibilityAutomation._key) {
const [jwtToken, testRunId] = await accessibilityAutomation.createAccessibilityTestRun();
process.env.BS_A11Y_JWT = jwtToken;
process.env.BS_A11Y_TEST_RUN_ID = testRunId;
if (helper.isAccessibilitySession()) {
accessibilityAutomation.setAccessibilityCapabilities(settings);
}
}
accessibilityAutomation.setAccessibilityCapabilities(settings);
}

} catch (error) {
Logger.error(`Could not configure or launch accessibility automation - ${error}`);
Logger.error(`Could not configure or launch testHub Build - ${error}`);
}

await setupTestObservability(settings);
await setupAccessibility(settings);

if (testHubUtils.shouldProcessEventForTestHub()) {
settings.test_runner.options['require'] = path.resolve(__dirname, 'observabilityLogPatcherHook.js');
}
},

async after() {
localTunnel.stop();
if (helper.isTestObservabilitySession()) {
await testHubHandler.stopTestHub();
if (helper.isTestObservabilitySession() && !helper.isCucumberTestSuite()) {
process.env.NIGHTWATCH_RERUN_FAILED = nightwatchRerun;
process.env.NIGHTWATCH_RERUN_REPORT_FILE = nightwatchRerunFile;
if (process.env.BROWSERSTACK_RERUN === 'true' && process.env.BROWSERSTACK_RERUN_TESTS) {
Expand All @@ -347,27 +378,28 @@ module.exports = {
} catch (error) {
Logger.error(`Something went wrong in stopping build session for test observability - ${error}`);
}
process.exit();
}
if (helper.isAccessibilitySession()){
if (helper.isAccessibilitySession() && !helper.isCucumberTestSuite()){
try {
await accessibilityAutomation.stopAccessibilityTestRun();
} catch (error) {
Logger.error(`Exception in stop accessibility test run: ${error}`);
}

}
process.exit();
},

async beforeEach(settings) {
browser.getAccessibilityResults = () => { return accessibilityAutomation.getAccessibilityResults() };
browser.getAccessibilityResultsSummary = () => { return accessibilityAutomation.getAccessibilityResultsSummary() };
// await accessibilityAutomation.beforeEachExecution(browser);
if (helper.isAccessibilitySession()) {
helper.modifySeleniumCommands();
helper.modifyNightwatchCommands();
browser.getAccessibilityResults = () => { return accessibilityAutomation.getAccessibilityResults() };
browser.getAccessibilityResultsSummary = () => { return accessibilityAutomation.getAccessibilityResultsSummary() };
}
},

// This will be run after each test suite is finished
// This will be run after each test suite is finished for default nightwatch runner
async afterEach(settings) {
// await accessibilityAutomation.afterEachExecution(browser);
},

beforeChildProcess(settings) {
Expand Down
13 changes: 9 additions & 4 deletions nightwatch/observabilityLogPatcherHook.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
try {
const {Before} = require('@cucumber/cucumber');

Before((testCase) => {
console.log(`TEST-OBSERVABILITY-PID-TESTCASE-MAPPING-${testCase.testCaseStartedId}`);
const {Before, After} = require('@cucumber/cucumber');
const testhubUtils = require('@nightwatch/browserstack/src/testHub/utils');

Before(async (testCase) => {
await testhubUtils.beforeEachCucumberTest(testCase);
});

After(async (testCase) => {
await testhubUtils.afterEachCucumberTest(testCase);
});

} catch (error) { /* empty */ }
101 changes: 41 additions & 60 deletions src/accessibilityAutomation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {makeRequest} = require('./utils/requestHelper');
const Logger = require('./utils/logger');
const {ACCESSIBILITY_URL} = require('./utils/constants');
const util = require('util');
const scripts = require('./utils/scripts');

class AccessibilityAutomation {
configure(settings = {}) {
Expand Down Expand Up @@ -73,7 +74,10 @@ class AccessibilityAutomation {
source: {
frameworkName: helper.getFrameworkName(this._testRunner),
frameworkVersion: helper.getPackageVersion('nightwatch'),
sdkVersion: helper.getAgentVersion()
sdkVersion: helper.getAgentVersion(),
language: 'javascript',
testFramework: 'selenium',
testFrameworkVersion: helper.getPackageVersion('selenium-webdriver')
},
settings: accessibilityOptions,
versionControl: await helper.getGitMetaData(),
Expand All @@ -91,9 +95,11 @@ class AccessibilityAutomation {
}
};

const response = await makeRequest('POST', 'test_runs', data, config, ACCESSIBILITY_URL);
const response = await makeRequest('POST', 'v2/test_runs', data, config, ACCESSIBILITY_URL);
const responseData = response.data.data || {};

scripts.parseFromJson(responseData);
scripts.toJson();
accessibilityOptions.scannerVersion = responseData.scannerVersion;
process.env.BROWSERSTACK_ACCESSIBILITY_OPTIONS = JSON.stringify(accessibilityOptions);

Expand Down Expand Up @@ -353,11 +359,13 @@ class AccessibilityAutomation {

async beforeEachExecution(testMetaData) {
try {
this.currentTest = browser.currentTest;
this.currentTest = browser.currentTest || {};
this.currentTest.shouldScanTestForAccessibility = this.shouldScanTestForAccessibility(
testMetaData
);
global.shouldScanTestForAccessibility = this.currentTest.shouldScanTestForAccessibility;
this.currentTest.accessibilityScanStarted = true;
global.isAccessibilityPlatform = true;
this._isAccessibilitySession = this.setExtension(browser);

if (this.isAccessibilityAutomationSession() && browser && helper.isAccessibilitySession() && this._isAccessibilitySession) {
Expand All @@ -381,25 +389,6 @@ class AccessibilityAutomation {
Logger.info(
'Setup for Accessibility testing has started. Automate test case execution will begin momentarily.'
);

await browser.executeAsyncScript(`
const callback = arguments[arguments.length - 1];
const fn = () => {
window.addEventListener('A11Y_TAP_STARTED', fn2);
const e = new CustomEvent('A11Y_FORCE_START');
window.dispatchEvent(e);
};
const fn2 = () => {
window.removeEventListener('A11Y_TAP_STARTED', fn);
callback();
}
fn();
`);
} else {
await browser.executeAsyncScript(`
const e = new CustomEvent('A11Y_FORCE_STOP');
window.dispatchEvent(e);
`);
}
}
this.currentTest.accessibilityScanStarted =
Expand All @@ -419,51 +408,43 @@ class AccessibilityAutomation {

async afterEachExecution(testMetaData) {
try {
if (this.currentTest.accessibilityScanStarted && this.isAccessibilityAutomationSession() && this._isAccessibilitySession) {
if (this.currentTest.shouldScanTestForAccessibility) {
const shouldScanTestForAccessibility = this.currentTest ? this.currentTest.shouldScanTestForAccessibility : this.shouldScanTestForAccessibility(
testMetaData
);
const accessibilityScanStarted = this.currentTest ? this.currentTest.accessibilityScanStarted : true;
this._isAccessibilitySession = this.setExtension(browser);
if (accessibilityScanStarted && this.isAccessibilityAutomationSession() && this._isAccessibilitySession) {
if (shouldScanTestForAccessibility) {
Logger.info(
'Automate test case execution has ended. Processing for accessibility testing is underway. '
);
}
const dataForExtension = {
saveResults: this.currentTest.shouldScanTestForAccessibility,
testDetails: {
name: testMetaData.testcase,
testRunId: process.env.BS_A11Y_TEST_RUN_ID,
filePath: testMetaData.metadata.modulePath,
scopeList: [testMetaData.metadata.name, testMetaData.testcase]
},
platform: await this.fetchPlatformDetails(browser)
};
const final_res = await browser.executeAsyncScript(
`
const callback = arguments[arguments.length - 1];

this.res = null;
if (arguments[0].saveResults) {
window.addEventListener('A11Y_TAP_TRANSPORTER', (event) => {
window.tapTransporterData = event.detail;
this.res = window.tapTransporterData;
callback(this.res);
});
}
const e = new CustomEvent('A11Y_TEST_END', {detail: arguments[0]});
window.dispatchEvent(e);
if (arguments[0].saveResults !== true ) {
callback();
}
`,
dataForExtension
);
if (this.currentTest.shouldScanTestForAccessibility) {
Logger.info('Accessibility testing for this test case has ended.');
let dataForExtension = {};
if (helper.isCucumberTestSuite()) {
dataForExtension = {
thTestRunUuid: process.env.TEST_OPS_TEST_UUID,
thBuildUuid: process.env.BROWSERSTACK_TESTHUB_UUID,
thJwtToken: process.env.BROWSERSTACK_TESTHUB_JWT
};
} else {
dataForExtension = {
saveResults: shouldScanTestForAccessibility,
testDetails: {
name: testMetaData.testcase,
testRunId: process.env.BS_A11Y_TEST_RUN_ID,
filePath: testMetaData.metadata.modulePath,
scopeList: [testMetaData.metadata.name, testMetaData.testcase]
},
platform: await this.fetchPlatformDetails(browser)
};
}
Logger.debug('Performing scan before saving results');
Logger.debug(util.format(await browser.executeAsyncScript(scripts.performScan, {method: testMetaData.testcase})));
await browser.executeAsyncScript(scripts.saveTestResults, dataForExtension);
Logger.info('Accessibility testing for this test case has ended.');
}
} catch (er) {
Logger.error(
`Accessibility results could not be processed for the test case ${this.currentTest.module}. Error :`,
er
);
Logger.error('Accessibility results could not be processed for the test case. Error: ' + er.toString());
}
}

Expand Down
Loading
Loading