|
1 | 1 | import { NightwatchBrowser } from 'nightwatch' |
2 | 2 | import EventEmitter from 'events' |
3 | 3 |
|
| 4 | +const findElementsAsync = (browser: NightwatchBrowser, selector: string): Promise<any[]> => { |
| 5 | + return new Promise((resolve, reject) => { |
| 6 | + browser.findElements('css selector', selector, (result) => { |
| 7 | + resolve(result.value as any) |
| 8 | + }) |
| 9 | + }) |
| 10 | +} |
| 11 | + |
| 12 | +const getTextAsync = (browser: NightwatchBrowser, elementId: string): Promise<string> => { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + browser.getText(elementId, (result) => { |
| 15 | + const text = typeof result === 'string' ? result : result.value |
| 16 | + resolve(text as any) |
| 17 | + }) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | + |
4 | 22 | class WaitForElementContainsText extends EventEmitter { |
5 | 23 | command (this: NightwatchBrowser, id: string, value: string, timeout = 10000): NightwatchBrowser { |
6 | 24 | let waitId // eslint-disable-line |
7 | | - let currentValue |
8 | | - const runid = setInterval(() => { |
9 | | - this.api.getText(id, (result) => { |
10 | | - currentValue = result.value |
11 | | - if (typeof result.value === 'string' && result.value.indexOf(value) !== -1) { |
| 25 | + let currentValues: string[] = [] |
| 26 | + const runid = setInterval(async () => { |
| 27 | + try { |
| 28 | + |
| 29 | + let elements = await findElementsAsync(this.api, id) |
| 30 | + |
| 31 | + if (!elements) { |
| 32 | + currentValues = [] |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if (elements.length === 0) { |
| 37 | + currentValues = [] |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // Check all elements that match the selector |
| 42 | + let foundMatch = false |
| 43 | + const textValues: string[] = [] |
| 44 | + |
| 45 | + for (const element of elements) { |
| 46 | + let text = await getTextAsync(this.api, element) |
| 47 | + currentValues.push(text) |
| 48 | + |
| 49 | + if (typeof text === 'string' && text.indexOf(value) !== -1) { |
| 50 | + foundMatch = true |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + currentValues = textValues |
| 56 | + |
| 57 | + if (foundMatch) { |
12 | 58 | clearInterval(runid) |
13 | 59 | clearTimeout(waitId) |
14 | 60 | this.api.assert.ok(true, `WaitForElementContainsText ${id} contains ${value}`) |
15 | 61 | this.emit('complete') |
16 | 62 | } |
17 | | - }) |
| 63 | + } catch (err) { |
| 64 | + // Ignore errors and continue polling |
| 65 | + console.error(`Error in waitForElementContainsText for selector ${id}:`, err) |
| 66 | + } |
18 | 67 | }, 200) |
19 | 68 |
|
20 | 69 | waitId = setTimeout(() => { |
21 | 70 | clearInterval(runid) |
22 | | - this.api.assert.fail(`TimeoutError: An error occurred while running .waitForElementContainsText() command on ${id} after ${timeout} milliseconds. expected: ${value} - got: ${currentValue}`) |
| 71 | + const valuesFound = currentValues.length > 0 ? currentValues.join(', ') : 'none' |
| 72 | + this.api.assert.fail(`TimeoutError: An error occurred while running .waitForElementContainsText() command on ${id} after ${timeout} milliseconds. expected: ${value} - got: ${valuesFound}`) |
23 | 73 | }, timeout) |
24 | 74 | return this |
25 | 75 | } |
|
0 commit comments