1

I'm trying to create this scenario:

scenario 'creating a new item category' do
  page.find("#launch-category-form-modal").click
  expect(page).to have_selector("#category-form-modal.active")
  fill_in("item_category[name]", with: "pudim")
  find("#submit-category-form-modal").click
  within ".sweet-alert.visible" do
    find('button.confirm').trigger('click')
  end
  expect(page).to have_selector(".category-line:last-child td:first-child",
                                text: "pudim")
end

The flow is:

  1. the user fill the input with a name;
  2. click the submit button;
  3. the sweet alert appear with a success message;
  4. the click in 'ok' inside the sweet alert;
  5. a new line is created with jQuery and append to the html table.

The JS code thats responsible for this flow is:

$.ajax({
  url: "/admin/item_categories",
  method: "POST",
  data: data,
  success: function(data) {
    if (data.status == true) {
      swal(
        'Success',
        'Item Category created successfully',
        'success'
      );
      var newLine = createItemCategoryLine(data.itemCategory);
      $("#item-category-table-body").append(newLine);
      $("#category-form-modal").modal('hide');
      $("form[action='/admin/item_categories']")[0].reset();
    } else if(data.status == false) {
      swal(
        'Error',
        'Something went wrong!',
        'error'
      )
    }
  }

After clicking the submit button, Capybara can't find the sweet alert. I tried used save_and_open_screenshot and the sweet alert don't appear in screen. I also tried to byebug in the controller but it's not called.

I'm using :selenium_chrome.

Thanks in advance for any help!

Edit 1

After a few comments of @ThomasWalpole, I tried to discover why <%= csrf_meta_tag %> don't rendered the corresponding tags. I discovered that csrf_meta_tag just call protect_against_forgery? to check if config.action_controller.allow_forgery_protection is true. Since I'm running in test environment, config.action_controller.allow_forgery_protection is false. I changed to true and re-run the tests. The csrf meta tags was rendered, but I'm still getting http status 405 - method not allowed after the ajax call.

Edit 2

My routes as @ThomasWalpole requested:

item_categories_path    GET /admin/item_categories(.:format)    
item_categories#index

create_item_category_path   POST    /admin/item_categories(.:format)    
item_categories#create

update_item_category_path   PATCH   /admin/item_categories/:id(.:format)    
item_categories#update

delete_item_category_path   DELETE  /admin/item_categories/:id(.:format)    
item_categories#destroy 

Edit 3

My capybara configs:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "spec_helper"
require "rspec/rails"
# Add additional requires below this line. Rails is not loaded until this point!
require "capybara/rspec"

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

Capybara.register_driver :selenium_chrome do |app|
 Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.javascript_driver = :selenium_chrome

# To ensure that browser tests can find the test server process,
# always include the port number in URLs.
Capybara.always_include_port = true
Rodrigo Chaves
  • 1,094
  • 8
  • 23
  • Add a `sleep 5; byebug` in the test after `find("#submit-category-form-modal").click` and look at the browser and see what's going on. The developer console might show an error explaining why the sweet alert isn't opening, and you can try clicking the button yourself to see if it works. Additionally, the selenium driver doesn't support `trigger` so `find('button.confirm').trigger('click')` will need to change to `find('button.confirm').click` – Thomas Walpole Aug 04 '17 at 19:01
  • I'm getting error 405. But I can't explain why. My form, in test env, needs the `hidden_input[name='authenticity_token']` ? – Rodrigo Chaves Aug 04 '17 at 19:20
  • Thats the CSRF protection - which rails automatically handles when you use UJS remote forms and the relevant events - http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#dealing-with-ajax-events. Since you're using rails 5.1 the UJS no longer requires JQuery so it may not be adding it automatically. Make sure you have `<%= csrf_meta_tag %>` in your layout header and if that doesn't fix it you'll need to manually add a header to your `$.ajax` call - see https://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails – Thomas Walpole Aug 04 '17 at 20:04
  • I made sure that `<%= csrf_meta_tag %>` is in `application.html.erb` but the tag isn't rendered in test environment, so `$('meta[name="csrf-token"]').attr('content')` returns `undefined`. – Rodrigo Chaves Aug 05 '17 at 20:22
  • I also have a question: protection against forgery requests should be disabled since I have this `config.action_controller.allow_forgery_protection = false` in `config/environments/test.rb`? – Rodrigo Chaves Aug 05 '17 at 20:26
  • What routes do you have that correspond to `/admin/item_categories`. You can run `rails routes` to see all your routes. – Thomas Walpole Aug 06 '17 at 00:08
  • Added as an edit in the question. Thanks a lot for helping Thomas! – Rodrigo Chaves Aug 06 '17 at 17:42
  • What version of JQuery are you using (do you need to use `type` instead of `method`)? Please post the full log from `test.log` for the request that's generating the 405. Additionally, you haven't done something silly like specify `Capybara.server_port = 3000` correct? – Thomas Walpole Aug 06 '17 at 18:30
  • I'm using `gem 'jquery-rails', '~> 4.3', '>= 4.3.1'`. As I asked [here](https://stackoverflow.com/questions/45314385/rails-5-capybara-test-log), the `test.log` isn't very helpful. Anyway, I posted the log [here](https://pastebin.com/WbKWU8eD). And I didn't have set `Capybara.server_port = 3000`. – Rodrigo Chaves Aug 06 '17 at 18:43
  • Are you sure you're actually using :selenium_chrome? Is it opening the browser when running the test? and show your full Capybara config. – Thomas Walpole Aug 06 '17 at 18:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151215/discussion-between-rodrigo-chaves-and-thomas-walpole). – Rodrigo Chaves Aug 06 '17 at 18:59

0 Answers0