* Switches the context of the browser session to the window tab with the given title. * This functionality is especially valuable in complex testing scenarios involving multiple window tabs, * allowing for interaction with a particular window or tab based on its title * * @param {string}
(title)
| 1368 | * @throws {Error} throws an error if no window with the specified title is found |
| 1369 | */ |
| 1370 | async switchToWindowWithTitle(title) { |
| 1371 | if (this.windowHandles) { |
| 1372 | await this.windowHandles.switchToWindowWithProperty('title', title); |
| 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | let windowHandles = await this.driver.getAllWindowHandles(); |
| 1377 | let timeElapsed = 0; |
| 1378 | |
| 1379 | while (timeElapsed <= this.timeout) { |
| 1380 | for (const handle of windowHandles) { |
| 1381 | // Wait 25 x 200ms = 5 seconds for the title to match the target title |
| 1382 | const handleTitle = await retry( |
| 1383 | { |
| 1384 | retries: 25, |
| 1385 | delay: 200, |
| 1386 | }, |
| 1387 | async () => { |
| 1388 | await this.driver.switchTo().window(handle); |
| 1389 | return await this.driver.getTitle(); |
| 1390 | }, |
| 1391 | ); |
| 1392 | |
| 1393 | if (handleTitle === title) { |
| 1394 | return; |
| 1395 | } |
| 1396 | } |
| 1397 | const delayTime = 1000; |
| 1398 | await this.delay(delayTime); |
| 1399 | timeElapsed += delayTime; |
| 1400 | // refresh the window handles |
| 1401 | windowHandles = await this.driver.getAllWindowHandles(); |
| 1402 | } |
| 1403 | |
| 1404 | throw new Error(`No window with title: ${title}`); |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * Waits until there is a window/tab with the given title, without changing the current window focus. |