Skip to content

Commit

Permalink
SDK-1884: resolved reviews.
Browse files Browse the repository at this point in the history
  • Loading branch information
osho-20 committed Dec 5, 2024
1 parent ccde61e commit f96066a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 74 deletions.
107 changes: 38 additions & 69 deletions bin/accessibility-automation/cypress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,30 @@ const browserStackLog = (message) => {
}

const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
// scroll is not a default function in cypress.
const commandToOverwrite = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];

/*
Overrriding the cypress commands to perform Accessibility Scan before Each command
- runCutomizedCommand is handling both the cases of subject available in cypress original command
and chaning available from original cypress command.
*/
const performModifiedScan = (originalFn, Subject, stateType, ...args) => {
let customChaining = cy.wrap(null).performScan();
function changeSub(args, stateType, newSubject) {
const changeSub = (args, stateType, newSubject) => {
if (stateType !== 'parent') {
return [newSubject, ...args.slice(1)];
}
return args;
}
function runCutomizedCommand() {
const runCutomizedCommand = () => {
if (!Subject) {
let orgS1, orgS2, cypressCommandSubject;
if((orgS2 = (orgS1 = cy).subject) !==null && orgS2 !== void 0){
cypressCommandSubject = orgS2.call(orgS1);
}
else{
cypressCommandSubject = null;
}
customChaining.then(()=> cypressCommandSubject).then(() => {originalFn(...args)});
}
else {
let orgSC1, orgSC2, timeO1, cypressCommandChain, setTimeout;
if((timeO1 = args.find(arg => arg !== null && arg !== void 0 ? arg.timeout : null)) !== null && timeO1 !== void 0) {
setTimeout = timeO1.timeout;
}
else {
setTimeout = null;
}
if((orgSC1 = (orgSC2 = cy).subjectChain) !== null && orgSC1 !== void 0){
cypressCommandChain = orgSC1.call(orgSC2);
}
else {
cypressCommandChain = null;
}
customChaining.performScanSubjectQuery(cypressCommandChain, setTimeout).then({timeout: 30000}, (newSubject) => originalFn(...changeSub(args, stateType, newSubject)));
let cypressCommandSubject = (cy.subject?.call(cy)) ?? null;
customChaining.then(() => cypressCommandSubject).then(() => { originalFn(...args); });
} else {
let setTimeout = args.find(arg => arg?.timeout)?.timeout ?? null;
let cypressCommandChain = (cy.subjectChain?.call(cy)) ?? null;
customChaining.performScanSubjectQuery(cypressCommandChain, setTimeout).then({ timeout: 10000 }, newSubject => originalFn(...changeSub(args, stateType, newSubject)));
}
}
runCutomizedCommand();
Expand All @@ -50,8 +39,7 @@ const performScan = (win, payloadToSend) =>
new Promise(async (resolve, reject) => {
const isHttpOrHttps = /^(http|https):$/.test(win.location.protocol);
if (!isHttpOrHttps) {
resolve();
return;
return resolve();
}

function findAccessibilityAutomationElement() {
Expand All @@ -64,16 +52,14 @@ new Promise(async (resolve, reject) => {
const intervalID = setInterval(async () => {
if (count > retryCount) {
clearInterval(intervalID);
reject(
return reject(
new Error(
"Accessibility Automation Scanner is not ready on the page."
)
);
return;
} else if (findAccessibilityAutomationElement()) {
clearInterval(intervalID);
resolve("Scanner set");
return;
return resolve("Scanner set");
} else {
count += 1;
}
Expand All @@ -84,8 +70,7 @@ new Promise(async (resolve, reject) => {
function startScan() {
function onScanComplete() {
win.removeEventListener("A11Y_SCAN_FINISHED", onScanComplete);
resolve();
return;
return resolve();
}

win.addEventListener("A11Y_SCAN_FINISHED", onScanComplete);
Expand All @@ -99,8 +84,7 @@ new Promise(async (resolve, reject) => {
waitForScannerReadiness()
.then(startScan)
.catch(async (err) => {
resolve("Scanner is not ready on the page after multiple retries. performscan");
return;
return resolve("Scanner is not ready on the page after multiple retries. performscan");
});
}
})
Expand All @@ -109,8 +93,7 @@ const getAccessibilityResultsSummary = (win) =>
new Promise((resolve) => {
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
if (!isHttpOrHttps) {
resolve();
return;
return resolve();
}

function findAccessibilityAutomationElement() {
Expand All @@ -123,16 +106,14 @@ new Promise((resolve) => {
const intervalID = setInterval(() => {
if (count > retryCount) {
clearInterval(intervalID);
reject(
return reject(
new Error(
"Accessibility Automation Scanner is not ready on the page."
)
);
return;
} else if (findAccessibilityAutomationElement()) {
clearInterval(intervalID);
resolve("Scanner set");
return;
return resolve("Scanner set");
} else {
count += 1;
}
Expand All @@ -143,8 +124,7 @@ new Promise((resolve) => {
function getSummary() {
function onReceiveSummary(event) {
win.removeEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
resolve(event.detail);
return;
return resolve(event.detail);
}

win.addEventListener("A11Y_RESULTS_SUMMARY", onReceiveSummary);
Expand All @@ -158,8 +138,7 @@ new Promise((resolve) => {
waitForScannerReadiness()
.then(getSummary)
.catch((err) => {
resolve();
return;
return resolve();
});
}
})
Expand All @@ -168,8 +147,7 @@ const getAccessibilityResults = (win) =>
new Promise((resolve) => {
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
if (!isHttpOrHttps) {
resolve();
return;
return resolve();
}

function findAccessibilityAutomationElement() {
Expand All @@ -182,16 +160,14 @@ new Promise((resolve) => {
const intervalID = setInterval(() => {
if (count > retryCount) {
clearInterval(intervalID);
reject(
return reject(
new Error(
"Accessibility Automation Scanner is not ready on the page."
)
);
return;
} else if (findAccessibilityAutomationElement()) {
clearInterval(intervalID);
resolve("Scanner set");
return;
return resolve("Scanner set");
} else {
count += 1;
}
Expand All @@ -202,8 +178,7 @@ new Promise((resolve) => {
function getResults() {
function onReceivedResult(event) {
win.removeEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
resolve(event.detail);
return;
return resolve(event.detail);
}

win.addEventListener("A11Y_RESULTS_RESPONSE", onReceivedResult);
Expand All @@ -217,8 +192,7 @@ new Promise((resolve) => {
waitForScannerReadiness()
.then(getResults)
.catch((err) => {
resolve();
return;
return resolve();
});
}
});
Expand All @@ -242,16 +216,14 @@ new Promise( (resolve, reject) => {
const intervalID = setInterval(async () => {
if (count > retryCount) {
clearInterval(intervalID);
reject(
return reject(
new Error(
"Accessibility Automation Scanner is not ready on the page."
)
);
return;
} else if (findAccessibilityAutomationElement()) {
clearInterval(intervalID);
resolve("Scanner set");
return;
return resolve("Scanner set");
} else {
count += 1;
}
Expand All @@ -261,8 +233,7 @@ new Promise( (resolve, reject) => {

function saveResults() {
function onResultsSaved(event) {
resolve();
return;
return resolve();
}
win.addEventListener("A11Y_RESULTS_SAVED", onResultsSaved);
const e = new CustomEvent("A11Y_SAVE_RESULTS", {
Expand All @@ -277,14 +248,12 @@ new Promise( (resolve, reject) => {
waitForScannerReadiness()
.then(saveResults)
.catch(async (err) => {
resolve("Scanner is not ready on the page after multiple retries. after run");
return;
return resolve("Scanner is not ready on the page after multiple retries. after run");
});
}
} catch(error) {
browserStackLog(`Error in saving results with error: ${error.message}`);
resolve();
return;
return resolve();
}

})
Expand Down Expand Up @@ -348,7 +317,7 @@ afterEach(() => {
let shouldScanTestForAccessibility = shouldScanForAccessibility(attributes);
if (!shouldScanTestForAccessibility) return cy.wrap({});

cy.wrap(performScan(win), {timeout: 30000}).then(() => {
cy.wrap(performScan(win), {timeout: 10000}).then(() => {
try {
let os_data;
if (Cypress.env("OS")) {
Expand Down Expand Up @@ -379,7 +348,7 @@ afterEach(() => {
}
};
browserStackLog(`Saving accessibility test results`);
cy.wrap(saveTestResults(win, payloadToSend), {timeout: 30000}).then(() => {
cy.wrap(saveTestResults(win, payloadToSend), {timeout: 10000}).then(() => {
browserStackLog(`Saved accessibility test results`);
})

Expand All @@ -400,7 +369,7 @@ Cypress.Commands.add('performScan', () => {
}
cy.window().then(async (win) => {
browserStackLog(`Performing accessibility scan`);
cy.wrap(performScan(win), {timeout:40000});
cy.wrap(performScan(win), {timeout:10000});
});
} catch(error) {
browserStackLog(`Error in performing scan with error: ${error.message}`);
Expand Down
2 changes: 1 addition & 1 deletion bin/accessibility-automation/plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const browserstackAccessibility = (on, config) => {
}
if (browser_validation) {
const ally_path = path.dirname(process.env.ACCESSIBILITY_EXTENSION_PATH)
const {_, payload} = decodeJWTToken(process.env.ACCESSIBILITY_AUTH);
const payload = decodeJWTToken(process.env.ACCESSIBILITY_AUTH);
launchOptions.extensions.push(ally_path);
if(!utils.isUndefined(payload) && !utils.isUndefined(payload.a11y_core_config) && payload.a11y_core_config.domForge === true) {
launchOptions.args.push("--auto-open-devtools-for-tabs");
Expand Down
7 changes: 3 additions & 4 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1788,11 +1788,10 @@ exports.decodeJWTToken = (token) => {
if (parts.length < 2) {
throw new Error('Invalid JWT token');
}
const header = JSON.parse(base64UrlDecode(parts[0]));
const payload = JSON.parse(base64UrlDecode(parts[1]));
return { header, payload };
return payload
} catch (error) {
logger.err(error.message);
return {undefined, undefined};
logger.err("Error in token decoding with error:", error.message);
return undefined;
}
}

0 comments on commit f96066a

Please sign in to comment.