|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import config from './test-config.js'; |
| 4 | +import testHelpers from './test-helpers.js'; |
| 5 | + |
| 6 | +const fileList = ['sortable-table.php']; // testHelpers.getPageList(); |
| 7 | +let mobileBrowser, |
| 8 | + desktopBrowser; |
| 9 | + |
| 10 | +describe('Test that all focusable elements have target area greater than 24 x 24 pixels', () => { |
| 11 | + beforeAll(async () => { // Put code here that should execute before starting tests. |
| 12 | + desktopBrowser = await testHelpers.getDesktopBrowser(); |
| 13 | + mobileBrowser = await testHelpers.getMobileBrowser(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(async () => { |
| 17 | + await mobileBrowser.close(); |
| 18 | + await desktopBrowser.close(); |
| 19 | + }); |
| 20 | + |
| 21 | + function truncString(s) { |
| 22 | + |
| 23 | + const maxLength = 60; |
| 24 | + let r; |
| 25 | + if (s.length > maxLength) { |
| 26 | + r = s.substring(0, maxLength) + '...'; |
| 27 | + } else { |
| 28 | + r = s; |
| 29 | + } |
| 30 | + |
| 31 | + return r; |
| 32 | + } |
| 33 | + |
| 34 | + async function testTargets(filename, isDesktop) { |
| 35 | + //console.log(`testing ${filename}, Is desktop: ${isDesktop}`); |
| 36 | + let domInfo, |
| 37 | + page; |
| 38 | + |
| 39 | + if (isDesktop) { |
| 40 | + page = await desktopBrowser.newPage(); |
| 41 | + } else { |
| 42 | + page = await mobileBrowser.newPage(); |
| 43 | + } |
| 44 | + |
| 45 | + // Wait until the DOM is fully loaded. |
| 46 | + await page.goto(`${ |
| 47 | + config.BASE_URL |
| 48 | + }/${filename}`, {waitUntil: 'domcontentloaded'}); |
| 49 | + |
| 50 | + // Let's loop through all the tabstops on the page. |
| 51 | + do { // Let's simulate a tab press |
| 52 | + await page.keyboard.press('Tab'); |
| 53 | + |
| 54 | + // Now let's see if there is a focus ring around the |
| 55 | + // focused element. |
| 56 | + domInfo = await page.evaluate(() => { |
| 57 | + function getTargetBoxImplicit(el) { |
| 58 | + if (!(el instanceof Element)) |
| 59 | + return null; |
| 60 | + |
| 61 | + |
| 62 | + const isVisible = (node) => { |
| 63 | + const cs = getComputedStyle(node); |
| 64 | + if (cs.display === 'none' || cs.visibility === 'hidden' || cs.pointerEvents === 'none') |
| 65 | + return false; |
| 66 | + |
| 67 | + const r = node.getBoundingClientRect(); |
| 68 | + return r.width > 0 && r.height > 0; |
| 69 | + }; |
| 70 | + |
| 71 | + const rects = []; |
| 72 | + if (isVisible(el)) |
| 73 | + rects.push(el.getBoundingClientRect()); |
| 74 | + |
| 75 | + |
| 76 | + // Only implicit label: <label> ... <input> ... </label> |
| 77 | + const wrapLabel = el.closest('label'); |
| 78 | + if (wrapLabel && isVisible(wrapLabel)) |
| 79 | + rects.push(wrapLabel.getBoundingClientRect()); |
| 80 | + |
| 81 | + |
| 82 | + if (! rects.length) |
| 83 | + return null; |
| 84 | + |
| 85 | + |
| 86 | + let left = rects[0].left, |
| 87 | + top = rects[0].top; |
| 88 | + let right = rects[0].right, |
| 89 | + bottom = rects[0].bottom; |
| 90 | + for (let i = 1; i < rects.length; i++) { |
| 91 | + const r = rects[i]; |
| 92 | + left = Math.min(left, r.left); |
| 93 | + top = Math.min(top, r.top); |
| 94 | + right = Math.max(right, r.right); |
| 95 | + bottom = Math.max(bottom, r.bottom); |
| 96 | + } |
| 97 | + |
| 98 | + const width = Math.max(0, right - left); |
| 99 | + const height = Math.max(0, bottom - top); |
| 100 | + return { |
| 101 | + left, |
| 102 | + top, |
| 103 | + right, |
| 104 | + bottom, |
| 105 | + width, |
| 106 | + height, |
| 107 | + x: left, |
| 108 | + y: top |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + /** Convenience: just the size |
| 113 | + function getTargetSizeImplicit(el) { |
| 114 | + const box = getTargetBoxImplicit(el); |
| 115 | + return box ? { |
| 116 | + width: Math.round(box.width), |
| 117 | + height: Math.round(box.height) |
| 118 | + } : { |
| 119 | + width: 0, |
| 120 | + height: 0 |
| 121 | + }; |
| 122 | + }*/ |
| 123 | + |
| 124 | + |
| 125 | + const {activeElement} = document; |
| 126 | + const display = document.defaultView.getComputedStyle(activeElement, null).display; |
| 127 | + const {width, height} = getTargetBoxImplicit(activeElement); |
| 128 | + const r = { |
| 129 | + offsetWidth: width, |
| 130 | + offsetHeight: height, |
| 131 | + display, |
| 132 | + isInline: display === 'inline', |
| 133 | + html: activeElement.outerHTML, |
| 134 | + isBody: activeElement === document.body |
| 135 | + } |
| 136 | + |
| 137 | + return r; |
| 138 | + }); |
| 139 | + if (! domInfo.isInline) { |
| 140 | + |
| 141 | + if (domInfo.offsetWidth < 24) { |
| 142 | + console.log(`Bad target width ${ |
| 143 | + domInfo.offsetWidth |
| 144 | + } for:\n\n ${ |
| 145 | + truncString(domInfo.html) |
| 146 | + }\n\n Display is ${ |
| 147 | + domInfo.display |
| 148 | + }`); |
| 149 | + } |
| 150 | + |
| 151 | + if (domInfo.offsetHeight < 24) { |
| 152 | + console.log(`Bad target height ${ |
| 153 | + domInfo.offsetHeight |
| 154 | + } for:\n\n ${ |
| 155 | + truncString(domInfo.html) |
| 156 | + }\n\n Display is ${ |
| 157 | + domInfo.display |
| 158 | + }`); |
| 159 | + } |
| 160 | + |
| 161 | + expect(domInfo.offsetWidth).toBeGreaterThan(24); |
| 162 | + expect(domInfo.offsetHeight).toBeGreaterThan(24); |
| 163 | + } |
| 164 | + |
| 165 | + } while (! domInfo.isBody); |
| 166 | + |
| 167 | + page.close(); |
| 168 | + } |
| 169 | + // end testFocusStates() |
| 170 | + |
| 171 | + // This goes through all the URLs in the site and |
| 172 | + // runs testFocusStates() on it twice, one in the desktop |
| 173 | + // browser and one in the mobile. |
| 174 | + for (let i = 0; i < fileList.length; i++) { |
| 175 | + const file = fileList[i]; |
| 176 | + it(`Desktop Breakpoint: Test focus states on ${file}`, async () => { |
| 177 | + await testTargets(file, true); |
| 178 | + }); |
| 179 | + it(`Mobile Breakpoint: Test focus states on ${file}`, async () => { |
| 180 | + await testTargets(file, false); |
| 181 | + }); |
| 182 | + } |
| 183 | +}); |
0 commit comments