Print duration of every step in terminal, during watir-cucumber execution

826 Views Asked by At

Is there a standard way of printing the duration of every step in the terminal, during a watir-cucumber execution?

enter image description here It might be possible to make some kind of invention, using the "after step" and a global variable. But, there is a proper way?

2

There are 2 best solutions below

2
On

The simplest way to do this would be using Ruby's built in Time class. Using Time.new we can record the current time into a local variable before and after the code within the step definition is executed. Then we work out the duration by subtracting start_time from end_time.

#get the start time
 start_time = Time.new

#do whatever the step needs to do here

#get the end time
 end_time = Time. new

#calculate duration 
duration = end_time - start_time
puts duration
0
On

You can use hooks like this:

Before do 
  @last_time = Time.new  
end

AfterStep do
  now = Time.new
  puts "Step duration: #{((now - @last_time)*100).round} ms"    # if (now-@last_time) > 10        
  @last_time = now
end

This is more verbose by outputting the duration on a new line, but unless you want to get more in-depth (by creating your own formatter), you cannot output stuff where you indicated it.

Uncomment the if if you are only interested in steps that take longer than a certain threshold.