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

Commit

Permalink
Wait up to 3 seconds for angular to be found on the page, and if it i…
Browse files Browse the repository at this point in the history
…sn't give

a sane error message. Fixes issue #2.
  • Loading branch information
juliemr committed May 30, 2013
1 parent c69bf23 commit 5d7622e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 25 deletions.
80 changes: 56 additions & 24 deletions externalhttpspec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
var webdriver = require('selenium-webdriver');
var protractor = require('./protractor.js');
var assert = require('assert');
var util = require('util');

var driver = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({
'browserName': 'chrome',
'version': '',
'platform': 'ANY',
'javascriptEnabled': true
}).build();

driver.manage().timeouts().setScriptTimeout(10000);
var ptor = protractor.wrapDriver(driver);
describe('angularjs homepage', function() {
var webdriver = require('selenium-webdriver');
var protractor = require('./protractor.js');

ptor.get('http://www.angularjs.org');
var driver = new webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({
'browserName': 'chrome',
'version': '',
'platform': 'ANY',
'javascriptEnabled': true
}).build();

ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");
driver.manage().timeouts().setScriptTimeout(10000);
var ptor = protractor.wrapDriver(driver);

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

// Uncomment to see failures.
// ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
// getText().then(function(text) {
// assert.equal('Hello Jack!', text);
// });
ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");

ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
getText().then(function(text) {
expect(text).toEqual('Hello Julie!');
done();
});
}, 10000);

it('should greet using binding - #2', function(done) {
ptor.get('http://www.angularjs.org');

ptor.findElement(protractor.By.input("yourName")).sendKeys("Jane");

ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
getText().then(function(text) {
expect(text).toEqual('Hello Jane!');
done();
});
}, 10000);

// Uncomment to see failures.
it('should greet using binding - this one fails', function(done) {
ptor.get('http://www.angularjs.org');

ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");

ptor.findElement(protractor.By.binding("Hello {{yourName}}!")).
getText().then(function(text) {
expect(text).toEqual('Hello Jack');
done();
});
});


it('afterAll', function() {
// This is a sad hack to do any shutdown of the server.
// TODO(juliemr): Add afterall functionality to jasmine-node
driver.quit();
})
});
38 changes: 37 additions & 1 deletion protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ clientSideScripts.findRepeaterElement = function() {
};


/**
* Tests whether the angular global variable is present on a page. Retries
* twice in case the page is just loading slowly.
*
* arguments none
*
* @return {boolean} true if angular was found.
*/
clientSideScripts.testForAngular = function() {
var callback = arguments[arguments.length - 1];
var retry = function(n) {
if (window.angular && window.angular.resumeBootstrap) {
callback(true);
} else if (n < 1) {
callback(false)
} else {
window.setTimeout(function() {retry(n - 1)}, 1000);
}
}
if (window.angular && window.angular.resumeBootstrap) {
callback(true);
} else {
retry(3);
}
}


/**
* @param {webdriver.WebDriver} webdriver
* @constructor
Expand Down Expand Up @@ -199,9 +226,18 @@ Protractor.prototype.get = function(destination) {
this.driver.get('about:blank');
this.driver.executeScript('window.name += "' + DEFER_LABEL + '";' +
'window.location.href = "' + destination + '"');

// Make sure the page is an Angular page.
this.driver.executeAsyncScript(clientSideScripts.testForAngular).
then(function(hasAngular) {
if (!hasAngular) {
throw new Error("Angular could not be found on the page " +
destination);
}
});

// At this point, Angular will pause for us, until angular.resumeBootstrap
// is called.

for (var i = 0; i < this.moduleScripts_.length; ++i) {
this.driver.executeScript(this.moduleScripts_[i]);
}
Expand Down

0 comments on commit 5d7622e

Please sign in to comment.