简体   繁体   English

使用Capybara,Selenium和Rspec测试UJS Rails

[英]Tests UJS Rails with Capybara, Selenium and Rspec

On my page I have : 在我的页面上,我有:

-->a form that scrapes the content of another website with a Ajax request made with ujs ( remote: true ): ->一种表单,该表单使用ujs( remote:true )发出的Ajax请求来抓取另一个网站的内容:

<%= simple_form_for @listing, :url => listings_fetch_url, remote: true do |f| %>
  <%= f.input :url %>
  <%= f.button :submit, id: 'import_button' %>
<% end %>

-->another form that is populated by the scraping results from the form above ->上面表格中的抓取结果填充的另一表格

Manually it works... 手动工作...

However when I'm trying to automatically test it , it doesn't. 但是,当我尝试自动测试时 ,不是。 Capybara finds the submit button, clicks on it but then it leaves me on a blank page. Capybara找到提交按钮,单击它,但是随后我留在空白页面上。

Here is the code for the test : 这是测试代码:

feature "Listing import" do
  scenario "fills in the form fields", js: true do
    visit new_search_path
    fill_in 'Url', :with => 'www.google.fr'

    find('input#import_button').click

    expect(page.body).to have_content 'query was'
  end
end

The controller actions are the following: 控制器动作如下:

def fetch
    url = listing_params[:url] 
    @query = Query.new(ScraperService.new({url: url}).perform)
    render :fill
end

# Renders an js.erb file
def fill
end

It seems like rspec with selenium can't render js.erb files? 似乎带有硒的rspec无法呈现js.erb文件?

Any help is appreciated! 任何帮助表示赞赏!

EDIT 编辑

Thanks to @ThomasWalpole, I fixed an error 500 which was preventing Mechanize to scrape the web. 感谢@ThomasWalpole,我修复了一个错误500,该错误阻止Mechanize刮擦网络。 The content is now scraped correctly but the problem remains: the page after clicking on the submit button becomes blank and the tests ends right afer. 现在可以正确抓取内容了,但问题仍然存在:单击“提交”按钮后的页面将变为空白,并且测试随后立即结束。 In the logs I have a lengthy message : 在日志中,我有一条冗长的消息:

Could not log "render_template.action_view" event. 无法记录“ render_template.action_view”事件。 NoMethodError: undefined method example_group' for nil:NilClass ["/Users/Greg/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rspec-rails-3.6.0/lib/rspec/rails/view_rendering.rb:67:in current_example_group'" NoMethodError: example_group' for nil:NilClass ["/Users/Greg/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rspec-rails-3.6.0/lib/rspec/rails/view_rendering.rb:67:in current_example_group中“”

Which seems to be the same problem as : 1 & 2 这似乎是与12相同的问题

Solved it! 解决了!

The problem came from the fact that my js.erb wasn't adding a new element on the current page (ie: a new class, a new id, a new DOM element...). 问题出在我的js.erb 没有在当前页面上添加新元素的事实(即:新类,新id,新DOM元素...)。

Capybara never waited for the Ajax request to complete! 水豚从未等待Ajax请求完成!

I appended a new line to my js.erb: 我在js.erb上添加了新行:

$("#import_button").addClass("imported");

This way I know that when the class imported is present on the current page it means the Ajax request is over. 这样,我知道当当前页面上存在导入的类时,这意味着Ajax请求已结束。

Then in my test I added the following line: 然后在我的测试中,添加了以下行:

expect(page).to have_selector :css, '#import_button.imported', wait: 10

Everything works fine now 🤘 现在一切都很好works

PS: this website helped me understand the problem as well PS: 这个网站也帮助我理解了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript在Rails Rspec Capybara测试中停止工作 - Javascript stopped working in Rails Rspec Capybara tests Rails 4 Rspec Capybara / Selenium:内部服务器错误 - Rails 4 Rspec Capybara/Selenium : Internal server error Stub Rails UJS / Ajax响应状态以测试Rspec / Capybara功能测试中的返回消息 - Stub Rails UJS/Ajax responses status to test returning message in Rspec/Capybara feature test Rspec / Capybara测试不一致 - inconsistent Rspec/Capybara tests database_cleaner无法清除我的一些测试(rspec,水豚,硒) - database_cleaner not clearing some of my tests (rspec, capybara, selenium) 用Rails编写capybara / rspec测试…称为id表示无错误 - Writing capybara/rspec tests with Rails… called id for nil error Rails Rspec Capybara和DatabaseCleaner-仅在功能测试中使用截断 - Rails Rspec Capybara and DatabaseCleaner - using truncation only on feature tests Capybara在请求时发送HTML格式,而不是在RSpec测试中发送JS(Rails) - Capybara is sending HTML format on request instead of JS on RSpec tests (Rails) 如何使用Rails 3 Rspec和Capybara访问Selenium 2的默认webdriver? - How can I access the default webdriver for Selenium 2 with Rails 3 Rspec and Capybara? Rails / RSpec / Capybara-无事务的数据库清理可与Selenium一起使用,但不适用于Webkit - Rails/RSpec/Capybara - transactionless database cleaning works with Selenium but not Webkit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM