Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 5d7622e

Browse files
committed
Wait up to 3 seconds for angular to be found on the page, and if it isn't give
a sane error message. Fixes issue #2.
1 parent c69bf23 commit 5d7622e

File tree

2 files changed

+93
-25
lines changed

2 files changed

+93
-25
lines changed

externalhttpspec.js

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
1-
var webdriver = require('selenium-webdriver');
2-
var protractor = require('./protractor.js');
3-
var assert = require('assert');
41
var util = require('util');
52

6-
var driver = new webdriver.Builder().
7-
usingServer('http://localhost:4444/wd/hub').
8-
withCapabilities({
9-
'browserName': 'chrome',
10-
'version': '',
11-
'platform': 'ANY',
12-
'javascriptEnabled': true
13-
}).build();
143

15-
driver.manage().timeouts().setScriptTimeout(10000);
16-
var ptor = protractor.wrapDriver(driver);
4+
describe('angularjs homepage', function() {
5+
var webdriver = require('selenium-webdriver');
6+
var protractor = require('./protractor.js');
177

18-
ptor.get('http://www.angularjs.org');
8+
var driver = new webdriver.Builder().
9+
usingServer('http://localhost:4444/wd/hub').
10+
withCapabilities({
11+
'browserName': 'chrome',
12+
'version': '',
13+
'platform': 'ANY',
14+
'javascriptEnabled': true
15+
}).build();
1916

20-
ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");
17+
driver.manage().timeouts().setScriptTimeout(10000);
18+
var ptor = protractor.wrapDriver(driver);
2119

22-
ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
23-
getText().then(function(text) {
24-
assert.equal('Hello Julie!', text);
25-
});
20+
it('should greet using binding', function(done) {
21+
ptor.get('http://www.angularjs.org');
2622

27-
// Uncomment to see failures.
28-
// ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
29-
// getText().then(function(text) {
30-
// assert.equal('Hello Jack!', text);
31-
// });
23+
ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");
24+
25+
ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
26+
getText().then(function(text) {
27+
expect(text).toEqual('Hello Julie!');
28+
done();
29+
});
30+
}, 10000);
31+
32+
it('should greet using binding - #2', function(done) {
33+
ptor.get('http://www.angularjs.org');
34+
35+
ptor.findElement(protractor.By.input("yourName")).sendKeys("Jane");
36+
37+
ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
38+
getText().then(function(text) {
39+
expect(text).toEqual('Hello Jane!');
40+
done();
41+
});
42+
}, 10000);
43+
44+
// Uncomment to see failures.
45+
it('should greet using binding - this one fails', function(done) {
46+
ptor.get('http://www.angularjs.org');
47+
48+
ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");
49+
50+
ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
51+
getText().then(function(text) {
52+
expect(text).toEqual('Hello Jack');
53+
done();
54+
});
55+
});
56+
57+
58+
it('afterAll', function() {
59+
// This is a sad hack to do any shutdown of the server.
60+
// TODO(juliemr): Add afterall functionality to jasmine-node
61+
driver.quit();
62+
})
63+
});

protractor.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ clientSideScripts.findRepeaterElement = function() {
118118
};
119119

120120

121+
/**
122+
* Tests whether the angular global variable is present on a page. Retries
123+
* twice in case the page is just loading slowly.
124+
*
125+
* arguments none
126+
*
127+
* @return {boolean} true if angular was found.
128+
*/
129+
clientSideScripts.testForAngular = function() {
130+
var callback = arguments[arguments.length - 1];
131+
var retry = function(n) {
132+
if (window.angular && window.angular.resumeBootstrap) {
133+
callback(true);
134+
} else if (n < 1) {
135+
callback(false)
136+
} else {
137+
window.setTimeout(function() {retry(n - 1)}, 1000);
138+
}
139+
}
140+
if (window.angular && window.angular.resumeBootstrap) {
141+
callback(true);
142+
} else {
143+
retry(3);
144+
}
145+
}
146+
147+
121148
/**
122149
* @param {webdriver.WebDriver} webdriver
123150
* @constructor
@@ -199,9 +226,18 @@ Protractor.prototype.get = function(destination) {
199226
this.driver.get('about:blank');
200227
this.driver.executeScript('window.name += "' + DEFER_LABEL + '";' +
201228
'window.location.href = "' + destination + '"');
229+
230+
// Make sure the page is an Angular page.
231+
this.driver.executeAsyncScript(clientSideScripts.testForAngular).
232+
then(function(hasAngular) {
233+
if (!hasAngular) {
234+
throw new Error("Angular could not be found on the page " +
235+
destination);
236+
}
237+
});
238+
202239
// At this point, Angular will pause for us, until angular.resumeBootstrap
203240
// is called.
204-
205241
for (var i = 0; i < this.moduleScripts_.length; ++i) {
206242
this.driver.executeScript(this.moduleScripts_[i]);
207243
}

0 commit comments

Comments
 (0)