Protractor running tests on PhantomJS

2019-02-01 11:17发布

问题:

I can't seem to get PhantomJS through a test successfully. I tried to integrate it into my project, but after that failed I tried to just run the basic Angular Docs samples and I'm getting the same issue. My steps so far:

  • npm install -g phantomjs
  • phantomjs --webdriver=9515 // ... GhostDriver - Main - running on port 9515
  • protractor protractorConf.js

This is the same file from the example with only browserName, and seleniumAddress port changed:

// An example configuration file.
exports.config = {
  // The address of a running selenium server.
  seleniumAddress: 'http://localhost:9515',

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'phantomjs'
  },

  // Spec patterns are relative to the current working directly when
  // protractor is called.
  specs: ['onProtractorRunner.js'],

  // Options to be passed to Jasmine-node.
  jasmineNodeOpts: {
    showColors: true,
  }
};

I get the following error message:

UnknownError: Error Message => 'Detected a page unload event; asynchronous script execution does not work across page loads.'

I found this issue on github, which seemed to be related. I thought I had made enough sense of their brower-setup.md to include it in one of my beforeEach functions. Then I found here ptor is just wrapping the driver anyway. Wow, I know I'm a noob here in protractor/selenium land, but the signal to noise ratio is intensively dissuasive. I'd really like to get the performance benefits of using PhantomJS, but the prospect of losing several more hours on this is hurting my head. I'm on Windows 7 Enterprise 64-bit, in case that matters. Thanks!

回答1:

Acutally this fix was solving the same issue for me:

https://github.com/pschwartau/protractor/commit/1eeff8b1b2e3e8f3b7c8152264411f26d4665a07

As originally described here: https://github.com/angular/protractor/issues/85#issuecomment-26846255 by renanmartins


Inside protractor/lib/protractor.js Replace

this.driver.get('about:blank');
this.driver.executeScript(
    'window.name = "' + DEFER_LABEL + '" + window.name;' +
    'window.location.href = "' + destination + '"');

with

  var driver = this.driver;
  this.getCapabilities().then(function (capabilities) {
    if (capabilities.caps_.browserName === 'phantomjs') {
      driver.executeScript('window.name = "' + DEFER_LABEL + '" + window.name;');
      driver.get(destination);
    } else {
      driver.get('about:blank');
      driver.executeScript(
          'window.name = "' + DEFER_LABEL + '" + window.name;' +
          'window.location.href = "' + destination + '"');
    }

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