4
votes

I have a series of automated tests in Ruby that use Capybara, Poltergeist and PhantomJS to interact with webpages. However, the problem is some of the pages are not working properly because jQuery does not appear to be getting loaded. For instance, on one webpage there is code like:

<script type="text/javascript">
<!--
  jQuery(window).load(function() {
    //Do Javascript-y things
  });
//-->
</script>

But this does not seem to work/run. From what I've read it appears that PhantomJS does not work naturally with jQuery (or at the very least it does not automatically load jQuery). How can I fix this. Keep in mind I can't change the webpage's actual code.

Summary: How can I add/inject/load jQuery when using PhantomJS, Poltergeist and Capybara?

EDIT: Here is how I'm using phantomjs. Notice how I never actually use phantomjs directly. The phantomjs executable is indirectly used by Poltergeist:

Capybara.register_driver :poltergeist do |app|
    driver = Capybara::Poltergeist::Driver.new(app,
                                      :debug => debug,
                                      :window_size => [1616, 1000],
                                      :js_errors => true,
                      :cookies => true,
    :phantomjs_options => ['--ignore-ssl-errors=true', 
               '--web-security=false', 
               '--local-to-remote-url-access=true', 
               '--proxy-type=none' ],:timeout => 180
       )       
1
I've never had this problem with apps that load jQuery. Are you using Rails? And what does the code / errors look like?CDub
No not using rails. The script (thats in the webpage) just doesn't run. I've included my example aboveNosrettap
You need to inject jQuery within the context of the page you are testing/scrapping or whatever you are doing. Here is a link on how to do so with PhantomjS: snippets.aktagon.com/snippets/…Nobita
The problem is that I'm using phantomjs indirectly through poltergeist so that it works nicely with Capybara. Therefore I don't really have direct access (as far as I know) to the page object. I've included my setup code above so that you can seeNosrettap
I think that Poltergeist doesn't wait for this snippet of code to be executed. Try to move load of jquery/your javascripts to head tag.Andrei Botalov

1 Answers

0
votes

Likely your jQuery needs time to execute. Add a find() statement in your test looking for a result of the jQuery. It will block, as described here: http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara