diff --git a/lib/protractor.js b/lib/protractor.js index f1d18836e..a4f5316d7 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -837,7 +837,7 @@ Protractor.prototype.clearMockModules = function() { * Remove a registered mock module. * @param {!string} name The name of the module to remove. */ -Protractor.prototype.removeMockModule = function (name) { +Protractor.prototype.removeMockModule = function(name) { var index = this.moduleNames_.indexOf(name); this.moduleNames_.splice(index, 1); this.moduleScripts_.splice(index, 1); @@ -848,7 +848,7 @@ Protractor.prototype.removeMockModule = function (name) { * * Navigate to the given destination and loads mock modules before * Angular. Assumes that the page being loaded uses Angular. - * If you need to access a page which does have Angular on load, use + * If you need to access a page which does not have Angular on load, use * the wrapped webdriver directly. * * @param {string} destination Destination URL. @@ -867,7 +867,7 @@ Protractor.prototype.get = function(destination, opt_timeout) { this.driver.get('about:blank'); this.driver.executeScript( 'window.name = "' + DEFER_LABEL + '" + window.name;' + - 'window.location.assign("' + destination + '");'); + 'window.location.replace("' + destination + '");'); // At this point, we need to make sure the new url has loaded before // we try to execute any asynchronous scripts. @@ -909,6 +909,39 @@ Protractor.prototype.get = function(destination, opt_timeout) { }, this.moduleNames_); }; +/** + * See webdriver.WebDriver.refresh + * + * Makes a full reload of the current page and loads mock modules before + * Angular. Assumes that the page being loaded uses Angular. + * If you need to access a page which does not have Angular on load, use + * the wrapped webdriver directly. + * + * @param {number=} opt_timeout Number of seconds to wait for Angular to start. + */ +Protractor.prototype.refresh = function(opt_timeout) { + var timeout = opt_timeout || 10; + var self = this; + + if (self.ignoreSynchronization) { + return self.driver.navigate().refresh(); + } + + return self.driver.executeScript('return window.location.href').then(function(href) { + return self.get(href, opt_timeout); + }); +}; + +/** + * Mixin navigation methods back into the navigation object so that + * they are invoked as before, i.e. driver.navigate().refresh() + */ +Protractor.prototype.navigate = function() { + var nav = this.driver.navigate(); + mixin(nav, this, 'refresh'); + return nav; +} + /** * Browse to another page using in-page navigation. * diff --git a/spec/basic/actions_spec.js b/spec/basic/actions_spec.js index 0a6905e89..64134db4a 100644 --- a/spec/basic/actions_spec.js +++ b/spec/basic/actions_spec.js @@ -24,4 +24,49 @@ describe('using an ActionSequence', function() { alertDialog.accept(); }); + + it('should refresh properly', function() { + var username = element(by.model('username')); + var name = element(by.binding('username')); + username.clear(); + expect(name.getText()).toEqual(''); + + browser.navigate().refresh(); + + expect(name.getText()).toEqual('Anon'); + }); + + // Back and forward do NOT work at the moment because of an issue + // bootstrapping with Angular + /* + it('should navigate back and forward properly', function() { + var port = process.env.HTTP_PORT || '8000'; + browser.get('index.html#/repeater'); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/repeater'); + + browser.navigate().back(); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/form'); + + browser.navigate().forward(); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/repeater'); + }); + */ + + it('should navigate back and forward properly from link', function() { + var port = process.env.HTTP_PORT || '8000'; + element(by.linkText('repeater')).click(); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/repeater'); + + browser.navigate().back(); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/form'); + + browser.navigate().forward(); + expect(browser.getCurrentUrl()). + toEqual('http://localhost:'+port+'/index.html#/repeater'); + }); }); diff --git a/spec/basic/mockmodule_spec.js b/spec/basic/mockmodule_spec.js index cfbe95472..9d530c151 100644 --- a/spec/basic/mockmodule_spec.js +++ b/spec/basic/mockmodule_spec.js @@ -48,4 +48,50 @@ describe('mock modules', function() { expect(element(by.css('[app-version]')).getText()).toEqual('2'); }); + it('should load mock modules after refresh', function() { + browser.addMockModule('moduleA', mockModuleA); + + browser.get('index.html'); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.navigate().refresh(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + }); + + // Back and forward do NOT work at the moment because of an issue + // bootstrapping with Angular + /* + it('should load mock modules after navigating back and forward', function() { + browser.addMockModule('moduleA', mockModuleA); + + browser.get('index.html'); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.get('index.html#/repeater'); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.navigate().back(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.navigate().forward(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + }); + */ + + it('should load mock modules after navigating back and forward from link', function() { + browser.addMockModule('moduleA', mockModuleA); + + browser.get('index.html'); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + element(by.linkText('repeater')).click(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.navigate().back(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + + browser.navigate().forward(); + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + }); + });