1- import { browser , by , element , Key , ProtractorBy } from 'protractor' ;
1+ import { browser , by , element , Key } from 'protractor' ;
2+ import { expectToExist , expectFocusOn } from '../../util/asserts' ;
3+ import { pressKeys , clickElementAtPoint } from '../../util/actions' ;
4+ import { waitForElement } from '../../util/query' ;
25
36describe ( 'dialog' , ( ) => {
47 beforeEach ( ( ) => browser . get ( '/dialog' ) ) ;
58
69 it ( 'should open a dialog' , ( ) => {
710 element ( by . id ( 'default' ) ) . click ( ) ;
8- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( true ) ) ;
11+ expectToExist ( 'md-dialog-container' ) ;
912 } ) ;
1013
1114 it ( 'should close by clicking on the backdrop' , ( ) => {
1215 element ( by . id ( 'default' ) ) . click ( ) ;
1316
1417 waitForDialog ( ) . then ( ( ) => {
1518 clickOnBackrop ( ) ;
16- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( false ) ) ;
19+ expectToExist ( 'md-dialog-container' , false ) ;
1720 } ) ;
1821 } ) ;
1922
2023 it ( 'should close by pressing escape' , ( ) => {
2124 element ( by . id ( 'default' ) ) . click ( ) ;
2225
2326 waitForDialog ( ) . then ( ( ) => {
24- pressEscape ( ) ;
25- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( false ) ) ;
27+ pressKeys ( Key . ESCAPE ) ;
28+ expectToExist ( 'md-dialog-container' , false ) ;
2629 } ) ;
2730 } ) ;
2831
@@ -31,15 +34,15 @@ describe('dialog', () => {
3134
3235 waitForDialog ( ) . then ( ( ) => {
3336 element ( by . id ( 'close' ) ) . click ( ) ;
34- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( false ) ) ;
37+ expectToExist ( 'md-dialog-container' , false ) ;
3538 } ) ;
3639 } ) ;
3740
3841 it ( 'should focus the first focusable element' , ( ) => {
3942 element ( by . id ( 'default' ) ) . click ( ) ;
4043
4144 waitForDialog ( ) . then ( ( ) => {
42- expectFocusOn ( element ( by . css ( 'md-dialog-container input' ) ) ) ;
45+ expectFocusOn ( 'md-dialog-container input' ) ;
4346 } ) ;
4447 } ) ;
4548
@@ -60,8 +63,8 @@ describe('dialog', () => {
6063 waitForDialog ( ) . then ( ( ) => {
6164 let tab = Key . TAB ;
6265
63- browser . actions ( ) . sendKeys ( tab , tab , tab ) . perform ( ) ;
64- expectFocusOn ( element ( by . id ( ' close') ) ) ;
66+ pressKeys ( tab , tab , tab ) ;
67+ expectFocusOn ( '# close') ;
6568 } ) ;
6669 } ) ;
6770
@@ -70,38 +73,24 @@ describe('dialog', () => {
7073
7174 waitForDialog ( ) . then ( ( ) => {
7275 clickOnBackrop ( ) ;
73- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( true ) ) ;
76+ expectToExist ( 'md-dialog-container' ) ;
7477 } ) ;
7578 } ) ;
7679
7780 it ( 'should be able to prevent closing by pressing escape' , ( ) => {
7881 element ( by . id ( 'disabled' ) ) . click ( ) ;
7982
8083 waitForDialog ( ) . then ( ( ) => {
81- pressEscape ( ) ;
82- waitForDialog ( ) . then ( ( isPresent : boolean ) => expect ( isPresent ) . toBe ( true ) ) ;
84+ pressKeys ( Key . ESCAPE ) ;
85+ expectToExist ( 'md-dialog-container' ) ;
8386 } ) ;
8487 } ) ;
8588
8689 function waitForDialog ( ) {
87- return browser . isElementPresent ( by . css ( 'md-dialog-container' ) as ProtractorBy ) ;
90+ return waitForElement ( 'md-dialog-container' ) ;
8891 }
8992
9093 function clickOnBackrop ( ) {
91- browser . actions ( )
92- // We need to move the cursor to the top left so
93- // the dialog doesn't receive the click accidentally.
94- . mouseMove ( element ( by . css ( '.cdk-overlay-backdrop' ) ) . getWebElement ( ) , { x : 0 , y : 0 } )
95- . click ( )
96- . perform ( ) ;
97- }
98-
99- function pressEscape ( ) {
100- browser . actions ( ) . sendKeys ( Key . ESCAPE ) . perform ( ) ;
101- }
102-
103- // TODO(crisbeto): should be moved to a common util. copied from the menu e2e setup.
104- function expectFocusOn ( el : any ) : void {
105- expect ( browser . driver . switchTo ( ) . activeElement ( ) . getId ( ) ) . toBe ( el . getId ( ) ) ;
94+ clickElementAtPoint ( '.cdk-overlay-backdrop' , { x : 0 , y : 0 } ) ;
10695 }
10796} ) ;
0 commit comments