Javascript Rspec,Capybara:不明确匹配,找到2个元素匹配字段';简介';
我刚刚用Jquery实现了一个内联表单显示/隐藏函数,我的一些规范失败了。我对答案和问题发表评论,并且可以在回复其他评论时发表评论。显示表单时,“添加注释”链接将更改为“取消”,单击“取消”后,表单将隐藏 我的规格:Javascript Rspec,Capybara:不明确匹配,找到2个元素匹配字段';简介';,javascript,ruby-on-rails,rspec,capybara-webkit,Javascript,Ruby On Rails,Rspec,Capybara Webkit,我刚刚用Jquery实现了一个内联表单显示/隐藏函数,我的一些规范失败了。我对答案和问题发表评论,并且可以在回复其他评论时发表评论。显示表单时,“添加注释”链接将更改为“取消”,单击“取消”后,表单将隐藏 我的规格: scenario 'on a answer', js:true do submit_answer @answer.brief submit_comment @comment_brief, '.answers' expect(page).to have_con
scenario 'on a answer', js:true do
submit_answer @answer.brief
submit_comment @comment_brief, '.answers'
expect(page).to have_content(@idea_brief)
expect(page).to have_content(@question_brief)
expect(page).to have_content(@answer.brief)
expect(page).to have_content(@comment_brief)
end
scenario 'on a comment', js: true do
@commentreply = FactoryGirl.create(:comment)
submit_comment @comment_brief, ".question"
submit_comment @commentreply.brief, '.question-comments', 'Reply to comment'
expect(page).to have_content(@question_brief)
expect(page).to have_content(@comment_reply)
end
我假设这两个错误是相同的,可能与js内联表单hide/show有关
我的失败:
1) Visitor submits a comment on a answer
Failure/Error: submit_comment @comment_brief, '.answers'
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching field "Brief"
# ./spec/support/features/session_helpers.rb:52:in `submit_comment'
# ./spec/features/comment_spec.rb:17:in `block (2 levels) in <top (required)>'
2) Visitor submits a comment on a comment
Failure/Error: submit_comment @commentreply.brief, '.question-comments', 'Reply to comment'
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching field "Brief"
# ./spec/support/features/session_helpers.rb:52:in `submit_comment'
# ./spec/features/comment_spec.rb:33:in `block (2 levels) in <top (required)>'
此外,我还有一个等级库辅助函数:
def submit_comment(brief, selector='html', add_comment_link = 'Add comment')
first(selector).click_link add_comment_link
fill_in 'Brief', with: brief
click_button 'Create Comment'
end
我将find()
不知道是什么导致了这一切?当我在服务器上测试同样的东西时,我没有任何问题。如果有任何其他代码有助于解决此问题,请让我知道。谢谢。例外情况表明问题出在行的“简短”中,用:简短
。这意味着页面上有多个“简短”元素。您需要使定位器更加具体,以便只匹配一个定位器。如果您共享生成的html以及您(作为用户)将如何区分两者,我们可以帮助您提供更好的定位器。如果您将Capybara.match
设置为:首选确切的,那么会发生什么?
var selector = ".<%= j @commentable.class.to_s.downcase + '-' + @commentable.id.to_s + ' .media-body ' %>"
selector0 = selector + ".<%= j @commentable.class.to_s.downcase %>-comments"
var selector1 = ".<%= j @commentable.class.to_s.downcase%>"
var selector2 = ".<%= j @commentable.class.to_s.downcase + '-' + @commentable.id.to_s%>"
var selector3 = selector1 + selector2 + " div.<%= j @commentable.id.to_s%>"
if($(selector3 + " .add.comment").is(":visible")){
if ($(selector0 + " .js-inline-form").length == 0) {
$(selector0)
.append("<li class='js-inline-form'><%= j render :partial => 'comments/form' %></li>")
}
else {
$(selector0 + " .js-inline-form").remove()
$(selector0)
.append("<li class='js-inline-form'><%= j render :partial => 'comments/form' %></li>")
}
$(selector3 + " .add.comment").hide()
$(selector3 + " .comment.cancel").show()
}
else if($(selector3 + " .comment.cancel").is(":visible")){
$(selector0 + " li.js-inline-form").remove()
$(selector3 + " .comment.cancel").hide()
$(selector3 + " .add.comment").show()
}
def submit_comment(brief, selector='html', add_comment_link = 'Add comment')
first(selector).click_link add_comment_link
fill_in 'Brief', with: brief
click_button 'Create Comment'
end