1

I am a newbie in automation, and I need help in writing this.

 before(:each) do
      # test start time 
      # video start time :00:00:00
      setup function
    end

it "test case 1" do
   #perform action1
   #perform action2 ...
end

it "test case 2" do
   #perform action1
   #perform action2 ...
end

after(:each) do
      teardown function
      #test end time
      #video end time : 00:00:57
  end

My .rb file looks like this where i need to print the time before and after test execution

Idea:

When the batch run script starts execution video also starts recording simultaneously so the time printed by the script should match with video play time video playtime starts from 00:00:00 so when the first test case starts execution time displayed should be 00:00:00 similarly the second test case will get executed at say 00:00:57 and then the next at 00:01:46

that way i can match which test case got executed at what timeline of the video

Final Output: 
Video start time: 00:00:00
Video end time : 00:00:57

Video start time: 00:00:57
Video end time: 00:01:40

I have to write it in ruby. How can I achieve this. Should I be using a timer??

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Archana
  • 639
  • 1
  • 4
  • 10

1 Answers1

4

To track time simply store the start time and compare the current time to that whenever you need to know how many seconds have elapsed since then:

start_time = Time.now
sleep(1)
Time.now - start_time # => 1.001715

In order to do something before and after each example in your RSpec test suite, around hooks are the way to go.

around(:each) do |example|
  # This happens before the example
  example.run
  # This happens after the example
end

To format a number of seconds as "HH:MM:SS", you can use a combination of Time.at and strftime as described in Ruby/Rails - How to convert seconds to time?:

Time.at(123).utc.strftime("%H:%M:%S")
=> "00:02:03"

Combining the above, you should be able to do something like:

around(:each) do |example|
  $start_time ||= Time.now
  elapsed_seconds = Time.now - $start_time
  puts "Video start time: #{Time.at(elapsed_seconds).utc.strftime("%H:%M:%S")}"
  example.run
  elapsed_seconds = Time.now - $start_time
  puts "Video end time: #{Time.at(elapsed_seconds).utc.strftime("%H:%M:%S")}"
  puts
end

This should output the number of seconds elapsed for each example, something like:

Video start time: 00:00:00
Video end time: 00:00:01

Video start time: 00:00:01
Video end time: 00:00:03

Video start time: 00:00:03
Video end time: 00:00:03
Community
  • 1
  • 1
Jakob S
  • 19,575
  • 3
  • 40
  • 38