如何在我的代码中将日期添加为变量字段以在文本字段中打印 (Ruby)
How to add Date as a variable field in my code to print in a text field (Ruby)
我正在使用 Ruby、Cucumber、Watir-webdriver 等编写一个快速自动化脚本。我正在创建一个广告活动并想要活动的标题(我可以在其中输入任何内容的文本字段) 来包含今天的日期时间。目前我的代码如下:
When(/^I enter "([^"]*)" into the campaign name field$/) do |arg1|
@browser.textarea(:id => 'campaign_name').set('SBTC - Regression - Campaign Create-' Time.now.strftime("%d/%m/%Y %H:%M"))
end
显然那是不正确的,我在论坛上没有看到任何关于如何做到这一点的信息。最终结果是,当自动化完成时,我有一个新的广告活动标题 "SBTC - Regression - Campaign Create - [TodayDateTime]"
您需要interpolate Time.now.strftime
方法以便代码在字符串内执行。
在ruby中,这通常是通过将要执行的代码放在花括号中来完成的,花括号前面是一个用双引号引起来的字符串。例如:
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto "google.com"
b.text_field(:name => 'q').set("example of string interpolation: #{Time.now.strftime('%d/%m/%Y %H:%M')}")
我正在使用 Ruby、Cucumber、Watir-webdriver 等编写一个快速自动化脚本。我正在创建一个广告活动并想要活动的标题(我可以在其中输入任何内容的文本字段) 来包含今天的日期时间。目前我的代码如下:
When(/^I enter "([^"]*)" into the campaign name field$/) do |arg1| @browser.textarea(:id => 'campaign_name').set('SBTC - Regression - Campaign Create-' Time.now.strftime("%d/%m/%Y %H:%M")) end
显然那是不正确的,我在论坛上没有看到任何关于如何做到这一点的信息。最终结果是,当自动化完成时,我有一个新的广告活动标题 "SBTC - Regression - Campaign Create - [TodayDateTime]"
您需要interpolate Time.now.strftime
方法以便代码在字符串内执行。
在ruby中,这通常是通过将要执行的代码放在花括号中来完成的,花括号前面是一个用双引号引起来的字符串。例如:
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto "google.com"
b.text_field(:name => 'q').set("example of string interpolation: #{Time.now.strftime('%d/%m/%Y %H:%M')}")