MCPcopy
hub / github.com/MetaMask/metamask-extension / switchToWindowWithTitle

Method switchToWindowWithTitle

test/e2e/webdriver/driver.js:1370–1405  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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.

Callers 15

waitForBrowserAlertMethod · 0.95
afterRequestMethod · 0.80
beforeRequestMethod · 0.80
afterRequestMethod · 0.80
afterRequestMethod · 0.80
switchToMethod · 0.80
switchToMethod · 0.80
checkFailedSignPermitMethod · 0.80

Calls 6

delayMethod · 0.95
retryFunction · 0.85
getAllWindowHandlesMethod · 0.45
switchToMethod · 0.45
getTitleMethod · 0.45

Tested by 15

confirmTransactionFunction · 0.64
openConfirmedTransactionFunction · 0.64
withAccountSnapFunction · 0.64
queueSignaturesFunction · 0.64
assertInfoValuesFunction · 0.64
assertVerifiedResultsFunction · 0.64
assertInfoValuesFunction · 0.64
assertVerifiedResultsFunction · 0.64
assertInfoValuesFunction · 0.64
triggerSignatureRequestFunction · 0.64