Skip to content

Commit 94cf03e

Browse files
authored
Merge pull request #327 from LambdaTest/dev
Release 31 july
2 parents 81e852d + 00172dc commit 94cf03e

File tree

7 files changed

+242
-4
lines changed

7 files changed

+242
-4
lines changed

accessibility/plugin/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const fs = require("fs");
2+
const path = require('path');
3+
const Accessibility = (on, config) => {
4+
5+
on('task', {
6+
lambdatest_log(message) {
7+
console.log(message)
8+
return null
9+
},
10+
initializeFile(filePath) {
11+
const dir = path.dirname(filePath);
12+
if (!fs.existsSync(dir)) {
13+
fs.mkdirSync(dir, { recursive: true });
14+
}
15+
if (!fs.existsSync(filePath)) {
16+
fs.writeFileSync(filePath, '[]');
17+
}
18+
return filePath;
19+
}
20+
})
21+
22+
let browser_validation = true;
23+
24+
on('before:browser:launch', (browser = {}, launchOptions) => {
25+
try {
26+
if (process.env.ACCESSIBILITY_EXTENSION_PATH !== undefined) {
27+
if (browser.name !== 'chrome') {
28+
console.log(`Accessibility Automation will run only on Chrome browsers.`);
29+
browser_validation = false;
30+
}
31+
if (browser.name === 'chrome' && browser.majorVersion <= 94) {
32+
console.log(`Accessibility Automation will run only on Chrome browser version greater than 94.`);
33+
browser_validation = false;
34+
}
35+
if (browser.isHeadless === true) {
36+
console.log(`Accessibility Automation will not run on legacy headless mode. Switch to new headless mode or avoid using headless mode.`);
37+
browser_validation = false;
38+
}
39+
if (!process.env.ACCESSIBILITY){
40+
console.log(`Accessibility Automation is disabled.`);
41+
browser_validation = false;
42+
}
43+
if (browser_validation) {
44+
45+
const accessibility_ext_path = process.env.ACCESSIBILITY_EXTENSION_PATH
46+
47+
launchOptions.args.push(`--load-extension=` + accessibility_ext_path)
48+
return launchOptions
49+
}
50+
}
51+
} catch(err) {
52+
console.log(`Error in loading Accessibility Automation extension: ${err.message}`);
53+
}
54+
55+
})
56+
config.env.WCAG_CRITERIA= process.env.WCAG_CRITERIA;
57+
config.env.BEST_PRACTICE= process.env.BEST_PRACTICE;
58+
config.env.NEEDS_REVIEW= process.env.NEEDS_REVIEW;
59+
config.env.ACCESSIBILITY_REPORT_PATH = process.env.ACCESSIBILITY_REPORT_PATH;
60+
config.env.ACCESSIBILITY = process.env.ACCESSIBILITY;
61+
console.log(`parameter for accessibility report ACCESSIBILITY - ` + config.env.ACCESSIBILITY)
62+
console.log(`parameter for accessibility report WCAG_CRITERIA - ` + config.env.WCAG_CRITERIA)
63+
console.log(`parameter for accessibility report BEST_PRACTICE -` + config.env.BEST_PRACTICE)
64+
console.log(`parameter for accessibility report NEEDS_REVIEW -` + config.env.NEEDS_REVIEW)
65+
console.log(`parameter for accessibility report ACCESSIBILITY_REPORT_PATH -` + config.env.ACCESSIBILITY_REPORT_PATH)
66+
67+
68+
return config;
69+
}
70+
71+
module.exports = Accessibility;

accessibility/scanner/index.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const fs = require("fs")
2+
3+
const LambdatestLog = (message) => {
4+
if (!Cypress.env('LAMBDATEST_LOGS')) return;
5+
cy.task('lambdatest_log', message);
6+
}
7+
8+
const commandsToWrap = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
9+
10+
const setScanConfig = (win, payload) =>
11+
new Promise(async (resolve, reject) => {
12+
const isHttpOrHttps = /^(http|https):$/.test(win.location.protocol);
13+
if (!isHttpOrHttps) {
14+
resolve();
15+
}
16+
17+
function startScan() {
18+
console.log('log', "Accessibility setting scan config")
19+
function onScanComplete(event) {
20+
win.document.removeEventListener("automation-custom-event", onScanComplete);
21+
console.log('log', "Recieved scan config data " + event.detail)
22+
resolve(event.detail);
23+
}
24+
25+
win.document.addEventListener("automation-custom-event", onScanComplete);
26+
const e = new CustomEvent("accessibility-extension-custom-event", { detail: payload });
27+
win.document.dispatchEvent(e);
28+
29+
30+
setTimeout(() => {
31+
resolve(new Error('automation-custom-event not received within timeout'));
32+
}, 45000);
33+
}
34+
startScan();
35+
36+
})
37+
38+
const getScanData = (win, payload) =>
39+
new Promise( async (resolve,reject) => {
40+
const isHttpOrHttps = /^(http|https):$/.test(window.location.protocol);
41+
if (!isHttpOrHttps) {
42+
resolve();
43+
}
44+
45+
46+
function getSummary() {
47+
function onReceiveSummary(event) {
48+
49+
win.document.removeEventListener("automation-custom-event", onReceiveSummary);
50+
resolve(event.detail);
51+
}
52+
53+
54+
win.document.addEventListener("automation-custom-event", onReceiveSummary);
55+
const e = new CustomEvent("accessibility-extension-custom-event", { detail: payload });
56+
win.document.dispatchEvent(e);
57+
58+
setTimeout(() => {
59+
resolve(new Error('automation-custom-event not received within timeout'));
60+
}, 45000);
61+
62+
}
63+
64+
65+
getSummary();
66+
67+
})
68+
69+
Cypress.on('command:start', async (command) => {
70+
if(!command || !command.attributes) return;
71+
if(command.attributes.name == 'window' || command.attributes.name == 'then' || command.attributes.name == 'wrap' || command.attributes.name == 'wait') {
72+
return;
73+
}
74+
75+
if (!commandsToWrap.includes(command.attributes.name)) return;
76+
let isAccessibilityLoaded = Cypress.env("ACCESSIBILITY") || false;
77+
if (!isAccessibilityLoaded){
78+
console.log('log', "accessibility not enabled " + isAccessibilityLoaded);
79+
return;
80+
}
81+
82+
83+
console.log('log', "debugging scan form command " + command.attributes.name);
84+
cy.window().then((win) => {
85+
let wcagCriteriaValue = Cypress.env("WCAG_CRITERIA") || "wcag21a";
86+
let bestPracticeValue = Cypress.env("BEST_PRACTICE") || false;
87+
let needsReviewValue = Cypress.env("NEEDS_REVIEW") || true;
88+
89+
const payloadToSend = {
90+
message: 'SET_CONFIG',
91+
wcagCriteria: wcagCriteriaValue,
92+
bestPractice: bestPracticeValue,
93+
needsReview: needsReviewValue
94+
}
95+
let testId = Cypress.env("TEST_ID") || ""
96+
97+
const filePath = Cypress.env("ACCESSIBILITY_REPORT_PATH") || 'cypress/results/accessibilityReport_' + testId + '.json';
98+
99+
cy.wrap(setScanConfig(win, payloadToSend), {timeout: 30000}).then((res) => {
100+
console.log('logging config reponse', res);
101+
102+
const payload = {
103+
message: 'GET_LATEST_SCAN_DATA',
104+
}
105+
106+
cy.wrap(getScanData(win, payload), {timeout: 45000}).then((res) => {
107+
LambdatestLog('log', "scanning data ");
108+
109+
110+
cy.task('initializeFile', filePath).then((filePath) => {
111+
cy.readFile(filePath, { log: true, timeout: 45000 }).then((fileContent) => {
112+
let resultsArray = [{}];
113+
console.log('logging report', res);
114+
// If the file is not empty, parse the existing content
115+
if (fileContent) {
116+
try {
117+
resultsArray = JSON.parse(JSON.stringify(fileContent));
118+
} catch (e) {
119+
console.log("parsing error for content " , fileContent)
120+
console.log('Error parsing JSON file:', e);
121+
return;
122+
}
123+
}
124+
console.log('scanned data recieved is', res.message);
125+
if (res.message == "GET_LATEST_SCAN_DATA") {
126+
// Append the new result
127+
resultsArray.push(res);
128+
console.log('resultsarray logging', resultsArray);
129+
}
130+
131+
// Write the updated content back to the file
132+
cy.writeFile(filePath, resultsArray, { log: true, timeout: 45000 });
133+
});
134+
});
135+
});
136+
137+
});
138+
})
139+
})
140+
141+
142+
Cypress.on('command:end', (command) => {
143+
144+
return;
145+
})

commands/utils/set_args.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ function sync_args_from_cmd(args) {
383383
lt_config["run_settings"]["useNode18"] = false;
384384
}
385385

386+
if ("accessibility" in args) {
387+
if (args["accessibility"] == "true") {
388+
lt_config.run_settings.accessibility = true;
389+
} else {
390+
lt_config.run_settings.accessibility = false;
391+
}
392+
} else if (lt_config["run_settings"]["accessibility"] && !lt_config["run_settings"]["accessibility"]) {
393+
lt_config["run_settings"]["accessibility"] = false;
394+
}
395+
386396
if ("network_ws" in args) {
387397
if (args["network_ws"] == "true") {
388398
lt_config.run_settings.network_ws = true;

commands/utils/validate.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,14 @@ module.exports = validate_config = function (lt_config, validation_configs) {
198198
}
199199
}
200200

201-
//validate if network_http2 field contains expected value
201+
//validate if accessibility field contains expected value
202+
if ("accessibility" in lt_config["run_settings"]) {
203+
if (![true, false].includes(lt_config["run_settings"]["accessibility"])) {
204+
reject("Error!! boolean value is expected in accessibility key");
205+
}
206+
}
207+
208+
//validate if useNode18 field contains expected value
202209
if ("useNode18" in lt_config["run_settings"]) {
203210
if (![true, false].includes(lt_config["run_settings"]["useNode18"])) {
204211
reject("Error!! boolean value is expected in useNode18 key");

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ const argv = require("yargs")
245245
alias: "network_sse",
246246
describe: "Bypass sse events calls for Network logs",
247247
type: "bool",
248+
})
249+
.option("cypress_accessibility", {
250+
alias: "accessibility",
251+
describe: "enable accessibility testing for cypress.",
252+
type: "bool",
248253
});
249254
},
250255
function (argv) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdatest-cypress-cli",
3-
"version": "3.0.29",
3+
"version": "3.0.30",
44
"description": "The lambdatest-cypress-cli is LambdaTest's command-line interface (CLI) aimed to help you run your Cypress tests on LambdaTest platform.",
55
"homepage": "https://github.com/LambdaTest/lambdatest-cypress-cli",
66
"author": "LambdaTest <keys@lambdatest.com>",

0 commit comments

Comments
 (0)