* Wait until the element is either hidden or visible or until timed out. * * Timeout is set to CSS_SELECTOR_RETRY_MS * CSS_SELECTOR_RETRY_ATTEMPTS ms. * * @param {!puppeteer.Page} page page to check the visibility of elements in. * @param {string} selector CSS selector for elements to wait on.
( page, selector, options, timeoutMillis )
| 137 | * error with the message value set to the CSS selector. |
| 138 | */ |
| 139 | async function waitForElementVisibility( |
| 140 | page, |
| 141 | selector, |
| 142 | options, |
| 143 | timeoutMillis |
| 144 | ) { |
| 145 | const waitForVisible = Boolean(options['visible']); |
| 146 | const waitForHidden = Boolean(options['hidden']); |
| 147 | if (waitForVisible == waitForHidden) { |
| 148 | log( |
| 149 | 'fatal', |
| 150 | 'waitForElementVisibility must be called with exactly one of', |
| 151 | "'visible' or 'hidden' set to true." |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | const startTime = Date.now(); |
| 156 | do { |
| 157 | const elementsAreVisible = []; |
| 158 | |
| 159 | for (const elementHandle of await page.$$(selector)) { |
| 160 | const boundingBox = await elementHandle.boundingBox(); |
| 161 | const elementIsVisible = |
| 162 | boundingBox != null && boundingBox.height > 0 && boundingBox.width > 0; |
| 163 | elementsAreVisible.push(elementIsVisible); |
| 164 | } |
| 165 | |
| 166 | if (elementsAreVisible.length) { |
| 167 | log( |
| 168 | 'verbose', |
| 169 | 'Found', |
| 170 | cyan(elementsAreVisible.length), |
| 171 | 'element(s) matching the CSS selector', |
| 172 | cyan(selector) |
| 173 | ); |
| 174 | log( |
| 175 | 'verbose', |
| 176 | 'Expecting all element visibilities to be', |
| 177 | cyan(waitForVisible), |
| 178 | '; they are:', |
| 179 | cyan(elementsAreVisible.join(', ')) |
| 180 | ); |
| 181 | } else { |
| 182 | log('verbose', 'No', cyan(selector), 'matches found'); |
| 183 | } |
| 184 | // Since we assert that waitForVisible == !waitForHidden, there is no need |
| 185 | // to check equality to both waitForVisible and waitForHidden. |
| 186 | if ( |
| 187 | elementsAreVisible.every( |
| 188 | (elementIsVisible) => elementIsVisible == waitForVisible |
| 189 | ) |
| 190 | ) { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | await sleep(CSS_SELECTOR_RETRY_MS); |
| 195 | } while (Date.now() < startTime + timeoutMillis); |
| 196 | throw new Error(selector); |
no test coverage detected