* Waits for an element that matches the given locator to reach the specified state within the timeout period. * * @param {string | object} rawLocator - Element locator * @param {object} [options] - parameter object * @param {number} [options.timeout] - specifies the maximum amount of tim
(
rawLocator,
{ timeout = this.timeout, state = 'visible', waitAtLeastGuard = 0 } = {},
)
| 428 | * @throws {Error} Will throw an error if the element does not reach the specified state within the timeout period. |
| 429 | */ |
| 430 | async waitForSelector( |
| 431 | rawLocator, |
| 432 | { timeout = this.timeout, state = 'visible', waitAtLeastGuard = 0 } = {}, |
| 433 | ) { |
| 434 | // Playwright has a waitForSelector method that will become a shallow |
| 435 | // replacement for the implementation below. It takes an option options |
| 436 | // bucket that can include the state attribute to wait for elements that |
| 437 | // match the selector to be removed from the DOM. |
| 438 | assert(timeout > waitAtLeastGuard); |
| 439 | if (waitAtLeastGuard > 0) { |
| 440 | await this.delay(waitAtLeastGuard); |
| 441 | } |
| 442 | |
| 443 | let element; |
| 444 | if (!['visible', 'detached', 'enabled', 'disabled'].includes(state)) { |
| 445 | throw new Error(`Provided state selector ${state} is not supported`); |
| 446 | } |
| 447 | if (state === 'visible') { |
| 448 | element = await this.driver.wait( |
| 449 | until.elementLocated(this.buildLocator(rawLocator)), |
| 450 | timeout, |
| 451 | ); |
| 452 | } else if (state === 'detached') { |
| 453 | element = await this.driver.wait( |
| 454 | until.stalenessOf(await this.findElement(rawLocator)), |
| 455 | timeout, |
| 456 | ); |
| 457 | } else if (state === 'enabled') { |
| 458 | element = await this.driver.wait( |
| 459 | until.elementIsEnabled(await this.findElement(rawLocator)), |
| 460 | timeout, |
| 461 | ); |
| 462 | } else if (state === 'disabled') { |
| 463 | element = await this.driver.wait( |
| 464 | until.elementIsDisabled(await this.findElement(rawLocator)), |
| 465 | timeout, |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | return wrapElementWithAPI(element, this); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Waits for multiple elements that match the given locators to reach the specified state within the timeout period. |