Skip to content

Commit 77eacb5

Browse files
committed
[JS] Add code sample for newTab/newWindow
1 parent 0852631 commit 77eacb5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

examples/javascript/test/interactions/windows.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ describe('Interactions - Windows', function () {
6060
base64Code = encodedString.slice(startIndex, endIndex)
6161
assert.strictEqual(base64Code, imgMagicNumber)
6262
});
63+
64+
it('Should be able to switch to newWindow and newTab and close', async function () {
65+
await driver.get('https://www.selenium.dev/selenium/web/');
66+
const initialWindow = await driver.getAllWindowHandles();
67+
assert.strictEqual(initialWindow.length, 1)
68+
69+
// Opens a new tab and switches to new tab
70+
await driver.switchTo().newWindow('tab');
71+
const browserTabs = await driver.getAllWindowHandles();
72+
assert.strictEqual(browserTabs.length, 2)
73+
74+
// Opens a new window and switches to new window
75+
await driver.switchTo().newWindow('window');
76+
const windows = await driver.getAllWindowHandles();
77+
assert.strictEqual(windows.length, 3)
78+
79+
//Close the tab or window
80+
await driver.close();
81+
82+
//Switch back to the old tab or window
83+
await driver.switchTo().window(windows[1]);
84+
85+
const windowsAfterClose = await driver.getAllWindowHandles();
86+
assert.strictEqual(windowsAfterClose.length, 2);
87+
});
6388
});

0 commit comments

Comments
 (0)