如何在截图名称中包含日期和时间的红宝石的selenium webdriver中截取屏幕截图?

时间:2016-09-26 10:05:53

标签: ruby selenium-webdriver testunit

我正在尝试使用当前日期和时间在每一步获取屏幕截图,但我收到错误

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

1 个答案:

答案 0 :(得分:3)

问题是Time.now返回的格式如'2016-09-28 04:45:40 +0000',这在Windows上不是有效的文件名。您可以将日期/时间重新格式化为有效的内容,例如

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