-
Notifications
You must be signed in to change notification settings - Fork 51
Chore: Update the Cypress JS example with custom threshold validation… #587
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
Draft
ash007ok
wants to merge
1
commit into
GovTechSG:master
Choose a base branch
from
ash007ok:chore/update_cypress_js_example_with_threshold_sc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 22 additions & 14 deletions
36
examples/oobee-cypress-integration-js/cypress/e2e/spec.cy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,27 @@ | ||
| describe("template spec", () => { | ||
| it("should run oobee A11y", () => { | ||
| cy.visit( | ||
| "https://govtechsg.github.io/purple-banner-embeds/purple-integrated-scan-example.htm" | ||
| ); | ||
| describe('template spec', () => { | ||
| beforeEach(() => { | ||
| cy.visit('https://govtechsg.github.io/purple-banner-embeds/purple-integrated-scan-example.htm'); | ||
| cy.injectOobeeA11yScripts(); | ||
| cy.runOobeeA11yScan(); | ||
|
|
||
| cy.get("button[onclick=\"toggleSecondSection()\"]").click(); | ||
| // Run a scan on <input> and <button> elements | ||
| cy.runOobeeA11yScan({ | ||
| elementsToScan: ["input", "button"], | ||
| elementsToClick: ["button[onclick=\"toggleSecondSection()\"]"], | ||
| metadata: "Clicked button" | ||
| }); | ||
| }); | ||
|
|
||
| after(() => { | ||
| cy.terminateOobeeA11y(); | ||
| }); | ||
|
|
||
| it('should not have WCAG violations in first section', () => { | ||
| cy.runOobeeA11yScan({}, { mustFix: 10 }); | ||
| }); | ||
|
|
||
| it('should not have WCAG violations in second section', () => { | ||
| cy.get('button[onclick="toggleSecondSection()"]').click(); | ||
| // Run a scan on <input> and <button> elements | ||
| cy.runOobeeA11yScan( | ||
| { | ||
| elementsToScan: ['input', 'button'], | ||
| elementsToClick: ['button[onclick="toggleSecondSection()"]'], | ||
| metadata: 'Clicked button', | ||
| }, | ||
| { mustFix: 1 }, | ||
| ); | ||
| }); | ||
| }); |
214 changes: 155 additions & 59 deletions
214
examples/oobee-cypress-integration-js/cypress/support/e2e.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,169 @@ | ||
| Cypress.Commands.add("injectOobeeA11yScripts", () => { | ||
| cy.task("getAxeScript").then((s) => { | ||
| cy.window().then((win) => { | ||
| try { | ||
| win.eval(s); | ||
| } | ||
| catch (error) { | ||
| // If eval fails due to cross-origin issues, try alternative injection | ||
| if (error.message.includes('SecurityError') || error.message.includes('cross-origin')) { | ||
| cy.log('Cross-origin error detected, attempting alternative script injection'); | ||
| // Create a script tag as fallback | ||
| const script = win.document.createElement('script'); | ||
| script.textContent = s; | ||
| win.document.head.appendChild(script); | ||
| } | ||
| else { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| import 'cypress-if'; | ||
|
|
||
| Cypress.Commands.add('injectOobeeA11yScripts', () => { | ||
| cy.task('getAxeScript').then(s => { | ||
| cy.window().then(win => { | ||
| try { | ||
| win.eval(s); | ||
| } catch (error) { | ||
| // If eval fails due to cross-origin issues, try alternative injection | ||
| if (error.message.includes('SecurityError') || error.message.includes('cross-origin')) { | ||
| cy.log('Cross-origin error detected, attempting alternative script injection'); | ||
| // Create a script tag as fallback | ||
| const script = win.document.createElement('script'); | ||
| script.textContent = s; | ||
| win.document.head.appendChild(script); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| cy.task("getOobeeA11yScripts").then((s) => { | ||
| cy.window().then((win) => { | ||
| try { | ||
| win.eval(s); | ||
| } | ||
| catch (error) { | ||
| // If eval fails due to cross-origin issues, try alternative injection | ||
| if (error.message.includes('SecurityError') || error.message.includes('cross-origin')) { | ||
| cy.log('Cross-origin error detected, attempting alternative script injection'); | ||
| // Create a script tag as fallback | ||
| const script = win.document.createElement('script'); | ||
| script.textContent = s; | ||
| win.document.head.appendChild(script); | ||
| } | ||
| else { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| cy.task('getOobeeA11yScripts').then(s => { | ||
| cy.window().then(win => { | ||
| try { | ||
| win.eval(s); | ||
| } catch (error) { | ||
| // If eval fails due to cross-origin issues, try alternative injection | ||
| if (error.message.includes('SecurityError') || error.message.includes('cross-origin')) { | ||
| cy.log('Cross-origin error detected, attempting alternative script injection'); | ||
| // Create a script tag as fallback | ||
| const script = win.document.createElement('script'); | ||
| script.textContent = s; | ||
| win.document.head.appendChild(script); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("runOobeeA11yScan", (items = {}) => { | ||
| cy.window().then(async (win) => { | ||
| Cypress.Commands.add('runOobeeA11yScan', (items = {}, threshold = {}) => { | ||
| cy.window().then(async win => { | ||
| const { elementsToScan, elementsToClick, metadata } = items; | ||
|
|
||
| // extract text from the page for readability grading | ||
| const sentences = win.extractText(); | ||
| // run readability grading separately as it cannot be done within the browser context | ||
| cy.task("gradeReadability", sentences).then( | ||
| async (gradingReadabilityFlag) => { | ||
| // passing the grading flag to runA11yScan to inject violation as needed | ||
| const res = await win.runA11yScan( | ||
| elementsToScan, | ||
| gradingReadabilityFlag, | ||
| ); | ||
| cy.task("pushOobeeA11yScanResults", { | ||
| res, | ||
| metadata, | ||
| elementsToClick, | ||
| }).then((count) => { | ||
| return count; | ||
| cy.task('gradeReadability', sentences).then(async gradingReadabilityFlag => { | ||
| // passing the grading flag to runA11yScan to inject violation as needed | ||
| const res = await win.runA11yScan(elementsToScan, gradingReadabilityFlag); | ||
|
|
||
| const takeOobeeScreenshotsFromCypressForMustFix = | ||
| Cypress.env('takeOobeeScreenshotsFromCypressForMustFix') || true; | ||
| const takeOobeeScreenshotsFromCypressForGoodToFix = | ||
| Cypress.env('takeOobeeScreenshotsFromCypressForGoodToFix') || true; | ||
|
|
||
| let shouldTakeScreenshot; | ||
| let oobeeReportPath; | ||
|
|
||
| // take screenshot and move to report dir | ||
| cy.wrap() | ||
| .then(() => { | ||
| cy.task('returnOobeeRandomTokenAndPage').then(({ randomToken }) => { | ||
| oobeeReportPath = randomToken; | ||
| }); | ||
| }) | ||
| .then(() => { | ||
| // Take screenshots based on flags and violation severity | ||
| if ( | ||
| (takeOobeeScreenshotsFromCypressForMustFix || | ||
| takeOobeeScreenshotsFromCypressForGoodToFix) && | ||
| res.axeScanResults.violations.length > 0 | ||
| ) { | ||
| const violations = res.axeScanResults.violations; | ||
| violations.forEach(violation => { | ||
| violation.nodes.forEach((node, nodeIndex) => { | ||
| const selector = node.target && node.target[0]; | ||
| const timestamp = Date.now() * 1000000 + Math.floor(Math.random() * 1000000); // Epoch time in nanoseconds | ||
| const screenshotFileName = `${node.impact}_${violation.id}_node_${nodeIndex}_${timestamp}`; | ||
| const screenshotPath = `elemScreenshots/html/${screenshotFileName}`; | ||
| const fullScreenshotPath = `${oobeeReportPath}/${screenshotPath}`; | ||
|
|
||
| if (selector) { | ||
| // Determine if we should take screenshot based on impact level | ||
| shouldTakeScreenshot = | ||
| (takeOobeeScreenshotsFromCypressForMustFix && | ||
| (node.impact === 'critical' || node.impact === 'serious')) || | ||
| (takeOobeeScreenshotsFromCypressForGoodToFix && | ||
| (node.impact === 'moderate' || node.impact === 'minor')); | ||
|
|
||
| if (shouldTakeScreenshot) { | ||
| takeScreenshotForHTMLElements(fullScreenshotPath, selector); | ||
| node.screenshotPath = screenshotPath + '.png'; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| }) | ||
| .then(() => { | ||
| // move screenshots to report dir | ||
| if ( | ||
| takeOobeeScreenshotsFromCypressForMustFix || | ||
| takeOobeeScreenshotsFromCypressForGoodToFix | ||
| ) { | ||
| cy.task('returnOobeeRandomTokenAndPage').then(({ randomToken }) => { | ||
| // const screenshotDir = `cypress/screenshots/${randomToken}/elemScreenshots/html`; | ||
| const screenshotPattern = `cypress/screenshots/**/elemScreenshots/html/*.png`; | ||
| const toReportDir = `results/${randomToken}/elemScreenshots/html`; | ||
| cy.task('copyFiles', { | ||
| fromPattern: screenshotPattern, | ||
| toDir: toReportDir, | ||
| }); | ||
| }); | ||
| } | ||
| }) | ||
| .then(() => { | ||
| cy.task('pushOobeeA11yScanResults', { | ||
| res, | ||
| metadata, | ||
| elementsToClick, | ||
| }).then(count => { | ||
| // validate the count against the thresholds | ||
| handleViolation(count, threshold); | ||
| return count; | ||
| }); | ||
| }); | ||
| }, | ||
| ); | ||
| cy.task("finishOobeeA11yTestCase"); // test the accumulated number of issue occurrences against specified thresholds. If exceed, terminate oobeeA11y instance. | ||
| }); | ||
| cy.task('finishOobeeA11yTestCase'); // test the accumulated number of issue occurrences against specified thresholds. If exceed, terminate oobeeA11y instance. | ||
| }); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("terminateOobeeA11y", () => { | ||
| cy.task("terminateOobeeA11y"); | ||
| Cypress.Commands.add('terminateOobeeA11y', () => { | ||
| cy.task('terminateOobeeA11y'); | ||
| }); | ||
|
|
||
| const handleViolation = (scanResults = {}, threshold = {}) => { | ||
| const assertIfConfigured = key => { | ||
| if (threshold && typeof threshold[key] === 'number') { | ||
| const actual = Number(scanResults[key] ?? 0); | ||
| const limit = Number(threshold[key]); | ||
| expect( | ||
| actual, | ||
| `The value of '${key}' (${actual}) should be less than or equal to the threshold (${limit}).`, | ||
| ).to.be.at.most(limit); | ||
| } | ||
| }; | ||
| assertIfConfigured('mustFix'); | ||
| assertIfConfigured('goodToFix'); | ||
| }; | ||
|
|
||
| const takeScreenshotForHTMLElements = (screenshotPath, selector) => { | ||
| try { | ||
| cy.get(selector) | ||
| .if() | ||
| .then(el => { | ||
| cy.wrap(el).first().invoke('css', 'border', '3px solid red'); // Highlight element with red border | ||
| cy.wrap(el).first().parent(); | ||
| cy.wrap(el).first().screenshot(screenshotPath, { | ||
| overwrite: true, | ||
| capture: 'viewport', | ||
| }); | ||
| cy.wrap(el).first().invoke('css', 'border', 'none'); // Remove highlight after screenshot | ||
| }); | ||
| } catch (e) { | ||
| cy.log('Error taking screenshot for element', selector, e); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ash007ok with your Cypress experience, do you think it is possible to make Oobee functions like these exposed as cypress keywords instead?
E.g. Can we have an equivalent of or extend
npmIndexfor Cypress? Then users don't have to copy/paste the examples.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add on, the screenshots keywords can be for Playwright too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@younglim That would be great! We can create a Cypress plugin like
cypress-oobeeto eliminate the copy/paste setup.Guide to create plugins: https://docs.cypress.io/guides/tooling/plugins-guide
Plugin examples: https://docs.cypress.io/app/plugins/plugins-list