问题
Is there any way to control test execution speed in watir? How can I slow down the speed of test execution?
Someone suggested me to use following method browser.speed = :slow
but, there is no such method for the Watir::Browser class with the browser driver I'm currently using.
回答1:
Speed Defaults to Slow
According to the documentation, Watir::Browser#speed= already defaults to :slow
. However, as far as I can tell changing this option is only valid for Internet Explorer, and has no effect on other browsers. In fact, with Chrome or Firefox the constructor won't accept a speed argument, and provides no public interface for the option.
However, as with most things Ruby, you can always access the variables directly and tweak them. This may or may not work for your use case, but you could certainly do something like this:
browser = Watir::Browser.new :chrome
#=> #<Watir::Browser:0x..fee868224270c4e1c url="data:," title="data:,">
browser.instance_variable_set :@speed, :slow
#=> :slow
browser.instance_variable_get :@speed
#=> :slow
Other Alternatives
In practice, testing browsers often involves a lot of asynchronous JavaScript events, so you probably want to wait for events or elements rather than trying to slow down the test itself. To do that, you can use explicit or implicit waits.
Implicit Waits
You can add an implicit wait in seconds. For example, to wait up to 30 seconds for each event or element:
browser.driver.manage.timeouts.implicit_wait = 30
Explicit Waits
Watir supports both Watir::Wait#until and Watir::Wait#while. For example, to wait until a login field is visible:
Watir::Wait.until { browser.text_field(name: 'login').visible? }
Use Sleep
Under the hood, Watir is Ruby, so you can also put explicit sleeps into your tests with Kernel#sleep. The main downside to doing this is that it isn't responsive. Your code will sleep for the defined time period even if the event or element you are waiting on triggers or changes earlier. This can make your tests unnecessarily slow.
来源:https://stackoverflow.com/questions/34456906/how-to-control-test-execution-speed-in-watir-webdriver-with-the-watirbrowsers