MCPcopy
hub / github.com/ampproject/amphtml / waitForElementVisibility

Function waitForElementVisibility

build-system/tasks/visual-diff/verifiers.js:139–197  ·  view source on GitHub ↗

* 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
)

Source from the content-addressed store, hash-verified

137 * error with the message value set to the CSS selector.
138 */
139async 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);

Callers 3

verifySelectorsInvisibleFunction · 0.85
verifySelectorsVisibleFunction · 0.85
waitForPageLoadFunction · 0.85

Calls 5

cyanFunction · 0.85
nowMethod · 0.80
logFunction · 0.70
sleepFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected