1

In our Ruby application web testing application using watir-webdriver, we have added code to allow up to 30 seconds for an element that is present on a page but not yet visible to become visible (due to AJAX, javascript, performance, etc.). The routine that is being tested is:

start_time = Time.now
while !visible? do
  @logger.info "!visible is true, looping"
  sleep 1
  if Time.now - start_time > 30
    @logger.debug("error while checking for visible")
    raise ElementHiddenError, element_hidden_message
  end
end

The RSpec that is used to test this is:

...
it "raises ElementHiddenError if element is hidden" do
  element.should_receive(:visible?).and_return(false)
  expect { proxy.set('something') }.to raise_error(ElementHiddenError)
end
....

When I run this RSpec, I receive this error:

1)
'ElementProxy#set raises ElementHiddenError if element is hidden' FAILED
expected ElementHiddenError but nothing was raised
./spec/element_proxy_spec.rb:144:in `(root)'
org/jruby/RubyKernel.java:2043:in `instance_eval'
./spec/spec_helper.rb:68:in `timeout'
org/jruby/RubyArray.java:1614:in `each'
org/jruby/RubyArray.java:1614:in `each'
org/jruby/RubyKernel.java:1052:in `load'

I can get this RSpec to pass by commenting out the loop:

#start_time = Time.now
while !visible? do
  @logger.info "!visible is true, looping"
  sleep 1
  #if Time.now - start_time > 30
    @logger.debug("error while checking for visible")
    raise ElementHiddenError, element_hidden_message
  #end
end

I am not the original author of this code, but it appears that something is happening with Time or sleep in the RSpec which is affecting this test.

I would appreciate any ideas about how to debug this issue, as this routine is used in almost all of our command sets. The delay works in our application but does not pass the RSpec.

Thanks.

4

0 回答 0