@@ -3,10 +3,10 @@ var Firebase = require('firebase');
33
44describe ( 'Chat App' , function ( ) {
55 // Reference to the Firebase which stores the data for this demo
6- var firebaseRef = new Firebase ( 'https://angularFireTests .firebaseio-demo.com/chat' ) ;
6+ var firebaseRef = new Firebase ( 'https://angularfire .firebaseio-demo.com/chat' ) ;
77
8- // Boolean used to clear the Firebase on the first test only
9- var firebaseCleared = false ;
8+ // Boolean used to load the page on the first test only
9+ var isPageLoaded = false ;
1010
1111 // Reference to the messages repeater
1212 var messages = element . all ( by . repeater ( 'message in messages' ) ) ;
@@ -21,35 +21,41 @@ describe('Chat App', function () {
2121 flow . execute ( waitOne ) ;
2222 }
2323
24- beforeEach ( function ( ) {
25- // Clear the Firebase before the first test and sleep until it's finished
26- if ( ! firebaseCleared ) {
27- flow . execute ( function ( ) {
28- var def = protractor . promise . defer ( ) ;
29- firebaseRef . remove ( function ( err ) {
30- if ( err ) {
31- def . reject ( err ) ;
32- }
33- else {
34- firebaseCleared = true ;
35- def . fulfill ( ) ;
36- }
37- } ) ;
38- return def . promise ;
39- } ) ;
40- }
24+ function clearFirebaseRef ( ) {
25+ var deferred = protractor . promise . defer ( ) ;
4126
42- // Navigate to the chat app
43- browser . get ( 'chat/chat.html' ) ;
27+ firebaseRef . remove ( function ( err ) {
28+ if ( err ) {
29+ deferred . reject ( err ) ;
30+ } else {
31+ deferred . fulfill ( ) ;
32+ }
33+ } ) ;
4434
45- // wait for page to load
46- sleep ( ) ;
47- } ) ;
35+ return deferred . promise ;
36+ }
4837
49- it ( 'loads' , function ( ) {
38+ beforeEach ( function ( done ) {
39+ if ( ! isPageLoaded ) {
40+ isPageLoaded = true ;
41+
42+ // Navigate to the chat app
43+ browser . get ( 'chat/chat.html' ) . then ( function ( ) {
44+ // Get the random push ID where the data is being stored
45+ return $ ( '#pushId' ) . getText ( ) ;
46+ } ) . then ( function ( pushId ) {
47+ // Update the Firebase ref to point to the random push ID
48+ firebaseRef = firebaseRef . child ( pushId ) ;
49+
50+ // Clear the Firebase ref
51+ return clearFirebaseRef ( ) ;
52+ } ) . then ( done ) ;
53+ } else {
54+ done ( ) ;
55+ }
5056 } ) ;
5157
52- it ( 'has the correct title ' , function ( ) {
58+ it ( 'loads ' , function ( ) {
5359 expect ( browser . getTitle ( ) ) . toEqual ( 'AngularFire Chat e2e Test' ) ;
5460 } ) ;
5561
@@ -74,7 +80,7 @@ describe('Chat App', function () {
7480 flow . execute ( function ( ) {
7581 var def = protractor . promise . defer ( ) ;
7682 // Simulate a message being added remotely
77- firebaseRef . child ( " messages" ) . push ( {
83+ firebaseRef . child ( ' messages' ) . push ( {
7884 from : 'Guest 2000' ,
7985 content : 'Remote message detected'
8086 } , function ( err ) {
@@ -92,8 +98,8 @@ describe('Chat App', function () {
9298 flow . execute ( function ( ) {
9399 var def = protractor . promise . defer ( ) ;
94100 // Simulate a message being deleted remotely
95- var onCallback = firebaseRef . child ( " messages" ) . limitToLast ( 1 ) . on ( " child_added" , function ( childSnapshot ) {
96- firebaseRef . child ( " messages" ) . off ( " child_added" , onCallback ) ;
101+ var onCallback = firebaseRef . child ( ' messages' ) . limitToLast ( 1 ) . on ( ' child_added' , function ( childSnapshot ) {
102+ firebaseRef . child ( ' messages' ) . off ( ' child_added' , onCallback ) ;
97103 childSnapshot . ref ( ) . remove ( function ( err ) {
98104 if ( err ) { def . reject ( err ) ; }
99105 else { def . fulfill ( ) ; }
0 commit comments