[英]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! 任何帮助表示赞赏!
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中“”
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.