[英]cucumber asynchronous javascript error
Whenever the "save" button from compositions/index.html.erb is pressed, an ajax request is being completed and a row is appended that shows the Composition.content string in that same page. 每当按下“ compositions / index.html.erb”中的“保存”按钮时,就会完成ajax请求,并附加一行以在同一页面中显示Composition.content字符串。 But my cucumber tests shows an error:
但是我的黄瓜测试显示了一个错误:
Feature: User creates compositions
Scenario: User creates a composition # features/composition.feature:2
Given I go to the compositions page # features/step_definitions/composition_steps.rb:1
And I fill in "Content" with "My Globe number: +63-916-475-4261" # features/step_definitions/composition_steps.rb:5
When I press "Save" # features/step_definitions/composition_steps.rb:9
Then I should see "My Globe number: +63-916-475-4261" added in the compositions page # features/step_definitions/composition_steps.rb:18
Unable to find xpath "/html" (Capybara::ElementNotFound)
(eval):2:in `text'
./features/step_definitions/composition_steps.rb:22:in `/^I should see "(.*?)" added in the compositions page$/'
features/composition.feature:6:in `Then I should see "My Globe number: +63-916-475-4261" added in the compositions page'
Failing Scenarios:
cucumber features/composition.feature:2 # Scenario: User creates a composition
1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m0.904s
Is the error caused because the added row isn't yet rendered at that point? 是因为此时尚未渲染添加的行而导致错误? Please give me some answers.
请给我一些答案。 Thanks!
谢谢!
Below are the involved files. 以下是涉及的文件。
composition.feature 组成特征
Feature: User creates compositions
Scenario: User creates a composition
Given I go to the compositions page
And I fill in "Content" with "My Globe number: +63-916-475-4261"
When I press "Save"
Then I should see "My Globe number: +63-916-475-4261" added in the compositions page
composition_step.rb composition_step.rb
Given /^I go to the compositions page$/ do
visit compositions_path
end
Given /^I fill in "(.*?)" with "(.*?)"$/ do |arg1, arg2|
fill_in(arg1, :with => arg2)
end
When /^I press "(.*?)"$/ do |arg1|
click_button 'Save'
end
Then /^I should see "(.*?)" added in the compositions page$/ do |arg1|
page.should have_content(arg1)
end
compositions/index.html.erb composition / index.html.erb
<%= form_for @new_composition, remote: true do |f| %>
<%= f.label :content %>
<%= f.text_field :content %>
<%= f.submit 'Save', id: "composition_submit" %>
<% end %>
<h1>Listing compositions</h1>
<table id="compositions_table">
<% @compositions.each do |composition| %>
<tr>
<td><%= composition.content %></td>
</tr>
<% end %>
</table>
compositions_controller.rb composition_controller.rb
class CompositionsController < ApplicationController
def index
@new_composition = Composition.new
@compositions = Composition.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @compositions }
end
end
def create
@composition = Composition.new(params[:composition])
respond_to do |format|
if @composition.save
format.js
format.json { render json: @composition, status: :created, location: @composition }
else
format.js
format.json { render json: @composition.errors, status: :unprocessable_entity }
end
end
end
end
create.js.erb create.js.erb
$('#compositions_table').prepend("<%= j(render('compositions/composition', composition: @composition)) %>")
try this in composition_step.rb 在composition_step.rb中尝试一下
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
fill_in(field, :with => value)
end
When /^(?:|I )press "([^\"]*)"$/ do |button|
click_button(button)
end
Then /^I should see "([^"]*)" within "([^"]*)"$/ do |text, selector|
find(:xpath, "//#{selector}[contains(text(),'#{text}')]").should_not(be_nil, "Could not find the text '#{text}' within the selector '#{selector}'")
end
Then /^I should see "(.*?)" added in the compositions page$/ do |text|
page.should have_content(text)
end
Scenario like this 像这样的场景
Scenario: User creates a composition
Given I go to the compositions page
When I fill in "Content" with "My Globe number: +63-916-475-4261"
And I press "Save"
Then I should see "My Globe number: +63-916-475-4261" added in the compositions page
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.