PhantomJS JavaScript errors from gstatic, but not

2019-03-19 08:12发布

问题:

I recently ran our website through our PhantomJS testing suite and ran into JavaScript errors that I can't reproduce in my browser manually. These errors are found in the Google maps api and the text returned by Capybara is as follows:

TypeError: Unable to delete property.
TypeError: Unable to delete property.
   at :215
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:20 in cf
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:20 in cf
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:19
   at :214
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:20 in cf
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:20 in cf
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:21
   at :176
   at :31
   at https://maps.gstatic.com/maps-api-v3/api/js/19/3/main.js:26 in Yf
   at :178

Is this a known bug with Capybara, PhantomJS, or the Google maps API? Could the problem be caused by the user agent string in PhantomJS?

回答1:

Using the latest experimental version of Google Maps (v3.19), which includes a workaround, fixed this problem for me: https://code.google.com/p/gmaps-api-issues/issues/detail?id=7475#c20

[Edit] Google Maps v3.19 was released as the production version on 17 Feb 2015. Also, PhantomJS version 2 was released on 23 Jan 2015 incorporating an updated WebKit module which doesn't exhibit the Unable to delete property problem.



回答2:

I'm using Cucumber / Poltergeist and I hacked around this in by creating the following extension:

/features/support/env.rb

Capybara::Poltergeist::Driver.new(app, 
  :extensions => ["features/support/ignore_gmaps_errors.js"]
)

/features/support/ignore_gmaps_errors.js

window.onerror = function(message) {
  if (message == 'TypeError: Unable to delete property.') {
    console.log('Ignoring gmaps error');
    return false;
  } else {
    return true;
  }
};


回答3:

Google maps API experimental version was updated this morning (12/11) causing this breakage. By default when you include:

<script src="//maps.googleapis.com/maps/api/js?libraries=places"></script>

it uses the latest experimental version. Locking yourself to the latest release version:

<script src="//maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>

should fix this.



回答4:

Try setting the user agent to

Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1

Google delivers JavaScript that is not entirely runnable with its default user agent. PhantomJS 1.x is based on a really old fork of QtWebKit which is comparable to Chrome 13 (the reason for that user agent string).

It looks like you can do this this way:

@session = Capybara::Session.new(:poltergeist)
@session.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1' }