简体   繁体   English

如何使用Ruby在Selenium Webdriver中使用屏幕快照名称中包含日期和时间的屏幕快照?

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

I am trying to get a screenshot at every step with the current date and time, but I am getting the error 我正在尝试获取包含当前日期和时间的每一步的屏幕截图,但出现错误

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

The code is 该代码是

 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

The problem is that Time.now returns a format like '2016-09-28 04:45:40 +0000' which is not a valid filename on Windows. 问题是Time.now返回的格式类似于'2016-09-28 04:45:40 +0000' ,在Windows上不是有效的文件名。 You can just reformat the date/time to something valid like 您可以将日期/时间重新格式化为类似

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

which outputs 2016-09-27_23.33.59 and then put that in your filename. 输出2016-09-27_23.33.59 ,然后将其放在您的文件名中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Ruby 中使用 selenium-webdriver/capybara 截取完整浏览器页面及其元素的屏幕截图? - How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby? Selenium Webdriver仅获取视口的屏幕截图 - Selenium Webdriver take screenshot of viewport only Ruby + Selenium-执行每个步骤后截屏 - Ruby + Selenium - Take a screenshot with each step executed Selenium或Watir Webdriver,如何保存调整大小的截图? - Selenium or Watir Webdriver, how to save resized screenshot? 谁能建议如何使用 ruby​​ selenium 截取整个网页的屏幕截图? - Can anyone suggest how to take screenshot of full webpage using ruby selenium? 以编程方式在Ruby中使用ScreenShot of Desktop? - Programmatically take ScreenShot of Desktop in Ruby? 使用Ruby自动将场景名称添加到Selenium中的屏幕快照文件名 - Automatically add Scenario name to filename of screenshot in Selenium using Ruby Ruby Capybara Poltergeist,如何为具有所需登录名的页面拍摄屏幕截图 - Ruby Capybara Poltergeist, how to take screenshot for a page with required login Ruby Selenium屏幕快照到特定的文件位置 - Ruby Selenium Screenshot to specific File location 使用selenium-webdriver / rc rails整个屏幕的ScreenShot - ScreenShot of entire webpage using selenium-webdriver/ rc rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM