* Waits for specified CSS selector to be located in the DOM * * @param {String} cssSelector CSS selector to query for * @param {Object} options optional parameters * @returns {Promise} promise which resolves to first matching element
(cssSelector, options = {})
| 55 | * @returns {Promise} promise which resolves to first matching element |
| 56 | */ |
| 57 | function waitForSelector(cssSelector, options = {}) { |
| 58 | const settings = { |
| 59 | ...defaults, |
| 60 | ...options, |
| 61 | }; |
| 62 | |
| 63 | return new Promise((resolve, reject) => { |
| 64 | let timeElapsed = 0; |
| 65 | |
| 66 | const int = setInterval(() => { |
| 67 | const elm = document.querySelector(cssSelector); |
| 68 | if (elm) { |
| 69 | clearInterval(int); |
| 70 | resolve(elm); |
| 71 | } |
| 72 | |
| 73 | timeElapsed += settings.delay; |
| 74 | |
| 75 | if (timeElapsed >= settings.timeout) { |
| 76 | const msg = `waitForSelector did not match CSS selector '${cssSelector}' (${settings.timeout} ms)`; |
| 77 | |
| 78 | reject(msg); |
| 79 | } |
| 80 | }, settings.delay); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Waits for specified CSS selector to contain text content |
no outgoing calls
no test coverage detected
searching dependent graphs…