获取等待时间

Get waited time

我需要检查一个元素是否从页面中消失,下面的代码对我来说工作正常:

def checkavailabilitycheck
    outtime  = 10
    wait = Selenium::WebDriver::Wait.new(:timeout => outtime)

    begin
        wait.until { $browser.find_elements(:css, 'img[src="preloader5.gif"]').size == 7 }
        puts "Check succeeded"
    rescue Selenium::WebDriver::Error::TimeOutError
        puts "Check aborted, canceld after " + outtime.to_s + " seconds"
    end
end

现在我想添加类似

的东西
puts "Check succeeded after " + wait.tookWhatEverTime + " seconds"

知道怎么做吗?

你可以这样做:

w1 = Time.now
begin
    wait.until { $browser.find_elements(:css, 'img[src="preloader5.gif"]').size == 7 }
    puts "Check succeeded"
rescue Selenium::WebDriver::Error::TimeOutError
    puts "Check aborted, canceld after " + outtime.to_s + " seconds"
end
w2 = Time.now
puts "Check succeeded after " + (w2 - w1) + " seconds"

一种方法是您可以在进入等待之前保存当前时间。并在你等待的时候得到时间。

两者的区别在于等待的时间。

以下是逻辑的伪代码

timeBeforeEnteringThewait = Time.now
//Code for the wait
// after the wait is finished...
timeAfterThewait = Time.now

elapsedTime = timeAfterThewait - timeBeforeEnteringThewait