Skip to content

Commit

Permalink
Add support for accessibility for nightwatch (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
karanshah-browserstack authored Oct 2, 2023
1 parent fe25b2b commit 8937856
Show file tree
Hide file tree
Showing 8 changed files with 899 additions and 20 deletions.
53 changes: 52 additions & 1 deletion nightwatch/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ const {CUSTOM_REPORTER_CALLBACK_TIMEOUT} = require('../src/utils/constants');
const CrashReporter = require('../src/utils/crashReporter');
const helper = require('../src/utils/helper');
const Logger = require('../src/utils/logger');
const AccessibilityAutomation = require('../src/accessibilityAutomation');

const localTunnel = new LocalTunnel();
const testObservability = new TestObservability();
const accessibilityAutomation = new AccessibilityAutomation();

const nightwatchRerun = process.env.NIGHTWATCH_RERUN_FAILED;
const nightwatchRerunFile = process.env.NIGHTWATCH_RERUN_REPORT_FILE;
Expand Down Expand Up @@ -44,7 +46,14 @@ module.exports = {
}
done(results);
},

registerEventHandlers(eventBroadcaster) {
eventBroadcaster.on('TestRunStarted', async (test) => {
await accessibilityAutomation.beforeEachExecution(test);
});
eventBroadcaster.on('TestRunFinished', async (test) => {
await accessibilityAutomation.afterEachExecution(test);
});
},
onEvent({eventName, hook_type, ...args}) {
if (typeof browser !== 'undefined' && eventName === 'TestRunStarted') {
browser.execute(`browserstack_executor: {"action": "annotate", "arguments": {"type":"Annotation","data":"ObservabilitySync:${Date.now()}","level": "debug"}}`);
Expand Down Expand Up @@ -89,6 +98,20 @@ module.exports = {
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;
accessibilityAutomation.setAccessibilityCapabilities(settings);
}
}
} catch (error) {
Logger.error(`Could not configure or launch accessibility automation - ${error}`);
}

},

async after() {
Expand All @@ -108,6 +131,25 @@ module.exports = {
await helper.deleteRerunFile();
}
}
if (helper.isAccessibilitySession()){
try {
await accessibilityAutomation.stopAccessibilityTestRun();
} catch (error) {
Logger.error(`Exception in stop accessibility test run: ${error}`);
}

}
},

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

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

beforeChildProcess(settings) {
Expand All @@ -126,5 +168,14 @@ module.exports = {
if (process.env.BROWSERSTACK_LOCAL_IDENTIFIER) {
settings.desiredCapabilities['bstack:options'].localIdentifier = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
}

try {
if (helper.isAccessibilitySession()) {
accessibilityAutomation.setAccessibilityCapabilities(settings);
}
} catch (err){
Logger.debug(`Exception while setting Accessibility Automation capabilities. Error ${err}`);
}

}
};
Loading

0 comments on commit 8937856

Please sign in to comment.