问题
How do we make some_element.present? or some_element.visible? wait for less than 5 secs.? Because I think some_element.present? alone will wait for default value of 30 secs before timing out.
Thanks
回答1:
I believe you are asking how to shorten the length of time before timeout, by default its set to 30 seconds, see below on how to customize that time.
According to http://watirwebdriver.com/waiting/
Explicit waits
There are four built in methods that you can use to make your waiting experience more pleasant (and remove those evil sleep statements from your code)
Watir::Wait.until { ... }: where you can wait for a block to be true
object.when_present.set: where you can do something when it’s present
object.wait_until_present:; where you just wait until something is present
object.wait_while_present:; where you just wait until something disappears
The default timeout for all these methods is 30 seconds, but your can pass an argument to any of these to increase (or decrease) it as needed.
and http://rdoc.info/gems/watir-webdriver/Watir/EventuallyPresent
- (Object) wait_until_present(timeout = nil)
Waits until the element is present.
Examples:
browser.button(:id => 'foo').wait_until_present
Parameters:
timeout (Fixnum) (defaults to: nil) — seconds to wait before timing out
- (Object) wait_while_present(timeout = nil)
Waits while the element is present.
Examples:
browser.button(:id => 'foo').wait_while_present
Parameters:
timeout (Integer) (defaults to: nil) — seconds to wait before timing out
- (Object) when_present(timeout = nil)
Waits until the element is present.
Examples:
browser.button(:id => 'foo').when_present.click
browser.div(:id => 'bar').when_present { |div| ... }
browser.p(:id => 'baz').when_present(60).text
Parameters:
timeout (Fixnum) (defaults to: nil) — seconds to wait before timing out
回答2:
The Element#present?
(and Element#visible?
and Element#exists?
) method does not wait at all. You can see this by checking the time before and after attempting to locate an element that is not present:
puts Time.now
#=> 2014-07-31 22:14:08 -0400
puts browser.element(id: 'does_not_exist').present?
#=> false
puts Time.now
#=> 2014-07-31 22:14:08 -0400
As you can see, the time before and after checking the prescence of the element is a negligible amount.
It should be noted that the above was executed against a tiny page. For a very large page, which would require more inspection, the method could take longer to execute. However, that would be an issue of execution time rather than being Watir is actually waiting.
来源:https://stackoverflow.com/questions/25071591/making-some-element-present-in-watir-wait-for-less-than-5-secs