when running protractor with phantomjs browser, only able to run tests once

angularjs, angularjs-e2e, phantomjs, protractor

Solution

This is an issue with Protractor that was resolved in version 0.17 and made better in 0.18.

It's a bug with a long tail, but the TL;DR is that Protractor's .get(url) function actually uses client-side JavaScript to make the location change; this is to ensure it properly bootstraps. An unfortunate side effect of that design is that for some reason, PhantomJS takes a few seconds to navigate over properly.

The bug was resolved by adding a longer timeout to the .get function.

Github Issue: https://github.com/angular/protractor/issues/85

Relevant changelog entries:

v0.18

(10aec0f) fix(pageload): increase wait timeout

The 300 ms wait caused problems when testing IE on Sauce Labs. It seems way too short. "browser.get()" invariably timed out. Increasing it solved our problem.

v0.17

(a0bd84b) fix(pageload): add a wait during protractor.get() to solve unload issues

Some systems would not wait for the browser unload event to finish before beginning the asynchronous script execution.

Closes #406. Closes #85.

I've run your test locally (with a different page, but otherwise the same code):

Happy testing!

Problem

test code: describe('mysite', function(){ ``` var init_url = 'http://localhost/mySite/#/home'; beforeEach(function(){ // driver = new webdriver.Builder(). // withCapabilities(webdriver.Capabilities.phantomjs()).build(); }) it('should click on toolbox and do stuff', function(){ browser.get(init_url); browser.waitForAngular(); browser.getCurrentUrl().then(function(url){ console.log('current_url', url); expect(init_url).toEqual(init_url); }) expect(true).toBe(true); browser.sleep(2000); }) ``` result 1st time run, ``` Using the selenium server at http://localhost:9515 data Zoom Pad class active mysite should click on toolbox and do stuff Finished in 3.94 seconds 1 test, 4 assertions, 0 failures ``` 2nd time run, without any interruption, just up arrow and enter: ``` Stacktrace: Error: Error while running testForAngular: Error Message => 'Detected a pag e unload event; asynchronous script execution does not work across page loads.' caused by Request => {"headers":{"Accept":"application/json; charset=utf-8","Co nnection":"keep-alive","Content-Length":"689","Content-Type":"application/json;c harset=UTF-8","Host":"localhost:9515"},"httpVersion":"1.1","method":"POST","post ":"{\"script\":\"return (function () {\\n var attempts = arguments[0];\\n var callback = arguments[arguments.length - 1];\\n var check = function(n) {\\n try {\\n if (window.angular && window.angular.resumeBootstrap) {\\n callback([true, null]);\\n } else if (n < 1) {\\n if (window.angular ) {\\n callback([false, 'angular never provided resumeBootstrap']);\\n } else {\\n callback([false, 'retries looking for angular exceed ``` third time ``` 1) mysite should click on toolbox and do stuff Message: Error: ECONNREFUSED connect ECONNREFUSED Stacktrace: Error: ECONNREFUSED connect ECONNREFUSED at ClientRequest.<anonymous> (K:\Users\Congwen\AppData\Roaming\npm\node_modu les\protractor\node_modules\selenium-webdriver\http\index.js:127:16) at ClientRequest.EventEmitter.emit (events.js:95:17) at Socket.socketErrorListener (http.js:1547:9) at Socket.EventEmitter.emit (events.js:95:17) at net.js:441:14 ``` and on third time the phantomjs webserver is down, and needs to be reconnected, and afterwards it goes back to result 1: any clues? config file used: ``` exports.config = { seleniumAddress: 'http://localhost:9515', specs: [ './ptor-tests/mysite-test.js' ], capabilities: { browserName: 'phantomjs', version: '', platform: 'ANY' }, //baseUrl: 'http://testapp.example.com/index.html', rootElement: 'body', allScriptsTimeout: 11000, onPrepare: function () {}, jasmineNodeOpts: { onComplete: function () {}, isVerbose: true, showColors: true, includeStackTrace: true, defaultTimeoutInterval: 30000 } }; ``` also I noticed that sometimes there's no step 2 needed and it will go directly to ECONNECT error, and sometimes it gets stuck in step 2 for a number of tests and eventually will terminate phantomjs server.

Original source