Skip to content

refactor the code #393

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

Merged
merged 1 commit into from
Mar 20, 2025
Merged
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
78 changes: 32 additions & 46 deletions accessibility/scanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,39 @@ const commandsToOverride = [
const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];

const performModifiedScan = (originalFn, Subject, stateType, ...args) => {
// Initialize custom chaining
const customChaining = cy.wrap(null).performScan();

// Function to modify arguments based on stateType
const updateArgs = (args, stateType, newSubject) => {
return stateType !== 'parent' ? [newSubject, ...args.slice(1)] : args;
};

// Extract timeout from args if present
const getTimeout = (args) => {
const timeoutArg = args.find(arg => arg && arg.timeout !== undefined);
return timeoutArg?.timeout;
};

// Get the current Cypress subject
const getCurrentSubject = () => {
return cy.subject?.() || null;
};

// Get the current Cypress subject chain
const getSubjectChain = () => {
return cy.subjectChain?.() || null;
};

// Execute the command pipeline
if (!Subject) {
// Handle case without Subject
const cypressCommandSubject = getCurrentSubject();

customChaining
.then(() => cypressCommandSubject)
.then(() => {
originalFn(...args);
});
} else {
// Handle case with Subject
const cypressCommandChain = getSubjectChain();
const timeout = getTimeout(args);

customChaining
.performScanSubjectQuery(cypressCommandChain, timeout)
.then({ timeout: 30000 }, (newSubject) => {
originalFn(...updateArgs(args, stateType, newSubject));
});
}
};
let customChaining = cy.wrap(null).performScan();
const updateSubj = (args, stateType, newSubject) =>
stateType === 'parent' ? args : [newSubject, ...args.slice(1)];

const runCustomizedChainingCommand = () => {
if (!Subject) {
let cypressCommandSubject = null;
const subjectFn = cy && cy.subject;
if (subjectFn !== null && subjectFn !== void 0) {
cypressCommandSubject = subjectFn.call(cy);
}

customChaining
.then(() => cypressCommandSubject)
.then(() => {
originalFn(...args);
});
} else {
let cypressCommandChain = null, setTimeout = null;
// Extract timeout value if present
const timeoutArg = args.find(arg => arg !== null && arg !== void 0 ? arg.timeout : null);
if (timeoutArg !== null && timeoutArg !== void 0) {
setTimeout = timeoutArg.timeout;
}
const subjectChainFn = cy && cy.subjectChain;
if (subjectChainFn !== null && subjectChainFn !== void 0) {
cypressCommandChain = subjectChainFn.call(cy);
}
customChaining.performScanSubjectQuery(cypressCommandChain, setTimeout).then({timeout: 30000}, (newSubject) => originalFn(...updateSubj(args, stateType, newSubject)));
}
}
runCustomizedChainingCommand();
}

Cypress.Commands.add('processAccessibilityReport', () => {
try {
Expand Down