| 93 | } |
| 94 | |
| 95 | async waitVisibility( |
| 96 | elementLocator: By, |
| 97 | timeout: number = TIMEOUT_CONSTANTS.TS_SELENIUM_CLICK_ON_VISIBLE_ITEM, |
| 98 | polling: number = TIMEOUT_CONSTANTS.TS_SELENIUM_DEFAULT_POLLING |
| 99 | ): Promise<WebElement> { |
| 100 | const attempts: number = Math.ceil(timeout / polling); |
| 101 | Logger.trace(`${elementLocator}`); |
| 102 | |
| 103 | for (let i: number = 0; i < attempts; i++) { |
| 104 | let element: WebElement; |
| 105 | try { |
| 106 | element = await this.driver.wait(until.elementLocated(elementLocator), polling); |
| 107 | } catch (err) { |
| 108 | if (i >= attempts - 1) { |
| 109 | Logger.error(`failed with exception, out of attempts - ${err}`); |
| 110 | throw err; |
| 111 | } |
| 112 | |
| 113 | if (err instanceof error.TimeoutError) { |
| 114 | if (attempts !== 1) { |
| 115 | // waitVisibility was spamming other methods when the number of attempts was 1 - only show message if attempts > 1 |
| 116 | Logger.trace(`polling timed out attempt #${i + 1}, retrying with ${polling}ms timeout`); |
| 117 | } |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | if (err instanceof error.NoSuchWindowError) { |
| 122 | // sometimes waitVisibility fails with NoSuchWindowError when the check is run too soon before the page loads |
| 123 | Logger.trace(`failed with NoSuchWindow exception. Attempt #${i + 1}, retrying with ${polling}ms timeout`); |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | Logger.error(`failed with an unexpected exception - ${err}`); |
| 128 | throw err; |
| 129 | } |
| 130 | |
| 131 | try { |
| 132 | const visibleWebElement: WebElement = await this.driver.wait(until.elementIsVisible(element), polling); |
| 133 | Logger.trace('element is located and is visible.'); |
| 134 | return visibleWebElement; |
| 135 | } catch (err) { |
| 136 | if (err instanceof error.TimeoutError) { |
| 137 | if (attempts !== 1) { |
| 138 | // waitVisibility was spamming other methods when the number of attempts was 1 - only show message if attempts > 1 |
| 139 | Logger.trace(`polling timed out attempt #${i + 1}, retrying with ${polling}ms timeout`); |
| 140 | } |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (err instanceof error.StaleElementReferenceError) { |
| 145 | Logger.debug(`stale element error - ${JSON.stringify(err)}`); |
| 146 | await this.wait(polling); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | Logger.error(`failed with an unexpected exception - ${err}`); |
| 151 | throw err; |
| 152 | } |