How to use switchWindow method in Webdriverio

Best JavaScript code snippet using webdriverio-monorepo

Run Webdriverio automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

linksTests.js

Source: linksTests.js Github

copy
1describe('testing links', () => {
2  before((browser) => {
3    browser
4      .url('http://localhost:8080')
5      .windowMaximize('current') // maximize window
6      .waitForElementVisible('#custom-button-color') // wait for login button to load
7      .click('#custom-button-color') // click that button
8      .assert.visible('img.auth0-lock-header-logo') // check that the case logo is there in auth0
9      .assert.visible('div.auth0-lock-content') // check that the auth0 login button is visible
10      .click('div.auth0-lock-social-button-text') // click the button
11      .waitForElementVisible('input#identifierId') // wait for email box to be visible
12      .setValue('input#identifierId', process.env.VUE_APP_AUTH0_EMAIL) // input your email
13      .click('button.VfPpkd-LgbsSe') // click continue button
14      .waitForElementVisible('#password') // wait for password field to load
15      .setValue('input[type="password"]', process.env.VUE_APP_AUTH0_PASSWORD) // input your password
16      .click('button.VfPpkd-LgbsSe') // click login button
17      .waitForElementVisible('h1.d-inline'); // wait for header text 'Case Portal'
18  });
19
20  this.tags = ['links'];
21
22  it('Test Link for basecamp in dropdown menu', (browser) => {
23    browser
24      .waitForElementVisible('#links-btn') //waiting for Link dropdown button to render
25      .click('#links-btn')
26      .waitForElementVisible('a[id="https://3.basecamp.com/3097063"]')
27      .click('a[id="https://3.basecamp.com/3097063"]') //clicks Basecamp link
28      .windowHandles(function (result) {
29        //switches window
30        browser.switchWindow(result.value[1]);
31        browser.assert.urlEquals('https://launchpad.37signals.com/bc3/3078589/signin');
32        browser.closeWindow();
33        browser.switchWindow(result.value[0]);
34        browser.assert.urlEquals('http://localhost:8080/reimbursements');
35      });
36  });
37
38  it('Test Link for Fidelity in dropdown menu', (browser) => {
39    browser
40      .waitForElementVisible('#links-btn')
41      .click('#links-btn')
42      .waitForElementVisible('a[id="https://nb.fidelity.com/public/nb/default/home"]')
43      .click('a[id="https://nb.fidelity.com/public/nb/default/home"]')
44      .windowHandles(function (result) {
45        browser.switchWindow(result.value[1]);
46        browser.assert.urlEquals('https://nb.fidelity.com/public/nb/default/home');
47        browser.closeWindow();
48        browser.switchWindow(result.value[0]);
49        browser.assert.urlEquals('http://localhost:8080/reimbursements');
50      });
51  });
52
53  it('Test Link for health insurance dropdown menu', (browser) => {
54    browser
55      .waitForElementVisible('#links-btn')
56      .click('#links-btn')
57      .waitForElementVisible('a[id="https://3.basecamp.com/3097063/buckets/179119/messages/2306027830"]')
58      .click('a[id="https://3.basecamp.com/3097063/buckets/179119/messages/2306027830"]')
59      .windowHandles(function (result) {
60        browser.switchWindow(result.value[1]);
61        browser.assert.urlEquals('https://launchpad.37signals.com/bc3/3078589/signin');
62        browser.closeWindow();
63        browser.switchWindow(result.value[0]);
64        browser.assert.urlEquals('http://localhost:8080/reimbursements');
65      });
66  });
67
68  it('Test Link for QuickBooks Time dropdown menu', (browser) => {
69    browser
70      .waitForElementVisible('#links-btn')
71      .click('#links-btn')
72      .waitForElementVisible('a[id="https://tsheets.intuit.com/page/login_oii"]')
73      .click('a[id="https://tsheets.intuit.com/page/login_oii"]')
74      .windowHandles(function (result) {
75        browser.switchWindow(result.value[1]);
76        browser.assert.urlEquals('https://tsheets.intuit.com/page/login_oii');
77        browser.closeWindow();
78        browser.switchWindow(result.value[0]);
79        browser.assert.urlEquals('http://localhost:8080/reimbursements');
80      })
81      .pause(400);
82  });
83
84  it('Test Link for ADP dropdown menu', (browser) => {
85    browser
86      .waitForElementVisible('#links-btn')
87      .click('#links-btn')
88      .waitForElementVisible('a[id="https://my.adp.com/"]')
89      .click('a[id="https://my.adp.com/"]')
90      .windowHandles(function (result) {
91        browser.switchWindow(result.value[1]);
92        browser.assert.urlEquals(
93          'https://online.adp.com/signin/v1/?APPID=RDBX&productId=80e309c3-70c6-bae1-e053-3505430b5495&returnURL=https://my.adp.com/&callingAppId=RDBX&TARGET=-SM-https://my.adp.com/static/redbox/'
94        );
95        browser.closeWindow();
96        browser.switchWindow(result.value[0]);
97        browser.assert.urlEquals('http://localhost:8080/reimbursements');
98      })
99      .pause(400);
100  });
101
102  it('Test Link for Life Insurance dropdown menu', (browser) => {
103    browser
104      .waitForElementVisible('#links-btn')
105      .click('#links-btn')
106      .waitForElementVisible('a[id="https://www.reliancestandard.com/home/"]')
107      .click('a[id="https://www.reliancestandard.com/home/"]')
108      .windowHandles(function (result) {
109        browser.switchWindow(result.value[1]);
110        browser.assert.urlEquals('https://www.reliancestandard.com/home/');
111        browser.closeWindow();
112        browser.switchWindow(result.value[0]);
113        browser.assert.urlEquals('http://localhost:8080/reimbursements');
114      });
115  });
116
117  it('Test Link for Red Mine dropdown menu', (browser) => {
118    browser
119      .waitForElementVisible('#links-btn')
120      .click('#links-btn')
121      .waitForElementVisible('a[id="https://redmine.consultwithcase.com"]')
122      .click('a[id="https://redmine.consultwithcase.com"]')
123      .windowHandles(function (result) {
124        browser.switchWindow(result.value[1]);
125        browser.assert.urlEquals(
126          'https://redmine.consultwithcase.com/login?back_url=https%3A%2F%2Fredmine.consultwithcase.com%2F'
127        );
128        browser.closeWindow();
129        browser.switchWindow(result.value[0]);
130        browser.assert.urlEquals('http://localhost:8080/reimbursements');
131      });
132  });
133
134  it('Test Link for Workspaces at Reston Town Center dropdown menu', (browser) => {
135    browser
136      .waitForElementVisible('#links-btn')
137      .click('#links-btn')
138      .waitForElementVisible('#floorPlan')
139      .click('#floorPlan')
140      .windowHandles(function (result) {
141        browser.switchWindow(result.value[1]);
142        browser.assert.urlEquals('http://localhost:8080/img/MakeOfficesfloorplan.0d9c2602.jpg');
143        browser.closeWindow();
144        browser.switchWindow(result.value[0]);
145        browser.assert.urlEquals('http://localhost:8080/reimbursements');
146      });
147  });
148
149  it('Test Link for GitHub', (browser) => {
150    browser
151      .waitForElementVisible('#links-btn')
152      .click('#Github')
153      .windowHandles(function (result) {
154        browser.switchWindow(result.value[1]);
155        browser.assert.urlEquals('https://github.com/caseconsulting');
156        browser.closeWindow();
157        browser.switchWindow(result.value[0]);
158        browser.assert.urlEquals('http://localhost:8080/reimbursements');
159      });
160  });
161
162  it('Test Link for LinkedIn', (browser) => {
163    browser
164      .waitForElementVisible('#links-btn')
165      .click('#LinkedIn')
166      .windowHandles(function (result) {
167        browser.switchWindow(result.value[1]);
168        browser.assert.urlContains('case-consulting-inc');
169        browser.closeWindow();
170        browser.switchWindow(result.value[0]);
171        browser.assert.urlEquals('http://localhost:8080/reimbursements');
172      });
173  });
174
175  it('Test Link for Youtube', (browser) => {
176    browser
177      .waitForElementVisible('#links-btn')
178      .click('#Youtube')
179      .windowHandles(function (result) {
180        browser.switchWindow(result.value[1]);
181        browser.assert.urlEquals('https://www.youtube.com/channel/UC_oJY4OrOpLNrIBAN7Y-9fA');
182        browser.closeWindow();
183        browser.switchWindow(result.value[0]);
184        browser.assert.urlEquals('http://localhost:8080/reimbursements');
185      });
186  });
187
188  it('Test Link for Twitter', (browser) => {
189    browser
190      .waitForElementVisible('#links-btn')
191      .click('#Twitter')
192      .windowHandles(function (result) {
193        browser.switchWindow(result.value[1]);
194        browser.assert.urlEquals('https://twitter.com/consultwithcase?lang=en');
195        browser.closeWindow();
196        browser.switchWindow(result.value[0]);
197        browser.assert.urlEquals('http://localhost:8080/reimbursements');
198      });
199  });
200
201  it('Test Link for Facebook', (browser) => {
202    browser
203      .waitForElementVisible('#links-btn')
204      .click('#Facebook')
205      .windowHandles(function (result) {
206        browser.switchWindow(result.value[1]);
207        browser.assert.urlEquals('https://www.facebook.com/login/?next=https%3A%2F%2Fwww.facebook.com%2FConsultwithCase%2F');
208        browser.closeWindow();
209        browser.switchWindow(result.value[0]);
210        browser.assert.urlEquals('http://localhost:8080/reimbursements');
211      });
212  });
213});
214
Full Screen

popup-spec.js

Source: popup-spec.js Github

copy
1describe("Popup Events", () => {
2  beforeEach(() => {
3    browser.url("http://localhost:3099");
4  });
5
6  it("should be able to receive events from opener frames", () => {
7    const expected = "hello from frame3!";
8
9    $("#open-popup").click();
10    browser.switchWindow("popup");
11    browser.waitUntil(
12      () => {
13        return $("body").getHTML() != null;
14      },
15      {
16        timeout: 1000,
17        timeoutMsg: "expected body to exist",
18      }
19    );
20
21    browser.switchWindow("localhost:3099");
22
23    browser.switchToFrame(2);
24
25    $("#popup-message").setValue(expected);
26    $("#send").click();
27
28    browser.switchWindow("popup");
29
30    browser.waitUntil(
31      () => {
32        return $("p").getHTML !== "";
33      },
34      {
35        timeout: 1000,
36      }
37    );
38    const actual = $("p").getText();
39
40    expect($$("p").length).toBe(1);
41    expect(actual).toBe(expected);
42  });
43
44  it("should be able to send events to opener frames", () => {
45    const expected = "hello from popup!";
46
47    $("#open-popup").click();
48
49    browser.switchWindow("popup");
50    browser.waitUntil(
51      () => {
52        return $("body").getHTML() != null;
53      },
54      {
55        timeout: 1000,
56        timeoutMsg: "expected body to exist",
57      }
58    );
59
60    $("#from-popup-message").setValue(expected);
61    $("#send").click();
62
63    browser.switchWindow("localhost:3099");
64    browser.switchToFrame(1);
65
66    browser.waitUntil(
67      () => {
68        return $("p").getHTML !== "";
69      },
70      {
71        timeout: 1000,
72      }
73    );
74    const actual = $("p").getText();
75
76    expect($$("p").length).toBe(1);
77    expect(actual).toContain(expected);
78  });
79
80  it("should not double-receive events in popups", () => {
81    const expected = "hello from popup!";
82
83    $("#open-popup").click();
84
85    browser.switchWindow("popup");
86    browser.waitUntil(
87      () => {
88        return $("body").getHTML() != null;
89      },
90      {
91        timeout: 1000,
92        timeoutMsg: "expected body to exist",
93      }
94    );
95
96    $("#from-popup-message").setValue(expected);
97    $("#send").click();
98
99    browser.switchWindow("localhost:3099");
100    browser.switchToFrame(1);
101
102    browser.waitUntil(
103      () => {
104        return $("p").getHTML !== "";
105      },
106      {
107        timeout: 1000,
108      }
109    );
110    const actual = $("p").getText();
111
112    expect($$("p").length).toBe(1);
113    expect(actual).not.toContain("FAILURE");
114  });
115
116  it("should be able to receive messages from opener window", () => {
117    const expected = "hello from opener!";
118
119    $("#open-popup").click();
120    browser.switchWindow("popup");
121    browser.waitUntil(
122      () => {
123        return $("body").getHTML() != null;
124      },
125      {
126        timeout: 1000,
127        timeoutMsg: "expected body to exist",
128      }
129    );
130
131    browser.switchWindow("localhost:3099");
132    $("#from-top-message").setValue(expected);
133    $("#send-from-top").click();
134
135    browser.switchWindow("popup");
136    browser.waitUntil(
137      () => {
138        return $("p").getHTML !== "";
139      },
140      {
141        timeout: 1000,
142      }
143    );
144    const actual = $("p").getText();
145
146    expect($$("p").length).toBe(1);
147    expect(actual).not.toContain("FAILURE");
148  });
149});
150
Full Screen

switchWindow.test.js

Source: switchWindow.test.js Github

copy
1import request from 'request'
2import { remote } from '../../../src'
3
4describe('switchWindow', () => {
5    let browser
6
7    beforeEach(async () => {
8        request.setMockResponse()
9        browser = await remote({
10            baseUrl: 'http://foobar.com',
11            capabilities: {
12                browserName: 'foobar'
13            }
14        })
15    })
16
17    it('should allow to switch to a window handle', async () => {
18        const tabId = await browser.switchWindow('webdriver')
19        expect(tabId).toBe('window-handle-1')
20    })
21
22    it('should iterate over all available handles to find the right window', async () => {
23        request.setMockResponse([null, null, 'foo', 'bar', null, 'hello', 'world', null, 'some', 'url'])
24        const tabId = await browser.switchWindow('so')
25        expect(tabId).toBe('window-handle-3')
26        request.setMockResponse([null, null, 'foo', 'bar', null, 'hello', 'world', null, 'some', 'url'])
27        const otherTabId = await browser.switchWindow(/h(e|a)llo/)
28        expect(otherTabId).toBe('window-handle-2')
29        request.setMockResponse([null, null, 'foo', 'bar', null, 'hello', 'world', null, 'some', 'url'])
30        const anotherTabId = await browser.switchWindow('world')
31        expect(anotherTabId).toBe('window-handle-2')
32    })
33
34    it('should fail if no window was found', async () => {
35        expect.hasAssertions()
36        request.setMockResponse([null, null, 'foo', 'bar', null, 'hello', 'world', null, 'some', 'url'])
37
38        try {
39            await browser.switchWindow('foobar')
40        } catch (e) {
41            expect(e.message).toContain('No window found')
42        }
43    })
44
45    it('should fail if parameter is not valid', async () => {
46        expect.hasAssertions()
47
48        try {
49            await browser.switchWindow(true)
50        } catch (e) {
51            expect(e.message).toContain('Unsupported parameter')
52        }
53    })
54})
55
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Webdriverio on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)