Skip to content

Commit 8c6acfe

Browse files
authored
Improve alert handling wdio (#4725)
* update to async/await style * update to async/await style
1 parent 24b9b9b commit 8c6acfe

File tree

2 files changed

+121
-146
lines changed

2 files changed

+121
-146
lines changed

lib/helper/WebDriver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ class WebDriver extends Helper {
619619
this.browser.capabilities.platformName = this.browser.capabilities.platformName.toLowerCase()
620620
}
621621

622+
this.browser.on('dialog', () => {})
622623
return this.browser
623624
}
624625

test/helper/WebDriver_test.js

Lines changed: 120 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe('WebDriver', function () {
5252
beforeEach(async () => {
5353
webApiTests.init({ I: wd, siteUrl })
5454
this.wdBrowser = await wd._before()
55-
this.wdBrowser.on('dialog', dialog => {})
5655
return this.wdBrowser
5756
})
5857

@@ -509,85 +508,73 @@ describe('WebDriver', function () {
509508
})
510509

511510
describe('#waitNumberOfVisibleElements', () => {
512-
it('should wait for a specified number of elements on the page', () => {
513-
return wd
514-
.amOnPage('/info')
515-
.then(() => wd.waitNumberOfVisibleElements('//div[@id = "grab-multiple"]//a', 3))
516-
.then(() => wd.waitNumberOfVisibleElements('//div[@id = "grab-multiple"]//a', 2, 0.1))
517-
.then(() => {
518-
throw Error('It should never get this far')
519-
})
520-
.catch(e => {
521-
e.message.should.include('The number of elements (//div[@id = "grab-multiple"]//a) is not 2 after 0.1 sec')
522-
})
511+
it('should wait for a specified number of elements on the page', async () => {
512+
try {
513+
await wd.amOnPage('/info')
514+
await wd.waitNumberOfVisibleElements('//div[@id = "grab-multiple"]//a', 3)
515+
await wd.waitNumberOfVisibleElements('//div[@id = "grab-multiple"]//a', 2, 0.1)
516+
throw new Error('It should never get this far')
517+
} catch (e) {
518+
e.message.should.include('The number of elements (//div[@id = "grab-multiple"]//a) is not 2 after 0.1 sec')
519+
}
523520
})
524521

525-
it('should be no [object Object] in the error message', () => {
526-
return wd
527-
.amOnPage('/info')
528-
.then(() => wd.waitNumberOfVisibleElements({ css: '//div[@id = "grab-multiple"]//a' }, 3))
529-
.then(() => {
530-
throw Error('It should never get this far')
531-
})
532-
.catch(e => {
533-
e.message.should.not.include('[object Object]')
534-
})
522+
it('should be no [object Object] in the error message', async () => {
523+
try {
524+
await wd.amOnPage('/info')
525+
await wd.waitNumberOfVisibleElements({ css: '//div[@id = "grab-multiple"]//a' }, 3)
526+
throw new Error('It should never get this far')
527+
} catch (e) {
528+
e.message.should.not.include('[object Object]')
529+
}
535530
})
536531

537-
it('should wait for a specified number of elements on the page using a css selector', () => {
538-
return wd
539-
.amOnPage('/info')
540-
.then(() => wd.waitNumberOfVisibleElements('#grab-multiple > a', 3))
541-
.then(() => wd.waitNumberOfVisibleElements('#grab-multiple > a', 2, 0.1))
542-
.then(() => {
543-
throw Error('It should never get this far')
544-
})
545-
.catch(e => {
546-
e.message.should.include('The number of elements (#grab-multiple > a) is not 2 after 0.1 sec')
547-
})
532+
it('should wait for a specified number of elements on the page using a css selector', async () => {
533+
try {
534+
await wd.amOnPage('/info')
535+
await wd.waitNumberOfVisibleElements('#grab-multiple > a', 3)
536+
await wd.waitNumberOfVisibleElements('#grab-multiple > a', 2, 0.1)
537+
throw new Error('It should never get this far')
538+
} catch (e) {
539+
e.message.should.include('The number of elements (#grab-multiple > a) is not 2 after 0.1 sec')
540+
}
548541
})
549542

550-
it('should wait for a specified number of elements which are not yet attached to the DOM', () => {
551-
return wd
552-
.amOnPage('/form/wait_num_elements')
553-
.then(() => wd.waitNumberOfVisibleElements('.title', 2, 3))
554-
.then(() => wd.see('Hello'))
555-
.then(() => wd.see('World'))
543+
it('should wait for a specified number of elements which are not yet attached to the DOM', async () => {
544+
await wd.amOnPage('/form/wait_num_elements')
545+
await wd.waitNumberOfVisibleElements('.title', 2, 3)
546+
await wd.see('Hello')
547+
await wd.see('World')
556548
})
557549
})
558550

559551
describe('#waitForVisible', () => {
560-
it('should be no [object Object] in the error message', () => {
561-
return wd
562-
.amOnPage('/info')
563-
.then(() => wd.waitForVisible('//div[@id = "grab-multiple"]//a', 3))
564-
.then(() => {
565-
throw Error('It should never get this far')
566-
})
567-
.catch(e => {
568-
e.message.should.not.include('[object Object]')
569-
})
552+
it('should be no [object Object] in the error message', async () => {
553+
try {
554+
await wd.amOnPage('/info')
555+
await wd.waitForVisible('//div[@id = "grab-multiple"]//a', 3)
556+
throw new Error('It should never get this far')
557+
} catch (e) {
558+
e.message.should.not.include('[object Object]')
559+
}
570560
})
571561
})
572562

573563
describe('#waitForInvisible', () => {
574-
it('should be no [object Object] in the error message', () => {
575-
return wd
576-
.amOnPage('/info')
577-
.then(() => wd.waitForInvisible('//div[@id = "grab-multiple"]//a', 3))
578-
.then(() => {
579-
throw Error('It should never get this far')
580-
})
581-
.catch(e => {
582-
e.message.should.not.include('[object Object]')
583-
})
564+
it('should be no [object Object] in the error message', async () => {
565+
try {
566+
await wd.amOnPage('/info')
567+
await wd.waitForInvisible('//div[@id = "grab-multiple"]//a', 3)
568+
throw new Error('It should never get this far')
569+
} catch (e) {
570+
e.message.should.not.include('[object Object]')
571+
}
584572
})
585573

586-
it('should wait for a specified element to be invisible', () => {
587-
return wd
588-
.amOnPage('/form/wait_invisible')
589-
.then(() => wd.waitForInvisible('#step1', 3))
590-
.then(() => wd.dontSeeElement('#step1'))
574+
it('should wait for a specified element to be invisible', async () => {
575+
await wd.amOnPage('/form/wait_invisible')
576+
await wd.waitForInvisible('#step1', 3)
577+
await wd.dontSeeElement('#step1')
591578
})
592579
})
593580

@@ -625,117 +612,105 @@ describe('WebDriver', function () {
625612
assert.equal(numPagesAfter, 2)
626613
})
627614

628-
it('should assert when there is no ability to switch to next tab', () => {
629-
return wd
630-
.amOnPage('/')
631-
.then(() => wd.click('More info'))
632-
.then(() => wd.wait(1)) // Wait is required because the url is change by previous statement (maybe related to #914)
633-
.then(() => wd.switchToNextTab(2))
634-
.then(() => assert.equal(true, false, 'Throw an error if it gets this far (which it should not)!'))
635-
.catch(e => {
636-
assert.equal(e.message, 'There is no ability to switch to next tab with offset 2')
637-
})
615+
it('should assert when there is no ability to switch to next tab', async () => {
616+
try {
617+
await wd.amOnPage('/')
618+
await wd.click('More info')
619+
await wd.wait(1) // Wait is required because the url is changed by the previous statement (maybe related to #914)
620+
await wd.switchToNextTab(2)
621+
assert.equal(true, false, 'Throw an error if it gets this far (which it should not)!')
622+
} catch (e) {
623+
assert.equal(e.message, 'There is no ability to switch to next tab with offset 2')
624+
}
638625
})
639626

640-
it('should close current tab', () => {
641-
return wd
642-
.amOnPage('/info')
643-
.then(() => wd.click('New tab'))
644-
.then(() => wd.switchToNextTab())
645-
.then(() => wd.seeInCurrentUrl('/login'))
646-
.then(() => wd.grabNumberOfOpenTabs())
647-
.then(numPages => assert.equal(numPages, 2))
648-
.then(() => wd.closeCurrentTab())
649-
.then(() => wd.seeInCurrentUrl('/info'))
650-
.then(() => wd.grabNumberOfOpenTabs())
627+
it('should close current tab', async () => {
628+
await wd.amOnPage('/info')
629+
await wd.click('New tab')
630+
await wd.switchToNextTab()
631+
await wd.seeInCurrentUrl('/login')
632+
const numPages = await wd.grabNumberOfOpenTabs()
633+
assert.equal(numPages, 2)
634+
await wd.closeCurrentTab()
635+
await wd.seeInCurrentUrl('/info')
636+
await wd.grabNumberOfOpenTabs()
651637
})
652638

653-
it('should close other tabs', () => {
654-
return wd
655-
.amOnPage('/')
656-
.then(() => wd.openNewTab())
657-
.then(() => wd.seeInCurrentUrl('about:blank'))
658-
.then(() => wd.amOnPage('/info'))
659-
.then(() => wd.click('New tab'))
660-
.then(() => wd.switchToNextTab())
661-
.then(() => wd.seeInCurrentUrl('/login'))
662-
.then(() => wd.closeOtherTabs())
663-
.then(() => wd.seeInCurrentUrl('/login'))
664-
.then(() => wd.grabNumberOfOpenTabs())
665-
})
666-
667-
it('should open new tab', () => {
668-
return wd
669-
.amOnPage('/info')
670-
.then(() => wd.openNewTab())
671-
.then(() => wd.waitInUrl('about:blank'))
672-
.then(() => wd.grabNumberOfOpenTabs())
673-
.then(numPages => assert.equal(numPages, 2))
639+
it('should close other tabs', async () => {
640+
await wd.amOnPage('/')
641+
await wd.openNewTab()
642+
await wd.seeInCurrentUrl('about:blank')
643+
await wd.amOnPage('/info')
644+
await wd.click('New tab')
645+
await wd.switchToNextTab()
646+
await wd.seeInCurrentUrl('/login')
647+
await wd.closeOtherTabs()
648+
await wd.seeInCurrentUrl('/login')
649+
await wd.grabNumberOfOpenTabs()
674650
})
675651

676-
it('should switch to previous tab', () => {
677-
return wd
678-
.amOnPage('/info')
679-
.then(() => wd.openNewTab())
680-
.then(() => wd.waitInUrl('about:blank'))
681-
.then(() => wd.switchToPreviousTab())
682-
.then(() => wd.waitInUrl('/info'))
652+
it('should open new tab', async () => {
653+
await wd.amOnPage('/info')
654+
await wd.openNewTab()
655+
await wd.waitInUrl('about:blank')
656+
const numPages = await wd.grabNumberOfOpenTabs()
657+
assert.equal(numPages, 2)
683658
})
684659

685-
it('should assert when there is no ability to switch to previous tab', () => {
686-
return wd
687-
.amOnPage('/info')
688-
.then(() => wd.openNewTab())
689-
.then(() => wd.waitInUrl('about:blank'))
690-
.then(() => wd.switchToPreviousTab(2))
691-
.then(() => wd.waitInUrl('/info'))
692-
.catch(e => {
693-
assert.equal(e.message, 'There is no ability to switch to previous tab with offset 2')
694-
})
660+
it('should switch to previous tab', async () => {
661+
await wd.amOnPage('/info')
662+
await wd.openNewTab()
663+
await wd.waitInUrl('about:blank')
664+
await wd.switchToPreviousTab()
665+
await wd.waitInUrl('/info')
666+
})
667+
668+
it('should assert when there is no ability to switch to previous tab', async () => {
669+
try {
670+
await wd.amOnPage('/info')
671+
await wd.openNewTab()
672+
await wd.waitInUrl('about:blank')
673+
await wd.switchToPreviousTab(2)
674+
await wd.waitInUrl('/info')
675+
} catch (e) {
676+
assert.equal(e.message, 'There is no ability to switch to previous tab with offset 2')
677+
}
695678
})
696679
})
697680

698-
// TO-DO: those tests are flaky so skipping them for now
699681
describe('popup : #acceptPopup, #seeInPopup, #cancelPopup', async () => {
700682
it('should accept popup window', async () => {
701683
await wd.amOnPage('/form/popup')
702-
await wd.waitForText('Confirm', 5)
703684
await wd.click('Confirm')
704685
await wd.acceptPopup()
705-
await wd.waitForElement({ css: '#result' }, 5)
706686
await wd.see('Yes', '#result')
707687
})
708688

709689
it('should cancel popup', async () => {
710690
await wd.amOnPage('/form/popup')
711-
await wd.waitForText('Confirm', 5)
712691
await wd.click('Confirm')
713692
await wd.cancelPopup()
714-
await wd.waitForElement({ css: '#result' }, 5)
715693
await wd.see('No', '#result')
716694
})
717695

718-
it('should check text in popup', () => {
719-
return wd
720-
.amOnPage('/form/popup')
721-
.then(() => wd.click('Alert'))
722-
.then(() => wd.seeInPopup('Really?'))
723-
.then(() => wd.cancelPopup())
696+
it('should check text in popup', async () => {
697+
await wd.amOnPage('/form/popup')
698+
await wd.click('Alert')
699+
await wd.seeInPopup('Really?')
700+
await wd.cancelPopup()
724701
})
725702

726-
it('should grab text from popup', () => {
727-
return wd
728-
.amOnPage('/form/popup')
729-
.then(() => wd.click('Alert'))
730-
.then(() => wd.grabPopupText())
731-
.then(text => assert.equal(text, 'Really?'))
703+
it('should grab text from popup', async () => {
704+
await wd.amOnPage('/form/popup')
705+
await wd.click('Alert')
706+
const text = await wd.grabPopupText()
707+
assert.equal(text, 'Really?')
732708
})
733709

734-
it('should return null if no popup is visible (do not throw an error)', () => {
735-
return wd
736-
.amOnPage('/form/popup')
737-
.then(() => wd.grabPopupText())
738-
.then(text => assert.equal(text, null))
710+
it('should return null if no popup is visible (do not throw an error)', async () => {
711+
await wd.amOnPage('/form/popup')
712+
const text = await wd.grabPopupText()
713+
assert.equal(text, null)
739714
})
740715
})
741716

@@ -1296,7 +1271,6 @@ describe('WebDriver - Basic Authentication', () => {
12961271

12971272
afterEach(() => wd._after())
12981273

1299-
// local run passed ✔ should be authenticated (443ms)
13001274
describe('open page : #amOnPage', () => {
13011275
it('should be authenticated', async () => {
13021276
await wd.amOnPage('/basic_auth')

0 commit comments

Comments
 (0)