1

I'm using Capybara to fill in a form and download the results.

It's a bit slow when filling in the form, and I want to check if JavaScript is the culprit.

How do I turn off JavaScript?

The Ruby code was something similar to, but not the same as, the following (the following won't reproduce the error message, but it is somewhat slow).

require "capybara"

url = "http://www.hiv.lanl.gov/content/sequence/HIGHLIGHT/highlighter.html"
fasta_text = [">seq1", "gattaca" * 1000, ">seq2", "aattaca" * 1000].join("\n")

session = Capybara::Session.new(:selenium)

# Code similar to this was run several times    
session.visit(url)
session.fill_in('sample', :with => fasta_text)
session.click_on('Submit')

And the error I was getting (with my real code, but not the code I have above) was

Warning: Unresponsive script

A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.

Script: chrome://browser/content/tabbrowser.xml:2884

I wasn't running Capybara as part of a test or as part of a spec.

To confirm that the code I wrote currently has JavaScript enabled (which is something I want to disable), doing

url = "http://www.isjavascriptenabled.com"
session = Capybara::Session.new(:selenium)
session.visit(url)

indicates that JavaScript is enabled.

sawa
  • 165,429
  • 45
  • 277
  • 381
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • To clarify: you want to use Selenium to drive a real browser, but with JavaScript disabled? By default, Capybara uses a RackTest driver that does not execute JavaScript, but it also does not drive a real browser. Would that suit? – Tim Moore Nov 04 '14 at 03:33
  • @TimMoore I'm not a web developer, but I assume a RackTest driver wouldn't work on a third party website (I don't run the hiv.lanl.gov site). – Andrew Grimm Nov 04 '14 at 06:37
  • Ah yes, you are correct. – Tim Moore Nov 05 '14 at 00:18
  • There's a related answer here http://stackoverflow.com/a/7492504/29470. The example there is for Java, but it should be translatable to Ruby/Capybara. I can try to write up a full answer when I have some more time, but maybe that will get you going. – Tim Moore Nov 05 '14 at 00:21
  • Here's another helpful blog post with some Ruby code http://yizeng.me/2014/01/08/disable-javascript-using-selenium-webdriver/ – Tim Moore Nov 05 '14 at 00:22

1 Answers1

1

Capybara only uses JavaScript if you've specified a javascript_browser:

Capybara.javascript_driver = :poltergeist

And if you've specified js: true as metadata in your spec:

context "this is a test", js: true do

Check for both of those things. If they're not there and the test is not running in a browser or using Poltergeist, then it's probably not using JavaScript.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • I've added more details to my question, including an almost-reproduction of the code. I wasn't running it in a spec. Also, even though I hadn't modified `javascript_driver=`, JavaScript seems to be enabled. – Andrew Grimm Oct 21 '14 at 02:17
  • @AndrewGrimm right, here is an open issue to add an option to disable javascript when using poltergeist https://github.com/teampoltergeist/poltergeist/issues/489 – Jahan Zinedine May 13 '16 at 11:47