如何使用 ruby 在 selenium webdriver 中截取屏幕截图名称中包含日期和时间的屏幕截图?

How to take screenshot in selenium webdriver with ruby with date and time included in screenshot name?

我想在每个步骤中获取当前日期和时间的屏幕截图,但出现错误

Error: test_login(Login_page): Argument Error: wrong number of arguments (1 for 0)

密码是

 def setup
     @driver = Selenium::WebDriver.for :chrome
     @driver.manage.window.maximize
     @driver.navigate.to "https://www.findmedecor.com"

      wait = Selenium::WebDriver::Wait.new(:timeout => 10)

    screenshot()
  end

  def test_login

    @driver.find_element(:class,'open-overlay').click
    screenshot(DateTime.now)
    wait = Selenium::WebDriver::Wait.new(:timeout => 10)
    login_email = wait.until {
        element = @driver.find_element(:name, "login_email")
        element if element.displayed?
    }

    login_email.send_keys("suwarna.wade@rohagroup.com")
    puts "Test Passed: login pop up found" if login_email.displayed?
    screenshot(DateTime.now)
    @driver.find_element(:id,'pass').send_keys('123456')
    @driver.find_element(:id,'btn_login').click

    puts "Logged in successfully"

    puts "Time of test = ", DateTime.now
    screenshot(DateTime.now)
  end
  $i = DateTime.now
  def screenshot
  @driver.save_screenshot("screenshot #{'$i'}.png")
    $i= +1
  end

end

问题是 Time.now returns 类似 '2016-09-28 04:45:40 +0000' 的格式在 Windows 上不是有效的文件名。您可以将 date/time 重新格式化为有效的格式,例如

Time.now.strftime('%Y-%m-%d_%H.%M.%S')

输出 2016-09-27_23.33.59 然后把它放在你的文件名中。

http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime