2016-11-15 22 views
0

我正在使用Ruby,Cucumber,Watir-webdriver等編寫快速自動化腳本。我正在製作廣告活動並希望標題(我可以輸入任何內容的文本字段)來包含今天的日期時間。目前我的代碼如下所示:如何在我的代碼中添加Date作爲變量字段以在文本字段中打印(Ruby)

 
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 - 迴歸 - 廣告系列創建 - [TodayDateTime]」

回答

1

您需要interpolateTime.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')}") 
+0

這樣做,謝謝! – SChen

相關問題