我如何从场景示例中提取日期编号并使用它在 adv 中移动我的日期 x 天,然后将其转换为 STRFTIME?

How can i pull a date's number from a scenario example and use it to move my date x day(s) in adv and then convert it to STRFTIME?

我无法理解如何从“从现在起 2 天”中删除数字“2”,然后使用它来将我的日期增加两天。所以像 date = (Date.today + 2).strftime('%a %-e') 也就是提前两天。

我的 'puts' 只是为了让我看看是否输出了正确的日期。在这种情况下,我遇到了第三种情况。

如您所见,我需要逐字地使日期等于 "Thur 26 Tasks"

And(/^I can see the correct task lists details for (.*)$/) do |date|

  sleep 1
  case
  when date == ("today")
    date = Date.today.strftime('%a %-e') + " Tasks"
    puts date
  when date == ("tomorrow")
    date = (Date.today + 1).strftime('%a %-e') + " Tasks"
    puts date
  when date.downcase.include?("days from now")
    date = ((Date.today + [/\d*/]) + " Tasks")
    puts date
  end
  TasksPage.todays_tasks_title.click unless exists {$driver.text(date)}

结束

给你的主意

date = "3 days from now"

date = "#{(Date.today + date.match(/\A\d+(?!days from now\z)/)[0].to_i).strftime('%a %-e')} Tasks" if date.match?(/\A\d+ days from now\z/)
# => "Fri 29 Tasks"

date.match?(/\A\d+ days from now\z/) returns truefalse

date.match(/\A\d+(?!days from now\z)/)[0] returns 带天数的子字符串 ("3").

这里使用了否定前瞻(?!...)

require 'date'

def doit(str)
  n = case str
  when /\btoday\b/
    0
  when /\btomorrow\b/
    1
  when /\bdays from now\b/
    str[/\d+/].to_i
  else
    nil
  end
  n.nil? ? nil : (Date.today+n).strftime('%a %-e Tasks')
end

doit "today"                 #=> "Tue 26 Tasks" 
doit "No rain today?"        #=> "Tue 26 Tasks" 
doit "todayornottoday"       #=> nil 
doit "tomorrow"              #=> "Wed 27 Tasks" 
doit "2 days from now"       #=> "Thu 28 Tasks" 
doit "1 Monday from now"     #=> nil