Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 91414e0

Browse files
author
Jacob Wenger
committed
Merge pull request #574 from firebase/jw-e2e-reliability
Updated e2e tests to store data at random children nodes
2 parents 5099669 + ae28f99 commit 91414e0

File tree

12 files changed

+220
-162
lines changed

12 files changed

+220
-162
lines changed

tests/protractor/chat/chat.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
</head>
1818

1919
<body ng-controller="ChatCtrl">
20+
<!-- Firebase push ID where data is stored (used by the test spec to know where data is stored) -->
21+
<p id="pushId"></p>
22+
2023
<!-- Clear Firebase button -->
2124
<button id="clearRef" ng-click="clearRef()">Clear Ref</button>
2225

@@ -40,4 +43,4 @@
4043
<!-- Custom JS -->
4144
<script src="chat.js" defer></script>
4245
</body>
43-
</html>
46+
</html>

tests/protractor/chat/chat.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
var app = angular.module('chat', ['firebase']);
22
app.controller('ChatCtrl', function Chat($scope, $firebaseObject, $firebaseArray) {
33
// Get a reference to the Firebase
4-
var chatFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/chat');
5-
var messagesFirebaseRef = chatFirebaseRef.child("messages").limitToLast(2);
4+
var rootRef = new Firebase('https://angularfire.firebaseio-demo.com');
5+
6+
// Store the data at a random push ID
7+
var chatRef = rootRef.child('chat').push();
8+
9+
// Put the random push ID into the DOM so that the test suite can grab it
10+
document.getElementById('pushId').innerHTML = chatRef.key();
11+
12+
var messagesRef = chatRef.child('messages').limitToLast(2);
613

714
// Get the chat data as an object
8-
$scope.chat = $firebaseObject(chatFirebaseRef);
15+
$scope.chat = $firebaseObject(chatRef);
916

1017
// Get the chat messages as an array
11-
$scope.messages = $firebaseArray(messagesFirebaseRef);
18+
$scope.messages = $firebaseArray(messagesRef);
1219

1320
// Verify that $inst() works
14-
verify($scope.chat.$ref() === chatFirebaseRef, "Something is wrong with $firebaseObject.$ref().");
15-
verify($scope.messages.$ref() === messagesFirebaseRef, "Something is wrong with $firebaseArray.$ref().");
21+
verify($scope.chat.$ref() === chatRef, 'Something is wrong with $firebaseObject.$ref().');
22+
verify($scope.messages.$ref() === messagesRef, 'Something is wrong with $firebaseArray.$ref().');
1623

1724
// Initialize $scope variables
18-
$scope.message = "";
25+
$scope.message = '';
1926
$scope.username = 'Guest' + Math.floor(Math.random() * 101);
2027

2128
/* Clears the chat Firebase reference */
2229
$scope.clearRef = function () {
23-
chatFirebaseRef.remove();
30+
chatRef.remove();
2431
};
2532

2633
/* Adds a new message to the messages list and updates the messages count */
2734
$scope.addMessage = function() {
28-
if ($scope.message !== "") {
35+
if ($scope.message !== '') {
2936
// Add a new message to the messages list
3037
$scope.messages.$add({
3138
from: $scope.username,
3239
content: $scope.message
3340
});
3441

3542
// Reset the message input
36-
$scope.message = "";
43+
$scope.message = '';
3744
}
3845
};
3946

@@ -55,4 +62,4 @@ app.controller('ChatCtrl', function Chat($scope, $firebaseObject, $firebaseArray
5562
throw new Error(message);
5663
}
5764
}
58-
});
65+
});

tests/protractor/chat/chat.spec.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ var Firebase = require('firebase');
33

44
describe('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(); }

tests/protractor/priority/priority.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
</head>
1818

1919
<body ng-controller="PriorityCtrl">
20+
<!-- Firebase push ID where data is stored (used by the test spec to know where data is stored) -->
21+
<p id="pushId"></p>
22+
2023
<!-- Clear Firebase button -->
2124
<button id="clearRef" ng-click="clearRef()">Clear Ref</button>
2225

@@ -47,4 +50,4 @@
4750
<!-- Custom JS -->
4851
<script src="priority.js" defer></script>
4952
</body>
50-
</html>
53+
</html>

tests/protractor/priority/priority.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
var app = angular.module('priority', ['firebase']);
22
app.controller('PriorityCtrl', function Chat($scope, $firebaseArray, $firebaseObject) {
33
// Get a reference to the Firebase
4-
var messagesFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/priority');
4+
var messagesRef = new Firebase('https://angularfire.firebaseio-demo.com/priority').push();
5+
6+
// Put the random push ID into the DOM so that the test suite can grab it
7+
document.getElementById('pushId').innerHTML = messagesRef.key();
58

69
// Get the chat messages as an array
7-
$scope.messages = $firebaseArray(messagesFirebaseRef);
10+
$scope.messages = $firebaseArray(messagesRef);
811

912
// Verify that $inst() works
10-
verify($scope.messages.$ref() === messagesFirebaseRef, 'Something is wrong with $firebaseArray.$ref().');
13+
verify($scope.messages.$ref() === messagesRef, 'Something is wrong with $firebaseArray.$ref().');
1114

1215
// Initialize $scope variables
1316
$scope.message = '';
1417
$scope.username = 'Guest' + Math.floor(Math.random() * 101);
1518

1619
/* Clears the priority Firebase reference */
1720
$scope.clearRef = function () {
18-
messagesFirebaseRef.remove();
21+
messagesRef.remove();
1922
};
2023

2124
/* Adds a new message to the messages list */
@@ -57,4 +60,4 @@ app.controller('PriorityCtrl', function Chat($scope, $firebaseArray, $firebaseOb
5760
throw new Error(message);
5861
}
5962
}
60-
});
63+
});

tests/protractor/priority/priority.spec.js

+33-37
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ describe('Priority App', function () {
66
var messages = element.all(by.repeater('message in messages'));
77

88
// Reference to the Firebase which stores the data for this demo
9-
var firebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/priority');
9+
var firebaseRef = new Firebase('https://angularfire.firebaseio-demo.com/priority');
1010

11-
// Boolean used to clear the Firebase on the first test only
12-
var firebaseCleared = false;
11+
// Boolean used to load the page on the first test only
12+
var isPageLoaded = false;
1313

1414
var flow = protractor.promise.controlFlow();
1515

@@ -18,49 +18,45 @@ describe('Priority App', function () {
1818
}
1919

2020
function sleep() {
21-
return flow.execute(waitOne);
21+
flow.execute(waitOne);
2222
}
2323

24-
beforeEach(function () {
25-
if( !firebaseCleared ) {
26-
firebaseCleared = true;
27-
flow.execute(purge);
28-
}
29-
30-
// Navigate to the priority app
31-
browser.get('priority/priority.html');
24+
function clearFirebaseRef() {
25+
var deferred = protractor.promise.defer();
3226

33-
// Wait for all data to load into the client
34-
flow.execute(waitForData);
27+
firebaseRef.remove(function(err) {
28+
if (err) {
29+
deferred.reject(err);
30+
} else {
31+
deferred.fulfill();
32+
}
33+
});
3534

36-
function purge() {
37-
var def = protractor.promise.defer();
38-
firebaseRef.remove(function(err) {
39-
if( err ) { def.reject(err); }
40-
else { def.fulfill(true); }
41-
});
42-
return def.promise;
43-
}
35+
return deferred.promise;
36+
}
4437

45-
function waitForData() {
46-
var def = protractor.promise.defer();
47-
firebaseRef.once('value', function() {
48-
waitOne().then(function() {
49-
def.fulfill(true);
50-
});
51-
});
52-
return def.promise;
38+
beforeEach(function (done) {
39+
if (!isPageLoaded) {
40+
isPageLoaded = true;
41+
42+
// Navigate to the priority app
43+
browser.get('priority/priority.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();
5355
}
5456
});
5557

56-
afterEach(function() {
57-
firebaseRef.off();
58-
});
5958

6059
it('loads', function () {
61-
});
62-
63-
it('has the correct title', function () {
6460
expect(browser.getTitle()).toEqual('AngularFire Priority e2e Test');
6561
});
6662

@@ -127,4 +123,4 @@ describe('Priority App', function () {
127123
return def.promise;
128124
}
129125
});
130-
});
126+
});

tests/protractor/tictactoe/tictactoe.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
</head>
1818

1919
<body ng-controller="TicTacToeCtrl">
20+
<!-- Firebase push ID where data is stored (used by the test spec to know where data is stored) -->
21+
<p id="pushId"></p>
22+
2023
<!-- Reset Firebase button -->
2124
<button id="resetRef" ng-click="resetRef()">Reset Ref</button>
2225

@@ -35,4 +38,4 @@
3538
<!-- Custom JS -->
3639
<script src="tictactoe.js" defer></script>
3740
</body>
38-
</html>
41+
</html>

0 commit comments

Comments
 (0)