Capybara测试失败,因为无法找到元素

时间:2017-07-07 19:51:17

标签: ruby-on-rails rspec capybara

我正在为Rails 5应用程序编写RSpec Capybara测试。我的应用程序是学生学习如何编写HTML和CSS。应用程序创建“测试”对象,然后允许用户进行测试。每次用户正确回答问题时,应用程序都会添加测试对象的“得分”属性。最后,应用程序为用户提供最终得分,然后记录测试结果。

我的Capybara测试失败了,因为他们无法找到按钮。我的应用程序的工作方式,用户回答问题,然后单击“提交”按钮。只有在他们提交答案后,才会出现一个SECOND按钮,上面写着“下一个问题”。

我的测试找不到第二个按钮。我不是测试专家,可以使用一些帮助。我试过“睡5”,但没办法。

这是错误:

1)用不正确的答案进行测试      失败/错误:click_button'下一个问题'

 Capybara::ElementNotFound:
   Unable to find button "Next Question"
 # ./spec/features/taking_test_spec.rb:12:in `block (2 levels) in <top (required)>'

这是我的测试:

require 'rails_helper'

feature "taking test" do 

    scenario "with incorrect answers" do 
        visit '/'
        click_link "HTML Test"
        click_button "Start"
        fill_in "answer", with: '<p>'
        click_button 'Submit'
        sleep 5
        click_button 'Next Question'
        expect(page).to have_content('CSS helps you control the appearance of HTML elements.')
    end

end  

以下是测试控制器的相关部分:

class TestsController < ApplicationController
before_action :authenticate_user!, only: [:destroy]

def index 
    @test = Test.new 
    @tests = Test.all.order(created_at: :desc).paginate(:page => params[:page], :per_page => 4)
end 

def question1 
    @test = Test.find(params[:id])
    correct_answer = "<p>"
    user_answer = params[:answer]
    if user_answer == correct_answer
        flash.now[:success] = "That is correct!"
        new_score = @test.score += 1
        @test.update(score: new_score)
    elsif params[:answer].present? && params[:answer] != correct_answer
        flash.now[:danger] = "Wrong answer." 
    end
end

以下是问题1的视图,包括Capybara无法找到的按钮:

<div class="container-fluid">

    <div class="row background-white">
        <div class="col-md-8 col-md-push-2 score_container">
            <h5 class="text-center">Current Score: <%= @test.score %></h5>
        </div>

        <div class="col-sm-6 col-sm-push-3 col-xs-10 col-xs-push-1 question_container">
            <div class="form_group">
            <%= form_tag question1_path(@test), method: :get do %>
                <h5>1. How do you write an opening paragraph tag in HTML?</h5>
                <div class="form-group">
                    <%= text_area_tag :answer, params[:answer], class: 'form-control', id: 'answer' %>
                </div>
                <% if !flash[:success] && !flash[:danger] %>
                <div class="form-group">
                    <%= submit_tag "Submit", class: "btn btn-primary" %>
                </div>
                <% end %>
            <% end %>
            </div>

            <% if flash[:success] || flash[:danger] %>
                <%= link_to "Next Question", question2_path(@test), class:"btn btn-success pull-right" %>
            <% end %> 
        </div>
    </div>
</div> 

1 个答案:

答案 0 :(得分:1)

您的Next Question不是一个按钮,它是一个链接(&lt; a&gt;元素具有href属性 - 恰好被设计为看起来像一个按钮,但它不是一个按钮)。 Capybara将按钮定义为[提交,重置,图像,按钮类型]或按钮元素的输入。您需要使用click_link来点击链接,或click_link_or_button如果您不关心它是什么。

visit '/'
click_link "HTML Test"
click_button "Start"
fill_in "answer", with: '<p>'
click_button 'Submit'
click_link 'Next Question'
expect(page).to have_content('CSS helps you control the ap