Skip to content

Commit 58aa803

Browse files
committed
review changes - try catch null handling, minor refactor
1 parent bea2579 commit 58aa803

File tree

7 files changed

+247
-263
lines changed

7 files changed

+247
-263
lines changed
Lines changed: 115 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,136 @@
11
/* Event listeners + custom commands for Cypress */
22

33
Cypress.on('test:before:run', () => {
4-
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return
5-
const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH")
6-
7-
if (extensionPath !== undefined) {
8-
new Promise((resolve, reject) => {
9-
window.parent.addEventListener('A11Y_TAP_STARTED', () => {
10-
resolve("A11Y_TAP_STARTED");
11-
});
12-
const e = new CustomEvent('A11Y_FORCE_START');
13-
window.parent.dispatchEvent(e);
14-
})
15-
}
16-
})
4+
try {
5+
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return
6+
const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH")
7+
8+
if (extensionPath !== undefined) {
9+
new Promise((resolve, reject) => {
10+
window.parent.addEventListener('A11Y_TAP_STARTED', () => {
11+
resolve("A11Y_TAP_STARTED");
12+
});
13+
const e = new CustomEvent('A11Y_FORCE_START');
14+
window.parent.dispatchEvent(e);
15+
})
16+
}
17+
} catch {}
18+
19+
});
1720

1821
Cypress.on('test:after:run', (attributes, runnable) => {
19-
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return
20-
const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH")
21-
const isHeaded = Cypress.browser.isHeaded;
22-
if (isHeaded && extensionPath !== undefined) {
22+
try {
23+
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") return
24+
const extensionPath = Cypress.env("ACCESSIBILITY_EXTENSION_PATH")
25+
const isHeaded = Cypress.browser.isHeaded;
26+
if (isHeaded && extensionPath !== undefined) {
2327

24-
let shouldScanTestForAccessibility = true;
25-
if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY") || Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) {
28+
let shouldScanTestForAccessibility = true;
29+
if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY") || Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) {
2630

27-
try {
28-
let includeTagArray = Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY").split(";")
29-
let excludeTagArray = Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY").split(";")
31+
try {
32+
let includeTagArray = [];
33+
let excludeTagArray = [];
34+
if (Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY")) {
35+
includeTagArray = Cypress.env("INCLUDE_TAGS_FOR_ACCESSIBILITY").split(";")
36+
}
37+
if (Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY")) {
38+
excludeTagArray = Cypress.env("EXCLUDE_TAGS_FOR_ACCESSIBILITY").split(";")
39+
}
3040

31-
const fullTestName = attributes.title;
32-
const excluded = excludeTagArray.some((exclude) => fullTestName.includes(exclude));
33-
const included = includeTagArray.length === 0 || includeTags.some((include) => fullTestName.includes(include));
34-
shouldScanTestForAccessibility = !excluded && included;
35-
} catch (error){
36-
console.log("Error while validating test case for accessibility before scanning. Error : ", error);
41+
const fullTestName = attributes.title;
42+
const excluded = excludeTagArray.some((exclude) => fullTestName.includes(exclude));
43+
const included = includeTagArray.length === 0 || includeTags.some((include) => fullTestName.includes(include));
44+
shouldScanTestForAccessibility = !excluded && included;
45+
} catch (error){
46+
console.log("Error while validating test case for accessibility before scanning. Error : ", error);
47+
}
3748
}
38-
}
39-
let os_data;
40-
if (Cypress.env("OS")) {
41-
os_data = Cypress.env("OS");
42-
} else {
43-
os_data = Cypress.platform === 'linux' ? 'mac' : "win"
44-
}
45-
const dataForExtension = {
46-
"saveResults": shouldScanTestForAccessibility,
47-
"testDetails": {
48-
"name": attributes.title,
49-
"testRunId": '5058', // variable not consumed, shouldn't matter what we send
50-
"filePath": attributes.invocationDetails.relativeFile,
51-
"scopeList": [
52-
attributes.invocationDetails.relativeFile,
53-
attributes.title
54-
]
55-
},
56-
"platform": {
57-
"os_name": os_data,
58-
"os_version": Cypress.env("OS_VERSION"),
59-
"browser_name": Cypress.browser.name,
60-
"browser_version": Cypress.browser.version
49+
let os_data;
50+
if (Cypress.env("OS")) {
51+
os_data = Cypress.env("OS");
52+
} else {
53+
os_data = Cypress.platform === 'linux' ? 'mac' : "win"
6154
}
62-
};
63-
return new Promise((resolve, reject) => {
64-
if (dataForExtension.saveResults) {
65-
window.parent.addEventListener('A11Y_TAP_TRANSPORTER', (event) => {
66-
resolve(event.detail);
67-
});
68-
}
69-
const e = new CustomEvent('A11Y_TEST_END', {detail: dataForExtension});
70-
window.parent.dispatchEvent(e);
71-
if (dataForExtension.saveResults !== true )
72-
resolve();
73-
});
74-
}
55+
const dataForExtension = {
56+
"saveResults": shouldScanTestForAccessibility,
57+
"testDetails": {
58+
"name": attributes.title,
59+
"testRunId": '5058', // variable not consumed, shouldn't matter what we send
60+
"filePath": attributes.invocationDetails.relativeFile,
61+
"scopeList": [
62+
attributes.invocationDetails.relativeFile,
63+
attributes.title
64+
]
65+
},
66+
"platform": {
67+
"os_name": os_data,
68+
"os_version": Cypress.env("OS_VERSION"),
69+
"browser_name": Cypress.browser.name,
70+
"browser_version": Cypress.browser.version
71+
}
72+
};
73+
return new Promise((resolve, reject) => {
74+
if (dataForExtension.saveResults) {
75+
window.parent.addEventListener('A11Y_TAP_TRANSPORTER', (event) => {
76+
resolve(event.detail);
77+
});
78+
}
79+
const e = new CustomEvent('A11Y_TEST_END', {detail: dataForExtension});
80+
window.parent.dispatchEvent(e);
81+
if (dataForExtension.saveResults !== true )
82+
resolve();
83+
});
84+
}
7585

86+
} catch {}
7687
});
7788

7889
Cypress.Commands.add('getAccessibilityResultsSummary', () => {
79-
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") {
80-
console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`);
81-
return
82-
}
83-
return new Promise(function (resolve, reject) {
84-
try{
85-
const e = new CustomEvent('A11Y_TAP_GET_RESULTS_SUMMARY');
86-
const fn = function (event) {
87-
window.parent.removeEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn);
88-
resolve(event.detail.summary);
89-
};
90-
window.parent.addEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn);
91-
window.parent.dispatchEvent(e);
92-
} catch (err) {
93-
console.log("No accessibility results summary was found.");
94-
reject(err);
90+
try {
91+
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") {
92+
console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`);
93+
return
9594
}
96-
});
95+
return new Promise(function (resolve, reject) {
96+
try{
97+
const e = new CustomEvent('A11Y_TAP_GET_RESULTS_SUMMARY');
98+
const fn = function (event) {
99+
window.parent.removeEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn);
100+
resolve(event.detail.summary);
101+
};
102+
window.parent.addEventListener('A11Y_RESULTS_SUMMARY_RESPONSE', fn);
103+
window.parent.dispatchEvent(e);
104+
} catch (err) {
105+
console.log("No accessibility results summary was found.");
106+
reject(err);
107+
}
108+
});
109+
} catch {}
110+
97111
});
98112

99113
Cypress.Commands.add('getAccessibilityResults', () => {
100-
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") {
101-
console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`);
102-
return
103-
}
104-
return new Promise(function (resolve, reject) {
105-
try{
106-
const e = new CustomEvent('A11Y_TAP_GET_RESULTS');
107-
const fn = function (event) {
108-
window.parent.removeEventListener('A11Y_RESULTS_RESPONSE', fn);
109-
resolve(event.detail.summary);
110-
};
111-
window.parent.addEventListener('A11Y_RESULTS_RESPONSE', fn);
112-
window.parent.dispatchEvent(e);
113-
} catch (err) {
114-
console.log("No accessibility results were found.");
115-
reject(err);
114+
try {
115+
if (Cypress.env("IS_ACCESSIBILITY_EXTENSION_LOADED") !== "true") {
116+
console.log(`Not a Accessibility Automation session, cannot retrieve Accessibility results.`);
117+
return
116118
}
117-
});
119+
return new Promise(function (resolve, reject) {
120+
try{
121+
const e = new CustomEvent('A11Y_TAP_GET_RESULTS');
122+
const fn = function (event) {
123+
window.parent.removeEventListener('A11Y_RESULTS_RESPONSE', fn);
124+
resolve(event.detail.summary);
125+
};
126+
window.parent.addEventListener('A11Y_RESULTS_RESPONSE', fn);
127+
window.parent.dispatchEvent(e);
128+
} catch (err) {
129+
console.log("No accessibility results were found.");
130+
reject(err);
131+
}
132+
});
133+
} catch {}
134+
118135
});
119136

bin/accessibility-automation/helper.js

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@ const helper = require('../helpers/helper');
1010

1111
exports.checkAccessibilityPlatform = (user_config) => {
1212
let accessibility = false;
13-
user_config.browsers.forEach(browser => {
14-
if (browser.accessibility) {
15-
accessibility = true;
16-
return true;
17-
}
18-
})
13+
try {
14+
user_config.browsers.forEach(browser => {
15+
if (browser.accessibility) {
16+
accessibility = true;
17+
return true;
18+
}
19+
})
20+
} catch {}
21+
1922
return accessibility;
2023
}
2124

2225
exports.setAccessibilityCypressCapabilities = async (user_config, accessibilityResponse) => {
23-
if (user_config.run_settings.accessibilityOptions) {
24-
25-
} else {
26+
if (utils.isUndefined(user_config.run_settings.accessibilityOptions)) {
2627
user_config.run_settings.accessibilityOptions = {}
2728
}
28-
user_config.run_settings.accessibilityOptions.authToken = accessibilityResponse.data.accessibilityToken;
29-
user_config.run_settings.accessibilityOptions.auth = accessibilityResponse.data.accessibilityToken;
30-
user_config.run_settings.accessibilityOptions.scannerVersion = accessibilityResponse.data.scannerVersion;
29+
user_config.run_settings.accessibilityOptions["authToken"] = accessibilityResponse.data.accessibilityToken;
30+
user_config.run_settings.accessibilityOptions["auth"] = accessibilityResponse.data.accessibilityToken;
31+
user_config.run_settings.accessibilityOptions["scannerVersion"] = accessibilityResponse.data.scannerVersion;
3132
user_config.run_settings.system_env_vars.push(`ACCESSIBILITY_AUTH=${accessibilityResponse.data.accessibilityToken}`)
3233
user_config.run_settings.system_env_vars.push(`ACCESSIBILITY_SCANNERVERSION=${accessibilityResponse.data.scannerVersion}`)
3334
}
@@ -37,13 +38,13 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
3738
try {
3839
const userName = user_config["auth"]["username"];
3940
const accessKey = user_config["auth"]["access_key"];
40-
let settings = user_config.run_settings.accessibilityOptions;
41+
let settings = utils.isUndefined(user_config.run_settings.accessibilityOptions) ? {} : user_config.run_settings.accessibilityOptions
4142

4243
const {
4344
buildName,
4445
projectName,
4546
buildDescription
46-
} = getBuildDetails(user_config);
47+
} = helper.getBuildDetails(user_config);
4748

4849
const data = {
4950
'projectName': projectName,
@@ -65,7 +66,7 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
6566
version: os.version(),
6667
arch: os.arch()
6768
},
68-
'browserstackAutomation': process.env.BROWSERSTACK_AUTOMATION
69+
'browserstackAutomation': process.env.BROWSERSTACK_AUTOMATION === 'true'
6970
};
7071

7172
const config = {
@@ -79,9 +80,8 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
7980
};
8081

8182
const response = await nodeRequest(
82-
'POST', 'test_runs', data, config
83+
'POST', 'test_runs', data, config, API_URL
8384
);
84-
logger.info("response in createAccessibilityTestRun", response);
8585
process.env.BS_A11Y_JWT = response?.data?.data?.accessibilityToken;
8686
process.env.BS_A11Y_TEST_RUN_ID = response?.data?.data?.id;
8787

@@ -125,7 +125,6 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
125125
}
126126

127127
const nodeRequest = (type, url, data, config) => {
128-
logger.info("API URL IN noderequest", API_URL);
129128
return new Promise(async (resolve, reject) => {
130129
const options = {...config,...{
131130
method: type,
@@ -157,31 +156,6 @@ const nodeRequest = (type, url, data, config) => {
157156
});
158157
}
159158

160-
const getBuildDetails = (bsConfig) => {
161-
let buildName = '',
162-
projectName = '',
163-
buildDescription = '',
164-
buildTags = [];
165-
166-
/* Pick from environment variables */
167-
buildName = process.env.BROWSERSTACK_BUILD_NAME || buildName;
168-
projectName = process.env.BROWSERSTACK_PROJECT_NAME || projectName;
169-
170-
/* Pick from run settings */
171-
buildName = buildName || bsConfig["run_settings"]["build_name"];
172-
projectName = projectName || bsConfig["run_settings"]["project_name"];
173-
if(!utils.isUndefined(bsConfig["run_settings"]["build_tag"])) buildTags = [...buildTags, bsConfig["run_settings"]["build_tag"]];
174-
175-
buildName = buildName || path.basename(path.resolve(process.cwd()));
176-
177-
return {
178-
buildName,
179-
projectName,
180-
buildDescription,
181-
buildTags
182-
};
183-
}
184-
185159
const getAccessibilityCypressCommandEventListener = () => {
186160
return (
187161
`require('browserstack-cypress-cli/bin/accessibility-automation/cypress');`

bin/accessibility-automation/plugin/index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,26 @@ const browserstackAccessibility = (on, config) => {
66
on('before:browser:launch', (browser = {}, launchOptions) => {
77
try {
88

9-
if (browser.name !== 'chrome') {
10-
console.log(`Accessibility Automation will run only on Chrome browsers.`);
11-
browser_validation = false;
9+
if (process.env.ACCESSIBILITY_EXTENSION_PATH !== undefined) {
10+
if (browser.name !== 'chrome') {
11+
console.log(`Accessibility Automation will run only on Chrome browsers.`);
12+
browser_validation = false;
13+
}
14+
if (browser.name === 'chrome' && browser.majorVersion <= 94) {
15+
console.log(`Accessibility Automation will run only on Chrome browser version greater than 94.`);
16+
browser_validation = false;
17+
}
18+
if (browser.isHeadless === true) {
19+
console.log(`Accessibility Automation will not run on legacy headless mode. Switch to new headless mode or avoid using headless mode.`);
20+
browser_validation = false;
21+
}
22+
if (browser_validation) {
23+
const ally_path = path.dirname(process.env.ACCESSIBILITY_EXTENSION_PATH)
24+
launchOptions.extensions.push(ally_path);
25+
return launchOptions
26+
}
1227
}
13-
if (browser.majorVersion <= 94) {
14-
console.log(`Accessibility Automation will run only on Chrome browser version greater than 94.`);
15-
browser_validation = false;
16-
}
17-
if (browser.isHeadless === true) {
18-
console.log(`Accessibility Automation will not run on legacy headless mode. Switch to new headless mode or avoid using headless mode.`);
19-
browser_validation = false;
20-
}
21-
22-
if (process.env.ACCESSIBILITY_EXTENSION_PATH === undefined) {
23-
browser_validation = false
24-
return
25-
}
26-
27-
if (browser_validation) {
28-
const ally_path = path.dirname(process.env.ACCESSIBILITY_EXTENSION_PATH)
29-
launchOptions.extensions.push(ally_path);
30-
return launchOptions
31-
}
32-
} catch {}
28+
} catch(err) {}
3329

3430
})
3531
config.env.ACCESSIBILITY_EXTENSION_PATH = process.env.ACCESSIBILITY_EXTENSION_PATH

0 commit comments

Comments
 (0)