Intern WebDriver and PhantomJS

2019-09-14 03:04发布

问题:

I've written my main unit tests while testing in the browser (client.html), and now I'm ready to setup test automation through Node, however I don't have a Selenium server setup yet, and management will probably never give the okay to use a third party service like Sauce Labs, so I'm trying to run Intern test through PhantomJS using it's webdriver mode.

I'm running PhantomJS with these options:

$ phantomjs --ignore-ssl-errors=true --web-security=false --webdriver=8910

My Intern configuration looks like this:

define({
  proxyPort: 9000,
  proxyUrl: 'http://localhost:9000/',
  environments: [
    {
      browserName: 'phantom',
      version: '1.9.0',
      platform: 'Linux'
    }
  ],
  webdriver: {
    host: 'localhost',
    port: 8910
  },
  maxConcurrency: 3,
  useSauceConnect: false,
  // ...
});

It seems to work for the most part, however my I am having issues with AJAX requests in my tests. Since tests are run through Intern's own server, I've added an AJAX filter to make sure requests are sent to the correct place:

$.ajaxPrefilter(function (options) {
  options.url = serverName + options.url;
});

I'm still finding that my AJAX requests are failing however. Upon further inspection I've found that the requests aren't being sent (AJAX fails with a readyState of 0). PhantomJS doesn't seem to provide any more information than this. The --web-security=false flag for phantomjs should ensure that cross-domain requests are possible, so I'm unsure of the cause.

No specific error is thrown in PhantomJS, the only information given to the fail handler is a status of "error".

EDIT: I've tried doing a manual XMLHttpRequest without jQuery, and it works successfully. So clearly this is something on jQuery's end. Enabling the crossDomain doesn't fix it.

回答1:

So, for reasons I haven't quite figured out yet, using method instead of type in the AJAX config resolved the issue (I stumbled upon that by accident). I can't quite see why, since method is just an alias for type in jQuery:

s.type = options.method || options.type || s.method || s.type;

Regardless, this did fix the issue for me.