Skip to content

Commit ccde61e

Browse files
committed
SDK-1884: improved logic
1 parent e8d4bc9 commit ccde61e

File tree

1 file changed

+63
-11
lines changed
  • bin/accessibility-automation/cypress

1 file changed

+63
-11
lines changed

bin/accessibility-automation/cypress/index.js

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,44 @@ const browserStackLog = (message) => {
77

88
const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
99
const commandToOverwrite = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
10-
commandToOverwrite.forEach((command) => {
11-
Cypress.Commands.overwrite(command, (originalFn, url, options) => {
12-
const attributes = Cypress.mocha.getRunner().suite.ctx.currentTest || Cypress.mocha.getRunner().suite.ctx._runnable;
13-
let shouldScanTestForAccessibility = shouldScanForAccessibility(attributes);
14-
if (!shouldScanTestForAccessibility) {
15-
cy.wrap(null).then(() => originalFn(url, options));
16-
return;
17-
}
18-
else cy.wrap(null).performScan().then(() => originalFn(url, options));
19-
});
20-
});
10+
const performModifiedScan = (originalFn, Subject, stateType, ...args) => {
11+
let customChaining = cy.wrap(null).performScan();
12+
function changeSub(args, stateType, newSubject) {
13+
if (stateType !== 'parent') {
14+
return [newSubject, ...args.slice(1)];
15+
}
16+
return args;
17+
}
18+
function runCutomizedCommand() {
19+
if (!Subject) {
20+
let orgS1, orgS2, cypressCommandSubject;
21+
if((orgS2 = (orgS1 = cy).subject) !==null && orgS2 !== void 0){
22+
cypressCommandSubject = orgS2.call(orgS1);
23+
}
24+
else{
25+
cypressCommandSubject = null;
26+
}
27+
customChaining.then(()=> cypressCommandSubject).then(() => {originalFn(...args)});
28+
}
29+
else {
30+
let orgSC1, orgSC2, timeO1, cypressCommandChain, setTimeout;
31+
if((timeO1 = args.find(arg => arg !== null && arg !== void 0 ? arg.timeout : null)) !== null && timeO1 !== void 0) {
32+
setTimeout = timeO1.timeout;
33+
}
34+
else {
35+
setTimeout = null;
36+
}
37+
if((orgSC1 = (orgSC2 = cy).subjectChain) !== null && orgSC1 !== void 0){
38+
cypressCommandChain = orgSC1.call(orgSC2);
39+
}
40+
else {
41+
cypressCommandChain = null;
42+
}
43+
customChaining.performScanSubjectQuery(cypressCommandChain, setTimeout).then({timeout: 30000}, (newSubject) => originalFn(...changeSub(args, stateType, newSubject)));
44+
}
45+
}
46+
runCutomizedCommand();
47+
}
2148

2249
const performScan = (win, payloadToSend) =>
2350
new Promise(async (resolve, reject) => {
@@ -295,6 +322,26 @@ const shouldScanForAccessibility = (attributes) => {
295322
return shouldScanTestForAccessibility;
296323
}
297324

325+
commandToOverwrite.forEach((command) => {
326+
Cypress.Commands.overwrite(command, (originalFn, ...args) => {
327+
const attributes = Cypress.mocha.getRunner().suite.ctx.currentTest || Cypress.mocha.getRunner().suite.ctx._runnable;
328+
const shouldScanTestForAccessibility = shouldScanForAccessibility(attributes);
329+
const state = cy.state('current'), Subject = 'getSubjectFromChain' in cy;
330+
const stateName = state === null || state === void 0 ? void 0 : state.get('name');
331+
let stateType;
332+
if (!shouldScanTestForAccessibility || (stateName && stateName !== command)) {
333+
return originalFn(...args);
334+
}
335+
if(state !== null && state !== void 0){
336+
stateType = state.get('type');
337+
}
338+
else {
339+
stateType = null;
340+
}
341+
performModifiedScan(originalFn, Subject, stateType, ...args);
342+
});
343+
});
344+
298345
afterEach(() => {
299346
const attributes = Cypress.mocha.getRunner().suite.ctx.currentTest;
300347
cy.window().then(async (win) => {
@@ -400,3 +447,8 @@ Cypress.Commands.add('getAccessibilityResults', () => {
400447
browserStackLog(`Error in getting accessibilty results with error: ${error.message}`);
401448
}
402449
});
450+
451+
Cypress.Commands.addQuery('performScanSubjectQuery', function (chaining, setTimeout) {
452+
this.set('timeout', setTimeout);
453+
return () => cy.getSubjectFromChain(chaining);
454+
});

0 commit comments

Comments
 (0)