-
Notifications
You must be signed in to change notification settings - Fork 32
/
switchToTabWithUrl.js
48 lines (38 loc) · 1.55 KB
/
switchToTabWithUrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Transaction scripts can open new pages in new tabs/windows. This example shows how to switch to a new tab/window based on the
URL.
Author: primoz@thousandeyes.com
*/
import { By, Key } from 'selenium-webdriver';
import { driver } from 'thousandeyes';
runScript();
async function runScript() {
await driver.manage().setTimeouts({
implicit: 7 * 1000 // If an element is not found, reattempt for this many milliseconds
});
await driver.get('https://developer.cisco.com/docs/thousandeyes/');
await driver.findElement(By.xpath(`//a[text()='API Changelog']`)).click();
await openInNewTab(By.xpath(`//a[text()='Changelog']`));
await switchToTabWithUrl('https://docs.thousandeyes.com');
await driver.takeScreenshot();
await switchToTabWithUrl('https://developer.cisco.com/');
await driver.takeScreenshot();
}
async function openInNewTab(linkSelector) {
await driver.findElement(linkSelector).sendKeys(Key.CONTROL, Key.RETURN);
}
async function switchToTabWithUrl(url) {
const configuredTimeouts = await driver.manage().getTimeouts();
const attemptEndTime = Date.now() + configuredTimeouts.implicit;
while (Date.now() < attemptEndTime) {
for (const tab of await driver.getAllWindowHandles()) {
await driver.switchTo().window(tab);
const currentTabUrl = await driver.getCurrentUrl();
if (currentTabUrl.includes(url)) {
return;
}
}
await driver.sleep(200);
}
throw new Error('Could not find a tab with url: ' + url);
}