Gabriel Pumple
Gabriel Pumple

Reputation: 823

cucumber capybara testing date picker selects wrong day

I am trying to write a cucumber test for a page that includes a datepicker. I swear this was working yesterday, but not so much today.

Then(/^select date (\d+) day(?:s|) prior to today$/) do |n|
  day=Date.today-(n.to_i)
  target = Date.strptime("#{day}",'%Y-%m-%d')
  target_month_year = target.strftime('%B %Y')
  selected_month_year = (find('.datepicker-switch').native.text)
        unless target_month_year == selected_month_year
          find('.prev').click
          sleep 1
        end
  find('.day', :text => "#{day.day}", match: :prefer_exact).click
  sleep 2
end

Then I have a separate test that checks that the correct date is presented after selection. I have verified that day.day is giving me the correct result by including a puts(day.day), as well as all the other variables. I think the problem is a matching issue, today's date is 04/24/2015 and I selecting 15 days prior. So the datepicker that displays the month and year above and allows you to select previous or next, then the days shown are according to how many days in that particular month. and a few day before and after. the days for the previous month are class="old day" and the ones for the month displayed are class="day" and for the next month class="new day". so the month I want is april and the day is the ninth. it keeps selecting the 29th of march. which is the first day listed on the page that contains a "9". but the class is wrong, since I want "day" not "old day" and the day is wrong because I want "9" not "29" I even put in a :prefer_exact because that has fixed matching the wrong element in the past for me.

Not sure what to try next. Any advice greatly appreciated.

cucumber 1.3.10

capybara 2.4.1

ruby 1.9.3p551

Upvotes: 0

Views: 795

Answers (1)

tgf
tgf

Reputation: 3266

Ideally, don't select by text if you can avoid it.

But in this case try using a regex instead of just plain text.

find('.day', :text => Regexp.new("^#{day.day}$"), match: :prefer_exact).click

There's a little related reading at the end of this (currently unimplemented) issue: https://github.com/jnicklas/capybara/issues/1256

Upvotes: 1

Related Questions