|
1 | 1 | const assert = require('node:assert/strict'); |
2 | | -const { afterEach, test } = require('node:test'); |
| 2 | +const { afterEach, mock, test } = require('node:test'); |
3 | 3 | const { BasePage } = require('./lib/common/BasePage.js'); |
4 | 4 | const { Keys } = require('./lib/common/consts.js'); |
5 | 5 | const { installLegacyDesktopAttributes, scrollNativeDesktopElement, sendNativeDesktopKeys } = require('./lib/common/nativeDesktop.js'); |
6 | 6 |
|
7 | 7 | afterEach(() => { |
| 8 | + mock.restoreAll(); |
8 | 9 | delete globalThis.browser; |
9 | 10 | }); |
10 | 11 |
|
| 12 | +test('Windows assertion checks inspect menu windows without selecting or activating them', async () => { |
| 13 | + const reads = []; |
| 14 | + globalThis.browser = { |
| 15 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'win32' }, |
| 16 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 17 | + sessionId: 'session', |
| 18 | + getWindowHandles: async () => ['main', 'menu'], |
| 19 | + getWindowHandle: async () => 'main', |
| 20 | + switchToWindow: () => assert.fail('an assertion check must not change native focus'), |
| 21 | + }; |
| 22 | + mock.method(globalThis, 'fetch', async (url, init) => { |
| 23 | + reads.push(String(url)); |
| 24 | + assert.equal(init.method, 'GET'); |
| 25 | + return new Response(JSON.stringify({ value: [{ role: 'menu', children: [{ role: 'menuitem', children: [] }] }] })); |
| 26 | + }); |
| 27 | + assert.equal(await new BasePage().didAssertPopup(), false); |
| 28 | + // eslint-disable-next-line @microsoft/sdl/no-insecure-url -- mocked loopback transport; no service is contacted |
| 29 | + assert.deepEqual(reads, ['http://127.0.0.1:1/session/session/furn/window/menu/tree']); |
| 30 | + browser.getWindowHandles = async () => ['main']; |
| 31 | + browser.getWindowHandle = () => assert.fail('one window needs no further inspection'); |
| 32 | + assert.equal(await new BasePage().didAssertPopup(), false); |
| 33 | + assert.equal(reads.length, 1); |
| 34 | +}); |
| 35 | + |
| 36 | +test('Windows assertion checks fail closed for dialogs, unrecognized roots, and unreadable windows', async () => { |
| 37 | + globalThis.browser = { |
| 38 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'windows' }, |
| 39 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 40 | + sessionId: 'session', |
| 41 | + getWindowHandles: async () => ['main', 'other'], |
| 42 | + getWindowHandle: async () => 'main', |
| 43 | + switchToWindow: () => assert.fail('an assertion check must not change native focus'), |
| 44 | + }; |
| 45 | + let value; |
| 46 | + const reader = mock.method(globalThis, 'fetch', async () => new Response(JSON.stringify({ value }))); |
| 47 | + const page = new BasePage(); |
| 48 | + for (const roots of [ |
| 49 | + [{ role: 'window', name: 'Assertion failed', children: [] }], |
| 50 | + [{ role: 'group', children: [] }], |
| 51 | + [], |
| 52 | + [{ role: 'menu', children: [{ role: 'dialog', children: [] }] }], |
| 53 | + [{ role: 'menu', children: [{ role: 'window', children: [] }] }], |
| 54 | + [ |
| 55 | + { role: 'menu', children: [] }, |
| 56 | + { role: 'dialog', children: [] }, |
| 57 | + ], |
| 58 | + ]) { |
| 59 | + value = roots; |
| 60 | + assert.equal(await page.didAssertPopup(), true); |
| 61 | + } |
| 62 | + reader.mock.mockImplementation(async () => { |
| 63 | + throw new Error('provider is unavailable'); |
| 64 | + }); |
| 65 | + await assert.rejects(page.didAssertPopup(), /provider is unavailable/); |
| 66 | +}); |
| 67 | + |
| 68 | +test('Windows assertion checks do not let an open menu hide another unexpected window', async () => { |
| 69 | + globalThis.browser = { |
| 70 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'win32' }, |
| 71 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 72 | + sessionId: 'session', |
| 73 | + getWindowHandles: async () => ['main', 'menu', 'assert'], |
| 74 | + getWindowHandle: async () => 'main', |
| 75 | + }; |
| 76 | + mock.method( |
| 77 | + globalThis, |
| 78 | + 'fetch', |
| 79 | + async (url) => |
| 80 | + new Response( |
| 81 | + JSON.stringify({ |
| 82 | + value: [{ role: String(url).endsWith('/menu/tree') ? 'menu' : 'window', children: [] }], |
| 83 | + }), |
| 84 | + ), |
| 85 | + ); |
| 86 | + assert.equal(await new BasePage().didAssertPopup(), true); |
| 87 | +}); |
| 88 | + |
| 89 | +test('Windows assertion checks skip a vanished menu and continue checking other windows', async () => { |
| 90 | + let handles = ['main', 'closing-menu']; |
| 91 | + globalThis.browser = { |
| 92 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'win32' }, |
| 93 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 94 | + sessionId: 'session', |
| 95 | + getWindowHandles: async () => handles, |
| 96 | + getWindowHandle: async () => 'main', |
| 97 | + switchToWindow: () => assert.fail('an assertion check must not change native focus'), |
| 98 | + }; |
| 99 | + mock.method(globalThis, 'fetch', async (url) => |
| 100 | + String(url).endsWith('/closing-menu/tree') |
| 101 | + ? new Response(JSON.stringify({ value: { error: 'no such window', message: 'The menu closed.' } }), { status: 404 }) |
| 102 | + : new Response(JSON.stringify({ value: [{ role: 'window', name: 'Assertion failed', children: [] }] })), |
| 103 | + ); |
| 104 | + const page = new BasePage(); |
| 105 | + assert.equal(await page.didAssertPopup(), false); |
| 106 | + handles = ['main', 'closing-menu', 'assert']; |
| 107 | + assert.equal(await page.didAssertPopup(), true); |
| 108 | +}); |
| 109 | + |
| 110 | +test('Windows assertion checks recheck an empty or unrecognized root before reporting an assertion', async () => { |
| 111 | + let lists = 0; |
| 112 | + globalThis.browser = { |
| 113 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'win32' }, |
| 114 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 115 | + sessionId: 'session', |
| 116 | + getWindowHandles: async () => (++lists === 1 ? ['main', 'closing-menu'] : ['main']), |
| 117 | + getWindowHandle: async () => 'main', |
| 118 | + switchToWindow: () => assert.fail('an assertion check must not change native focus'), |
| 119 | + }; |
| 120 | + let value; |
| 121 | + mock.method(globalThis, 'fetch', async () => new Response(JSON.stringify({ value }))); |
| 122 | + const page = new BasePage(); |
| 123 | + for (const roots of [[], [{ role: 'group', children: [] }], [{ role: 'menu', children: [{ role: 'dialog', children: [] }] }]]) { |
| 124 | + lists = 0; |
| 125 | + value = roots; |
| 126 | + assert.equal(await page.didAssertPopup(), false); |
| 127 | + } |
| 128 | +}); |
| 129 | + |
| 130 | +test('Windows assertion checks do not treat other provider errors as closed windows', async () => { |
| 131 | + globalThis.browser = { |
| 132 | + capabilities: { browserName: 'furn-native-desktop', platformName: 'windows', 'furn:endpoint': 'win32' }, |
| 133 | + options: { protocol: 'http', hostname: '127.0.0.1', port: 1, path: '/' }, |
| 134 | + sessionId: 'session', |
| 135 | + getWindowHandles: async () => ['main', 'unreadable-menu'], |
| 136 | + getWindowHandle: async () => 'main', |
| 137 | + }; |
| 138 | + let code; |
| 139 | + mock.method( |
| 140 | + globalThis, |
| 141 | + 'fetch', |
| 142 | + async () => new Response(JSON.stringify({ value: { error: code, message: 'The provider could not read the tree.' } }), { status: 500 }), |
| 143 | + ); |
| 144 | + for (const error of ['unknown error', 'stale element reference']) { |
| 145 | + code = error; |
| 146 | + await assert.rejects(new BasePage().didAssertPopup(), { code: error }); |
| 147 | + } |
| 148 | +}); |
| 149 | + |
| 150 | +test('assertion checks retain non-native and macOS window-count behavior', async () => { |
| 151 | + for (const capabilities of [ |
| 152 | + { browserName: 'non-native' }, |
| 153 | + { browserName: 'furn-native-desktop', platformName: 'macos', 'furn:endpoint': 'macos' }, |
| 154 | + ]) { |
| 155 | + globalThis.browser = { capabilities, getWindowHandles: async () => ['main', 'other'] }; |
| 156 | + assert.equal(await new BasePage().didAssertPopup(), true); |
| 157 | + } |
| 158 | +}); |
| 159 | + |
11 | 160 | test('native checkbox and switch state never falls back to selected', async () => { |
12 | 161 | const page = new BasePage(); |
13 | 162 | const element = { elementId: 'toggle', isSelected: () => assert.fail('checked is not selected') }; |
|
0 commit comments