Search code examples
datetestingrspeccapybaraapache-cocoon

RSpec - select date cocoon gem


I am doing some testing regarding my rails app. I am using the gem cocoon regarding some forms. I have some troubles testing the "date selection". I would like to know what I am doing wrong, your help will be much appreciated!!

Please find here below what I have done so far :

Nested fields for Job form ( I am trying to test the input field started_at)

    .nested-fields
      = f.input :company_name,  label:'Nom de entreprise', input_html: {class: "form-control job-company-name"}
      = f.input :title, label: 'Votre fonction', input_html: {class: "form-control job-title "}
      = f.input :location, label: 'Lieu de travail', input_html: {class: "form-control job-location"}
      = f.input :started_at, discard_day: true, order: [:month, :year], include_blank: true, label:'Date début',input_html: {class: "work-started-at job-started-at"}, start_year: 1970, end_year: 2016, :id => 'task_target_date'
      = f.input :finished_at, discard_day: true, order: [:month, :year], label: 'Date fin ( mettre le mois en cours si c\'est votre emploi actuel)',input_html: {class: "work-finished-at end_date job-finished-at"},start_year: 1970, end_year: 2016, :default => Time.now   
      = f.input :description, label: 'Vos responsabilités',:input_html => {:rows => 9, style: 'max-width:100%;', class: ""}
   .remove-experience.text-center
      = link_to_remove_association "supprimer cette expérience", f, class: 'btn btn-delete form-button'

RSpec and Capybara

require 'rails_helper'

RSpec.feature "Edit Profile" do

  scenario "Profile updated successfully ", js: true do 
    Given "user visits profile edit page"
    When "user updates profile basic info"
    When "user updates work experiences info"
    When "user updates education info"
    Then "user views profile updated"
  end



  def user_updates_work_experiences_info
    click_link "Parcours professionnel"
    click_link "Ajouter une expérience"
    find(".job-company-name").set("Natixis")
    find(".job-title").set("Contrôleur Financier")
    find(".job-location").set("Paris")
    select_date("2013,December,7", :from => "Date début")
  end


  def select_date(date, options = {})
    field = options[:from]
    base_id = find(:xpath, ".//label[contains(.,'#{field}')]")[:for]
    year, month, day = date.split(',')
    select year,  :from => "#{base_id}_1i"
    select month, :from => "#{base_id}_2i"
  end


end

When I run the test, I have the following message :

Failure/Error: select year,  :from => "#{base_id}_1i"
 Capybara::ElementNotFound:
   Unable to find select box "profile_experiences_attributes_1467917726232_started_at_2i_1i"

Thank you guys ;)


Solution

  • It is finally working. I first checked the rails console to view what was the value of the variable base:

    profile_experiences_attributes_1467922079698_started_at_2i
    

    I have removed the last 3 characters of the variable with the following code :

    base_id = base[0...-3]
    

    And updated the select_date method :

     def select_date(date, options = {})
        field = options[:from]
        base = find(:xpath, ".//label[contains(.,'#{field}')]")[:for]
        base_id = base[0...-3]
        year, month, day = date.split(',')
        select month, :from => "#{base_id}_2i"
        select year,  :from => "#{base_id}_1i"
      end
    

    Green light everywhere!!! Much appreciated your assistance Tom