focusgroup-utils.js (1680B)
1 /* 2 Methods for testing the focusgroup feature. 3 */ 4 5 // https://w3c.github.io/webdriver/#keyboard-actions 6 const kArrowLeft = '\uE012'; 7 const kArrowUp = '\uE013'; 8 const kArrowRight = '\uE014'; 9 const kArrowDown = '\uE015'; 10 11 // Set the focus on target and send the arrow key press event from it. 12 function focusAndKeyPress(target, key) { 13 target.focus(); 14 return test_driver.send_keys(target, key); 15 } 16 17 function sendArrowKey(key) { 18 return new test_driver.Actions().keyDown(key).keyUp(key).send(); 19 } 20 21 // Test bidirectional navigation through a list of elements in visual order. 22 // Tests forward navigation with kArrowRight and backward navigation with kArrowLeft. 23 // At boundaries, verifies focus does not move (unless wrap is expected). 24 async function assert_focus_navigation_bidirectional(elements, shouldWrap = false) { 25 // Test forward navigation. 26 for (let i = 0; i < elements.length; i++) { 27 await focusAndKeyPress(elements[i], kArrowRight); 28 const nextIndex = shouldWrap ? (i + 1) % elements.length : Math.min(i + 1, elements.length - 1); 29 const expectedElement = elements[nextIndex]; 30 assert_equals(document.activeElement, expectedElement, 31 `From ${elements[i].id}, right arrow should move to ${expectedElement.id}`); 32 } 33 34 // Test backward navigation. 35 for (let i = elements.length - 1; i >= 0; i--) { 36 await focusAndKeyPress(elements[i], kArrowLeft); 37 const prevIndex = shouldWrap ? (i - 1 + elements.length) % elements.length : Math.max(i - 1, 0); 38 const expectedElement = elements[prevIndex]; 39 assert_equals(document.activeElement, expectedElement, 40 `From ${elements[i].id}, left arrow should move to ${expectedElement.id}`); 41 } 42 }