|
1 | 1 | /*
|
2 |
| -Copyright 2022 The Matrix.org Foundation C.I.C. |
3 |
| -
|
4 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| -you may not use this file except in compliance with the License. |
6 |
| -You may obtain a copy of the License at |
7 |
| -
|
8 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| -
|
10 |
| -Unless required by applicable law or agreed to in writing, software |
11 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| -See the License for the specific language governing permissions and |
14 |
| -limitations under the License. |
| 2 | +copied from matrix-react-sdk/cypress/support/axe.ts |
15 | 3 | */
|
16 | 4 |
|
17 | 5 | /// <reference types="cypress" />
|
18 | 6 |
|
19 | 7 | import "cypress-axe";
|
20 | 8 | import * as axe from "axe-core";
|
21 |
| -import { Options } from "cypress-axe"; |
22 | 9 |
|
| 10 | +import type { Options } from "cypress-axe"; |
23 | 11 | import Chainable = Cypress.Chainable;
|
24 | 12 |
|
25 | 13 | function terminalLog(violations: axe.Result[]): void {
|
@@ -67,3 +55,35 @@ Cypress.Commands.overwrite(
|
67 | 55 | );
|
68 | 56 | },
|
69 | 57 | );
|
| 58 | + |
| 59 | +// Load axe-core into the window under test. |
| 60 | +// |
| 61 | +// The injectAxe in cypress-axe attempts to load axe via an `eval`. That conflicts with our CSP |
| 62 | +// which disallows "unsafe-eval". So, replace it with an implementation that loads it via an |
| 63 | +// injected <script> element. |
| 64 | +Cypress.Commands.overwrite("injectAxe", (originalFn: Chainable["injectAxe"]): void => { |
| 65 | + Cypress.log({ name: "injectAxe" }); |
| 66 | + |
| 67 | + // load the minified axe source, and create an intercept to serve it up |
| 68 | + cy.readFile("node_modules/axe-core/axe.min.js", { log: false }).then((source) => { |
| 69 | + cy.intercept("/_axe", source); |
| 70 | + }); |
| 71 | + |
| 72 | + // inject a script tag to load it |
| 73 | + cy.get("head", { log: false }).then( |
| 74 | + (head) => |
| 75 | + new Promise((resolve, reject) => { |
| 76 | + const script = document.createElement("script"); |
| 77 | + script.type = "text/javascript"; |
| 78 | + script.async = true; |
| 79 | + script.onload = resolve; |
| 80 | + script.onerror = (_e) => { |
| 81 | + // Unfortunately there does not seem to be a way to get a reason for the error. |
| 82 | + // The error event is useless. |
| 83 | + reject(new Error("Unable to load axe")); |
| 84 | + }; |
| 85 | + script.src = "/_axe"; |
| 86 | + head.get()[0].appendChild(script); |
| 87 | + }), |
| 88 | + ); |
| 89 | +}); |
0 commit comments