Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 904
user2167582 Avatar asked Jan 27 '14 14:01

user2167582


People also ask

How do you use a headless browser on a protractor?

To start Chrome in headless mode, start Chrome with the --headless flag. As of Chrome 58 you also need to set --disable-gpu , though this may change in future versions. Also, changing the window size during a test will not work in headless mode, but you can set it on the commandline like this --window-size=800,600 .

What Browsers are supported by protractor?

Now let us see how to execute protractor tests on multiple browsers in parallel using multiCapabilities. multiCapabilities: [ {'browserName': 'chrome'}, {'browserName': 'firefox'}, ], If we add above parameter to conf. js file, your tests will run on chrome and Firefox browsers at the same time across browser.

What is protractor Conf JS?

Protractor is an end-to-end testing framework for AngularJS applications and works as a solution integrator — combining powerful tools and technologies such as NodeJS, Selenium, web driver, Jasmine, Cucumber and Mocha. It has a bunch of customizations from Selenium to easily create tests for AngularJS applications.


1 Answers

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):

Test

Happy testing!

like image 125
Damiya Avatar answered Nov 15 '22 08:11

Damiya