【问题标题】:Capybara test failing because element can not be foundCapybara 测试失败,因为找不到元素
【发布时间】:2017-12-12 05:13:50
【问题描述】:

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

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

我的测试找不到第二个按钮。我不是测试专家,可以使用一些帮助。我尝试了“睡眠 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> 

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    您的Next Question 不是按钮,它是一个链接(具有 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
    

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 2015-11-15
      相关资源
      最近更新 更多