2

ruby '~> 2.6.1' rails 5.2.3

I have a feature spec that requires executing some javascript in order to populate a an alt field of a datepicker.

the spec:

require 'rails_helper'
Capybara.default_driver = :selenium
Capybara.javascript_driver = :selenium

RSpec.feature "Guest user creates a new order", js: true do

  scenario "order is valid" do
    area = create(:coverage_area)
    visit root_path

    click_on "Book Now"

    fill_in "zipcode", with: "98168"

    page.execute_script('document.querySelector(".datepicker").value = "9/20/2020"')
    .
    .
    .
  end

end

I've pinpointed that enabling js:true on my feature spec immediately causes the following error on the first scenario action:

1) Guest user creates a new order order is valid
     Failure/Error: visit root_path

     FrozenError:
       can't modify frozen String
`block (2 levels) in <top (required)>'

My Gemfile:

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
  gem 'rails-controller-testing'
  gem 'guard-rspec'
end


group :test do
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  gem 'webdrivers', '~> 4.0'
  gem "database_cleaner"
  gem "factory_girl_rails"
  gem "shoulda-matchers"
end

My spec_helper.rb

require 'simplecov'
require 'webdrivers'
.
.
.

After some research, I learned that capybara uses rack test driver by default which doesn't support javascript, however going through the steps of configuring capybara to use :selenium for this spec test has no effect on the error.

Mike Ray
  • 91
  • 2
  • 10
  • Please show the stacktrace for the error so we can see where it’s coming from – Thomas Walpole May 31 '19 at 17:15
  • Also, as a separate note - what library are you using for your `.datepicker` widgets - there has to be a better method to set them instead of using `execute_script` (which should really be the absolute last resort) – Thomas Walpole May 31 '19 at 17:46
  • Thanks for your response. To answer your first question, there is no visible stack trace for me to include. All I get is an error. I'm using a jquery-ui component with some configuration to populate an alt field in order to display human-readable date format in the visible field while populating the alt field with valid DateTime format for submitted params. – Mike Ray May 31 '19 at 17:54
  • Without a stacktrace it's really not possible to figure out what the error is - so maybe look at your configs to see if you can figure out why it's not showing you a stacktrace - check if you've set your RSpec `config.full_backtrace = false` and make it true - or try running rspec with the `--backtrace` option, etc. On the datepicker - if https://jqueryui.com/datepicker/ is what you're using you should just be able to use normal fill_in on the visible text field rather than using execute_script. – Thomas Walpole May 31 '19 at 18:00
  • Thanks for your reply. Filling in either the user-visible field `datepicker` or the hidden_field `pick_up_date` gives me the error: `Capybara::ElementNotFound: Unable to find field "_" that is not disabled` – Mike Ray May 31 '19 at 18:13
  • Then you're not passing something to fill_in that's actually correctly identifying the visible field (maybe you need to click something before the field actually becomes visible?)- without seeing the html I can't really diagnose that more - were you able to get a stacktrace for the other issue? – Thomas Walpole May 31 '19 at 18:14
  • I appreciate your help, but having reverted to a previous configuration (that doesn't use webdrivers or Capybara driver configuration) in order to test `fill_in`, it has put my question outside of it's original context and adding html blocks won't be relevant to the original question. I'd prefer to use `fill_in` over javascript honestly. – Mike Ray May 31 '19 at 18:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194264/discussion-between-michael-arriaga-and-thomas-walpole). – Mike Ray May 31 '19 at 18:35
  • Did you find a solution? I have the same problem and cant find a solution, thanks! – reves Aug 08 '19 at 15:06
  • I literally get this error from just calling `visit root_path` with capybara & selenium, the focus on `fill_in` is a red herring. – Kelsey Hannan Aug 20 '19 at 23:21

1 Answers1

1

YMMV but, for my coworker and I reinstalling firefox and geckodriver https://github.com/mozilla/geckodriver/releases worked for us.

rjspotter
  • 89
  • 1
  • 8