How to use getWindowHandles 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.

register.UI.js

Source: register.UI.js Github

copy
1const LoginPage = require('../../pageobjects/login.page');
2const UserInviteUserPage = require('../../pageobjects/userInviteUser.page');
3const TestUtil = require('../../utility/testUtil');
4const ElementUtil = require('../../utility/elementUtil')
5const RegisterPage = require('../../pageobjects/register.page');
6const RegisterUserData = require('../../testData/registerUser.data');
7
8describe('GRS Register page functionality',()=>{
9
10    it('Should navigate to registration screen with name and email are prefilled as entered by admin',()=>{
11        //1. sent invite to user via invite user from app.
12        //Open App
13        LoginPage.open();
14
15        //wait till Login page is loaded
16        LoginPage.waitTillPageLoadComplete();
17
18        //login as Admin
19        LoginPage.loginAsAdmin();
20
21        //navigate to user invite page
22        LoginPage.navigateToUserInviteUsers();
23
24        //wait till User Invite page is loaded
25        UserInviteUserPage.waitTillPageLoadComplete();
26
27        //create new email address
28        let emailAccount = TestUtil.createNewEmailAccount();
29
30        //create invite for new user by super admin
31        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
32
33        //click on send button
34        UserInviteUserPage.clickSendInvitationButton();
35
36        //logout as Super Admin from whom invite is send
37        LoginPage.logOutFromApplication();
38
39        //2. click on register link over email
40        TestUtil.clickRegisterLinkInEmail();
41
42        //close first two windows
43        browser.switchToWindow(browser.getWindowHandles()[0]);
44        browser.closeWindow();
45        browser.switchToWindow(browser.getWindowHandles()[0]);
46        browser.closeWindow();
47        browser.switchToWindow(browser.getWindowHandles()[0]);
48
49        //verify name and email is filled by Admin
50        expect(RegisterPage.inputRegisterUserName.getAttribute('value')).to.be.equal('AutomationTesting'+ emailAccount.replace('@mailinator.com',""));
51        expect(RegisterPage.inputRegisterUserEmail.getAttribute('value')).to.be.equal(emailAccount);
52    });
53
54    it('Should display error message for mandatory fields Name, Email and Password',()=>{
55        //1. sent invite to user via invite user from app.
56        //Open App
57        LoginPage.open();
58
59        //wait till Login page is loaded
60        LoginPage.waitTillPageLoadComplete();
61
62        //login as Admin
63        LoginPage.loginAsAdmin();
64
65        //navigate to user invite page
66        LoginPage.navigateToUserInviteUsers();
67
68        //wait till User Invite page is loaded
69        UserInviteUserPage.waitTillPageLoadComplete();
70
71        //create new email address
72        let emailAccount = TestUtil.createNewEmailAccount();
73
74        //create invite for new user by super admin
75        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
76
77        //click on send button
78        UserInviteUserPage.clickSendInvitationButton();
79
80        //logout as Super Admin from whom invite is send
81        LoginPage.logOutFromApplication();
82
83        //2. click on register link over email
84        TestUtil.clickRegisterLinkInEmail();
85
86        //close first two windows
87        browser.switchToWindow(browser.getWindowHandles()[0]);
88        browser.closeWindow();
89        browser.switchToWindow(browser.getWindowHandles()[0]);
90        browser.closeWindow();
91        browser.switchToWindow(browser.getWindowHandles()[0]);
92
93        //clear email and name values
94        RegisterPage.clearRegisterUserNameEmail();
95
96        //3. try to submit without mandatory fields.
97        RegisterPage.clickAcceptInviteSaveButton();
98
99        //verify error message for Name, Email and Password
100        expect(RegisterPage.labelRegisterUserNameError.getText()).to.be.equal('The name field is required.');
101        expect(RegisterPage.labelRegisterUserEmailError.getText()).to.be.equal('The email field is required.');
102        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal('Password is required.');
103    });
104
105    it('Should display error message for Invalid Email format',()=>{
106           //1. sent invite to user via invite user from app.
107        //Open App
108        LoginPage.open();
109
110        //wait till Login page is loaded
111        LoginPage.waitTillPageLoadComplete();
112
113        //login as Admin
114        LoginPage.loginAsAdmin();
115
116        //navigate to user invite page
117        LoginPage.navigateToUserInviteUsers();
118
119        //wait till User Invite page is loaded
120        UserInviteUserPage.waitTillPageLoadComplete();
121
122        //create new email address
123        let emailAccount = TestUtil.createNewEmailAccount();
124
125        //create invite for new user by super admin
126        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
127
128        //click on send button
129        UserInviteUserPage.clickSendInvitationButton();
130
131        //logout as Super Admin from whom invite is send
132        LoginPage.logOutFromApplication();
133
134        //2. click on register link over email
135        TestUtil.clickRegisterLinkInEmail();
136
137        //close first two windows
138        browser.switchToWindow(browser.getWindowHandles()[0]);
139        browser.closeWindow();
140        browser.switchToWindow(browser.getWindowHandles()[0]);
141        browser.closeWindow();
142        browser.switchToWindow(browser.getWindowHandles()[0]);
143
144        //enter invalid email format
145        RegisterPage.updateRegisterUserWithInvalidEmail();
146
147        //click on send button
148        RegisterPage.clickAcceptInviteSaveButton();
149
150        //verify error message for invalid email format
151        expect(RegisterPage.labelRegisterUserEmailError.getText()).to.be.equal('The email must be a valid email address.');
152    });
153
154    it('Should display error message for Already Registered Email',()=>{
155        //1. sent invite to user via invite user from app.
156        //Open App
157        LoginPage.open();
158
159        //wait till Login page is loaded
160        LoginPage.waitTillPageLoadComplete();
161
162        //login as Admin
163        LoginPage.loginAsAdmin();
164
165        //navigate to user invite page
166        LoginPage.navigateToUserInviteUsers();
167
168        //wait till User Invite page is loaded
169        UserInviteUserPage.waitTillPageLoadComplete();
170
171        //create new email address
172        let emailAccount = TestUtil.createNewEmailAccount();
173
174        //create invite for new user by super admin
175        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
176
177        //click on send button
178        UserInviteUserPage.clickSendInvitationButton();
179
180        //logout as Super Admin from whom invite is send
181        LoginPage.logOutFromApplication();
182
183        //2. click on register link over email
184        TestUtil.clickRegisterLinkInEmail();
185
186        //close first two windows
187        browser.switchToWindow(browser.getWindowHandles()[0]);
188        browser.closeWindow();
189        browser.switchToWindow(browser.getWindowHandles()[0]);
190        browser.closeWindow();
191        browser.switchToWindow(browser.getWindowHandles()[0]);
192
193        //enter invalid email format
194        RegisterPage.updateRegisterUserWithRegisteredEmail();
195
196        //click on send button
197        RegisterPage.clickAcceptInviteSaveButton();
198
199        //verify error message for invalid email format
200        expect(RegisterPage.labelRegisterUserEmailError.getText()).to.be.equal('The email has already been taken.');
201    });
202
203    it('Should display an error message when password length is less than 8 characters',()=>{
204                //1. sent invite to user via invite user from app.
205        //Open App
206        LoginPage.open();
207
208        //wait till Login page is loaded
209        LoginPage.waitTillPageLoadComplete();
210
211        //login as Admin
212        LoginPage.loginAsAdmin();
213
214        //navigate to user invite page
215        LoginPage.navigateToUserInviteUsers();
216
217        //wait till User Invite page is loaded
218        UserInviteUserPage.waitTillPageLoadComplete();
219
220        //create new email address
221        let emailAccount = TestUtil.createNewEmailAccount();
222
223        //create invite for new user by super admin
224        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
225
226        //click on send button
227        UserInviteUserPage.clickSendInvitationButton();
228
229        //logout as Super Admin from whom invite is send
230        LoginPage.logOutFromApplication();
231
232        //2. click on register link over email
233        TestUtil.clickRegisterLinkInEmail();
234
235        //close first two windows
236        browser.switchToWindow(browser.getWindowHandles()[0]);
237        browser.closeWindow();
238        browser.switchToWindow(browser.getWindowHandles()[0]);
239        browser.closeWindow();
240        browser.switchToWindow(browser.getWindowHandles()[0]);
241
242        //enter password with 5 character length
243        RegisterPage.enterRegisterUserPasswordWithLess8CharacterLength();
244
245        //click on send button
246        RegisterPage.clickAcceptInviteSaveButton();
247
248        //verify error message for invalid email format
249        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal('Password must be at least 8 characters in length.');
250    });
251
252    it('Should display an error message when password without lower characters',()=>{
253        //1. sent invite to user via invite user from app.
254        //Open App
255        LoginPage.open();
256
257        //wait till Login page is loaded
258        LoginPage.waitTillPageLoadComplete();
259
260        //login as Admin
261        LoginPage.loginAsAdmin();
262
263        //navigate to user invite page
264        LoginPage.navigateToUserInviteUsers();
265
266        //wait till User Invite page is loaded
267        UserInviteUserPage.waitTillPageLoadComplete();
268
269        //create new email address
270        let emailAccount = TestUtil.createNewEmailAccount();
271
272        //create invite for new user by super admin
273        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
274
275        //click on send button
276        UserInviteUserPage.clickSendInvitationButton();
277
278        //logout as Super Admin from whom invite is send
279        LoginPage.logOutFromApplication();
280
281        //2. click on register link over email
282        TestUtil.clickRegisterLinkInEmail();
283
284        //close first two windows
285        browser.switchToWindow(browser.getWindowHandles()[0]);
286        browser.closeWindow();
287        browser.switchToWindow(browser.getWindowHandles()[0]);
288        browser.closeWindow();
289        browser.switchToWindow(browser.getWindowHandles()[0]);
290
291        //enter password with 5 character length
292        RegisterPage.enterRegisterUserPasswordWithoutLowerCharacters();
293
294        //click on send button
295        RegisterPage.clickAcceptInviteSaveButton();
296
297        //verify error message for invalid email format
298        let expectedOutput = 'Password be a combination of each of the following: lowercase letter, uppercase letter, number, and a special character (ex: ! @ # $ % ^ & * ).';
299        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal(expectedOutput);
300    });
301
302    it('Should display an error message when password without upper characters',()=>{
303        //1. sent invite to user via invite user from app.
304        //Open App
305        LoginPage.open();
306
307        //wait till Login page is loaded
308        LoginPage.waitTillPageLoadComplete();
309
310        //login as Admin
311        LoginPage.loginAsAdmin();
312
313        //navigate to user invite page
314        LoginPage.navigateToUserInviteUsers();
315
316        //wait till User Invite page is loaded
317        UserInviteUserPage.waitTillPageLoadComplete();
318
319        //create new email address
320        let emailAccount = TestUtil.createNewEmailAccount();
321
322        //create invite for new user by super admin
323        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
324
325        //click on send button
326        UserInviteUserPage.clickSendInvitationButton();
327
328        //logout as Super Admin from whom invite is send
329        LoginPage.logOutFromApplication();
330
331        //2. click on register link over email
332        TestUtil.clickRegisterLinkInEmail();
333
334        //close first two windows
335        browser.switchToWindow(browser.getWindowHandles()[0]);
336        browser.closeWindow();
337        browser.switchToWindow(browser.getWindowHandles()[0]);
338        browser.closeWindow();
339        browser.switchToWindow(browser.getWindowHandles()[0]);
340
341        //enter password with 5 character length
342        RegisterPage.enterRegisterUserPasswordWithoutUpperCharacters();
343
344        //click on send button
345        RegisterPage.clickAcceptInviteSaveButton();
346
347        //verify error message for invalid email format
348        let expectedOutput = 'Password be a combination of each of the following: lowercase letter, uppercase letter, number, and a special character (ex: ! @ # $ % ^ & * ).';
349        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal(expectedOutput);
350    });
351
352    it('Should display an error message when password without special characters',()=>{
353        //1. sent invite to user via invite user from app.
354        //Open App
355        LoginPage.open();
356
357        //wait till Login page is loaded
358        LoginPage.waitTillPageLoadComplete();
359
360        //login as Admin
361        LoginPage.loginAsAdmin();
362
363        //navigate to user invite page
364        LoginPage.navigateToUserInviteUsers();
365
366        //wait till User Invite page is loaded
367        UserInviteUserPage.waitTillPageLoadComplete();
368
369        //create new email address
370        let emailAccount = TestUtil.createNewEmailAccount();
371
372        //create invite for new user by super admin
373        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
374
375        //click on send button
376        UserInviteUserPage.clickSendInvitationButton();
377
378        //logout as Super Admin from whom invite is send
379        LoginPage.logOutFromApplication();
380
381        //2. click on register link over email
382        TestUtil.clickRegisterLinkInEmail();
383
384        //close first two windows
385        browser.switchToWindow(browser.getWindowHandles()[0]);
386        browser.closeWindow();
387        browser.switchToWindow(browser.getWindowHandles()[0]);
388        browser.closeWindow();
389        browser.switchToWindow(browser.getWindowHandles()[0]);
390
391        //enter password with 5 character length
392        RegisterPage.enterRegisterUserPasswordWithoutSpecialCharacters();
393
394        //click on send button
395        RegisterPage.clickAcceptInviteSaveButton();
396
397        //verify error message for invalid email format
398        let expectedOutput = 'Password be a combination of each of the following: lowercase letter, uppercase letter, number, and a special character (ex: ! @ # $ % ^ & * ).';
399        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal(expectedOutput);
400    });
401
402    it('Should display an error message when password without number',()=>{
403        //1. sent invite to user via invite user from app.
404        //Open App
405        LoginPage.open();
406
407        //wait till Login page is loaded
408        LoginPage.waitTillPageLoadComplete();
409
410        //login as Admin
411        LoginPage.loginAsAdmin();
412
413        //navigate to user invite page
414        LoginPage.navigateToUserInviteUsers();
415
416        //wait till User Invite page is loaded
417        UserInviteUserPage.waitTillPageLoadComplete();
418
419        //create new email address
420        let emailAccount = TestUtil.createNewEmailAccount();
421
422        //create invite for new user by super admin
423        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
424
425        //click on send button
426        UserInviteUserPage.clickSendInvitationButton();
427
428        //logout as Super Admin from whom invite is send
429        LoginPage.logOutFromApplication();
430
431        //2. click on register link over email
432        TestUtil.clickRegisterLinkInEmail();
433
434        //close first two windows
435        browser.switchToWindow(browser.getWindowHandles()[0]);
436        browser.closeWindow();
437        browser.switchToWindow(browser.getWindowHandles()[0]);
438        browser.closeWindow();
439        browser.switchToWindow(browser.getWindowHandles()[0]);
440
441        //enter password with 5 character length
442        RegisterPage.enterRegisterUserPasswordWithoutNumbers();
443
444        //click on send button
445        RegisterPage.clickAcceptInviteSaveButton();
446
447        //verify error message for invalid email format
448        let expectedOutput = 'Password be a combination of each of the following: lowercase letter, uppercase letter, number, and a special character (ex: ! @ # $ % ^ & * ).';
449        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal(expectedOutput);
450    });
451
452    it('Should display an error message when confirm password is not match with password',()=>{
453        //1. sent invite to user via invite user from app.
454        //Open App
455        LoginPage.open();
456
457        //wait till Login page is loaded
458        LoginPage.waitTillPageLoadComplete();
459
460        //login as Admin
461        LoginPage.loginAsAdmin();
462
463        //navigate to user invite page
464        LoginPage.navigateToUserInviteUsers();
465
466        //wait till User Invite page is loaded
467        UserInviteUserPage.waitTillPageLoadComplete();
468
469        //create new email address
470        let emailAccount = TestUtil.createNewEmailAccount();
471
472        //create invite for new user by super admin
473        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
474
475        //click on send button
476        UserInviteUserPage.clickSendInvitationButton();
477
478        //logout as Super Admin from whom invite is send
479        LoginPage.logOutFromApplication();
480
481        //2. click on register link over email
482        TestUtil.clickRegisterLinkInEmail();
483
484        //close first two windows
485        browser.switchToWindow(browser.getWindowHandles()[0]);
486        browser.closeWindow();
487        browser.switchToWindow(browser.getWindowHandles()[0]);
488        browser.closeWindow();
489        browser.switchToWindow(browser.getWindowHandles()[0]);
490
491        //set value for Password 
492        RegisterPage.enterRegisterUserPassword(RegisterUserData.newRegisterUser.password);
493
494        //click on save button
495        RegisterPage.clickAcceptInviteSaveButton();
496
497        //verify error message for Confirm Password
498        expect(RegisterPage.labelRegisterUserPasswordError.getText()).to.be.equal('The password confirmation does not match.');
499    });
500
501
502    it('Should display an error message for Invalid zip code',()=>{
503        //1. sent invite to user via invite user from app.
504        //Open App
505        LoginPage.open();
506
507        //wait till Login page is loaded
508        LoginPage.waitTillPageLoadComplete();
509
510        //login as Admin
511        LoginPage.loginAsAdmin();
512
513        //navigate to user invite page
514        LoginPage.navigateToUserInviteUsers();
515
516        //wait till User Invite page is loaded
517        UserInviteUserPage.waitTillPageLoadComplete();
518
519        //create new email address
520        let emailAccount = TestUtil.createNewEmailAccount();
521
522        //create invite for new user by super admin
523        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
524
525        //click on send button
526        UserInviteUserPage.clickSendInvitationButton();
527
528        //logout as Super Admin from whom invite is send
529        LoginPage.logOutFromApplication();
530
531        //2. click on register link over email
532        TestUtil.clickRegisterLinkInEmail();
533
534        //close first two windows
535        browser.switchToWindow(browser.getWindowHandles()[0]);
536        browser.closeWindow();
537        browser.switchToWindow(browser.getWindowHandles()[0]);
538        browser.closeWindow();
539        browser.switchToWindow(browser.getWindowHandles()[0]);
540
541        //fill new user details
542        RegisterPage.enterNewRegisterUserDetails();
543
544        //enter invalid Zip code
545        RegisterPage.enterRegisterUserInvalidZipCode();
546
547        //click on save button
548        RegisterPage.clickAcceptInviteSaveButton();
549
550        //verify error message for Confirm Password
551        expect(RegisterPage.inputRegisterUserZipError.getText()).to.be.equal('The zip format is invalid.');
552    });
553
554    it('Should be able to register successfully and navigated to Login page',()=>{
555        //1. sent invite to user via invite user from app.
556        //Open App
557        LoginPage.open();
558
559        //wait till Login page is loaded
560        LoginPage.waitTillPageLoadComplete();
561
562        //login as Admin
563        LoginPage.loginAsAdmin();
564
565        //navigate to user invite page
566        LoginPage.navigateToUserInviteUsers();
567
568        //wait till User Invite page is loaded
569        UserInviteUserPage.waitTillPageLoadComplete();
570
571        //create new email address
572        let emailAccount = TestUtil.createNewEmailAccount();
573
574        //create invite for new user by super admin
575        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
576
577        //click on send button
578        UserInviteUserPage.clickSendInvitationButton();
579
580        //logout as Super Admin from whom invite is send
581        LoginPage.logOutFromApplication();
582
583        //2. click on register link over email
584        TestUtil.clickRegisterLinkInEmail();
585
586        //close first two windows
587        browser.switchToWindow(browser.getWindowHandles()[0]);
588        browser.closeWindow();
589        browser.switchToWindow(browser.getWindowHandles()[0]);
590        browser.closeWindow();
591        browser.switchToWindow(browser.getWindowHandles()[0]);
592
593        //fill new user details
594        RegisterPage.enterNewRegisterUserDetails();
595
596        //click on save button
597        RegisterPage.clickAcceptInviteSaveButton();
598
599        //verify error message for Confirm Password
600        expect(browser.getUrl()).to.have.string('/login');
601    });
602
603    it('Should be able to register successfully and navigated to Login page',()=>{
604        //1. sent invite to user via invite user from app.
605        //Open App
606        LoginPage.open();
607
608        //wait till Login page is loaded
609        LoginPage.waitTillPageLoadComplete();
610
611        //login as Admin
612        LoginPage.loginAsAdmin();
613
614        //navigate to user invite page
615        LoginPage.navigateToUserInviteUsers();
616
617        //wait till User Invite page is loaded
618        UserInviteUserPage.waitTillPageLoadComplete();
619
620        //create new email address
621        let emailAccount = TestUtil.createNewEmailAccount();
622
623        //create invite for new user by super admin
624        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
625
626        //click on send button
627        UserInviteUserPage.clickSendInvitationButton();
628
629        //logout as Super Admin from whom invite is send
630        LoginPage.logOutFromApplication();
631
632        //2. click on register link over email
633        TestUtil.clickRegisterLinkInEmail();
634
635        //close first two windows
636        browser.switchToWindow(browser.getWindowHandles()[0]);
637        browser.closeWindow();
638        browser.switchToWindow(browser.getWindowHandles()[0]);
639        browser.closeWindow();
640        browser.switchToWindow(browser.getWindowHandles()[0]);
641
642        //fill new user details
643        RegisterPage.enterNewRegisterUserDetails();
644
645        //click on save button
646        RegisterPage.clickAcceptInviteSaveButton();
647
648        //verify error message for Confirm Password
649        expect(browser.getUrl()).to.have.string('/login');
650    });
651
652    it('Should navigate to Invalid Link once user registered successfully',()=>{
653        //1. sent invite to user via invite user from app.
654        //Open App
655        LoginPage.open();
656
657        //wait till Login page is loaded
658        LoginPage.waitTillPageLoadComplete();
659
660        //login as Admin
661        LoginPage.loginAsAdmin();
662
663        //navigate to user invite page
664        LoginPage.navigateToUserInviteUsers();
665
666        //wait till User Invite page is loaded
667        UserInviteUserPage.waitTillPageLoadComplete();
668
669        //create new email address
670        let emailAccount = TestUtil.createNewEmailAccount();
671
672        //create invite for new user by super admin
673        UserInviteUserPage.inviteUserAsSuperAdmin(emailAccount);
674
675        //click on send button
676        UserInviteUserPage.clickSendInvitationButton();
677
678        //logout as Super Admin from whom invite is send
679        LoginPage.logOutFromApplication();
680
681        //2. click on register link over email
682        TestUtil.clickRegisterLinkInEmail();
683
684        //switch to send window
685        browser.switchToWindow(browser.getWindowHandles()[2]);
686
687        //fill new user details
688        RegisterPage.enterNewRegisterUserDetails();
689
690        //click on save button
691        RegisterPage.clickAcceptInviteSaveButton();
692
693        //wait till login page appear
694        LoginPage.waitTillPageLoadComplete();
695
696        //close browser
697        browser.closeWindow();
698
699        //move to second window
700        browser.switchToWindow(browser.getWindowHandles()[1]);
701
702        //back to mailbox
703        browser.back();
704
705        //click on Register link
706        TestUtil.clickRegisterLinkInEmail();
707
708        //verify error message for Confirm Password
709        expect(TestUtil.checkInvalidInvitationCode()).to.have.equal(true);
710
711        //switch to first browser
712        browser.switchToWindow(browser.getWindowHandles()[0]);
713    });    
714})
Full Screen

checkNewWindow.spec.js

Source: checkNewWindow.spec.js Github

copy
1import checkNewWindow from 'src/support/check/checkNewWindow';
2
3describe('checkNewWindow', () => {
4    let expectToEqual;
5    let expectToNotEqual;
6
7    beforeEach(() => {
8        global.browser = {
9            getWindowHandles: jest.fn(() => ({
10                value: ['window1'],
11            })),
12        };
13
14        expectToEqual = jest.fn();
15        expectToNotEqual = jest.fn();
16
17        global.expect = jest.fn(() => ({
18            not: {
19                toHaveLength: expectToNotEqual,
20            },
21            toHaveLength: expectToEqual,
22        }));
23    });
24
25    it('Should test if a new window is opened', () => {
26        checkNewWindow('', false);
27
28        _expect(global.browser.getWindowHandles).toHaveBeenCalledTimes(1);
29        _expect(global.browser.getWindowHandles).toHaveBeenCalledWith();
30
31        _expect(expectToNotEqual).toHaveBeenCalledTimes(1);
32        _expect(expectToNotEqual).toHaveBeenCalledWith(
33            1,
34            'A new window has been opened'
35        );
36    });
37
38    it('Should test if a new window is not opened', () => {
39        checkNewWindow('', true);
40
41        _expect(global.browser.getWindowHandles).toHaveBeenCalledTimes(1);
42        _expect(global.browser.getWindowHandles).toHaveBeenCalledWith();
43
44        _expect(expectToEqual).toHaveBeenCalledTimes(1);
45        _expect(expectToEqual).toHaveBeenCalledWith(
46            1,
47            'A new window should not have been opened'
48        );
49    });
50});
51
Full Screen

checkIsOpenedInNewWindow.spec.js

Source: checkIsOpenedInNewWindow.spec.js Github

copy
1import checkIsOpenedInNewWindow from
2    'src/support/check/checkIsOpenedInNewWindow';
3
4describe('checkIsOpenedInNewWindow', () => {
5    let expectLengthShouldNotEqual;
6    let expectShouldContain;
7
8    beforeEach(() => {
9        global.browser = {
10            getWindowHandles: jest.fn(() => ['window1', 'window2']),
11            switchToWindow: jest.fn(() => {
12                // foo
13            }),
14            getUrl: jest.fn(() => 'http://www.example.com/test'),
15            closeWindow: jest.fn(() => {
16                // foo
17            }),
18        };
19
20        expectLengthShouldNotEqual = jest.fn();
21        expectShouldContain = jest.fn();
22
23        global.expect = jest.fn(() => ({
24            not: {
25                toHaveLength: expectLengthShouldNotEqual,
26            },
27            toContain: expectShouldContain,
28        }));
29    });
30
31    it('Should fail if no popup was opened', () => {
32        global.expect = jest.fn(() => {
33            throw new Error();
34        });
35
36        global.browser.getWindowHandles.mockReturnValueOnce(['window1']);
37
38        try {
39            checkIsOpenedInNewWindow(
40                'http://www.example.com/test',
41                ''
42            );
43        } catch (e) {
44            _expect(e);
45        }
46
47        _expect(global.browser.getWindowHandles).toHaveBeenCalledTimes(1);
48        _expect(global.browser.getWindowHandles).toHaveBeenCalledWith();
49        _expect(global.browser.switchToWindow).not.toHaveBeenCalled();
50        _expect(global.browser.getUrl).not.toHaveBeenCalled();
51        _expect(global.browser.closeWindow).not.toHaveBeenCalled();
52        _expect(global.expect).toHaveBeenCalledTimes(1);
53        _expect(global.expect).toHaveBeenCalledWith(['window1']);
54
55        _expect(global.expect).toThrow();
56
57        _expect(expectLengthShouldNotEqual).not.toHaveBeenCalled();
58        _expect(expectShouldContain).not.toHaveBeenCalled();
59    });
60
61    it('Should not fail if the URL of the popup does match', () => {
62        checkIsOpenedInNewWindow('http://www.example.com/test', '');
63
64        _expect(global.browser.getWindowHandles).toHaveBeenCalledTimes(1);
65        _expect(global.browser.getWindowHandles).toHaveBeenCalledWith();
66        _expect(global.browser.switchToWindow).toHaveBeenCalledTimes(1);
67        _expect(global.browser.switchToWindow).toHaveBeenCalledWith('window2');
68        _expect(global.browser.getUrl).toHaveBeenCalledTimes(1);
69        _expect(global.browser.getUrl).toHaveBeenCalledWith();
70        _expect(global.browser.closeWindow).toHaveBeenCalledTimes(1);
71        _expect(global.browser.closeWindow).toHaveBeenCalledWith();
72
73        _expect(expectLengthShouldNotEqual).toHaveBeenCalledTimes(1);
74        _expect(expectShouldContain).toHaveBeenCalledTimes(1);
75        _expect(expectShouldContain).toHaveBeenCalledWith(
76            'http://www.example.com/test',
77            'The popup has a incorrect getUrl'
78        );
79    });
80});
81
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)