Error accessing input fields with Cucumber + Capybara

Asked

Viewed 370 times

1

I was writing my Steps for my tests with the Cucumber and they are not finding the ids, Labels or Names of the forms. I’ve looked at several tutorials on the internet and really my problem is very strange, because in all always the problem is the reference, and in this case, mine is correct.

Steps (The first step works correctly)

Dado(/^que eu esteja na página de criação de eventos$/) do
   visit "/events/new"
end

Quando(/^eu prencho os dados do evento corretamente$/) do
  fill_in "event_name", :with => "Nome do Evento"
end

Part of the form containing the input

<div class="field form-group">
  <%= f.label :Evento %><br>
  <%= f.text_field :name, :class => "form-control", :required => true  %>
</div>
<div class="field form-group">
  <%= f.label :Data_Evento %><br>
  <%= f.date_select :date_event, :class => "form-control", :required => true  %>
</div>
<div class="field form-group">
  <%= f.label :Destacar_Evento? %><br>
  <%= f.check_box :detach, {}, "1", "2" %>
</div>

1 answer

2

I created an environment to simulate your code here worked perfectly, see below the structure of the project:

    #./projeto/app.rb    
    #encoding: utf-8  
    require 'sinatra'

    get '/' do
      "home"
    end

    get '/events/new' do
      erb :index
    end

__END__
@@index
<!doctype html>
<html>
    <head>
    </head>
    <body>
      <form>
       <div class="field form-group">
          <label for="event_name">Event Name</label>
          <input name="event_name" id="event_name" type="text"/>
        </div>  
     </form>
  </body>
</html>

#./projeto/features/teste.feature
#language: pt
Funcionalidade: Preencher formulario
Dado que o quadro abaixo   

    Cenário:Preenchendo trecho de código
    Dado que eu esteja na página de criação de eventos
    Quando eu prencho os dados do evento corretamente

#./projeto/features/support/env.rb
#encoding: utf-8
require_relative '../../app'
require "capybara/cucumber"

Capybara.app = Sinatra::Application


#./projeto/features/step_definitions/teste_step.rb
#language: pt
Dado(/^que eu esteja na página de criação de eventos$/) do
    visit "/events/new"
end

Quando(/^eu prencho os dados do evento corretamente$/) do
  fill_in "event_name", :with => "Nome do Evento"
end

My tests were often breaking because there was no route that worked another time because the return ( both from Rails and from Sinatra ) did not find the variables were not located in the <%=f tag. algumacoisa%>, so it was not possible to find the fields ( since the page does not go through the rendering process ). Suddenly you can check the success of some of the elements that you think essential for your view. You can use query:

page.has_xpath?('//table/tr')

if the problem persists, send the codes of your file: env.Rb, views and Feature, ..., etc Abrasss

Browser other questions tagged

You are not signed in. Login or sign up in order to post.