Rename video record in after hook ruby

62 views Asked by At

Hi I have implemented Selenoid in my project and by default it names the video records like 'selenoid + browser session id.mp4', which I want to change to timestamp with scenario name.

Here's what I've done so far:

def rename_video_files(scenario, failed = false)
  Dir.glob("/end-to-end-test/test-reports/video-logs") do
    File.rename("#{page.driver.browser.session_id}.mp4", 
    "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_#{scenario
      .name}#{failed ? '-FAILED' : ''}.mp4"
      .gsub(/[^0-9A-Za-z.\-]/, '_'))
  end

and after hook:

After('@ui') do |scenario|
  rename_video = rename_video_files(scenario, failed = false)
  logger.info "Recorded video name: #{rename_video}.mp4"
  page.driver.quit
end

Tests pass, but the names of the video records stay unchanged.

New edit: Apparently selenoid when creates record, first names it 'selenoidxxxx.mp4' and after the test is finished renames it with session_id.mp4 by default. Therefore I'm not able to catch file name with session_id in the after step, because it's not renamed yet.

2

There are 2 answers

0
Rajagopalan On

You have to include the extension here in Dir and try again

Dir.glob("/end-to-end-test/test-reports/video-logs/*.mp4") do |file|

end
0
desislava_453 On

I found working solution:

The after step:

  After('not @noui') do |scenario|
    arr = Dir.glob("../test-reports/video-logs/selenoid*.mp4")
    string1 = arr.join(",")
    string2 = "../test-reports/video-logs/#{rename_video_files(scenario, failed = false)}"
    File.rename(string1, string2)
    logger.info "Recorded video name: #{string2}.mp4"
    page.driver.quit
end

and rename_video_files method:

 def rename_video_files(scenario, failed = false)
    "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_#{scenario
     .name}#{failed ? '-FAILED' : ''}.mp4"
     .gsub(/[^0-9A-Za-z.\-]/, '_')
 end