如何使用 Watir::Browser#speed= 方法控制 watir-webdriver 中的测试执行速度? [英] How to control test execution speed in watir-webdriver with the Watir::Browser#speed= method?

查看:28
本文介绍了如何使用 Watir::Browser#speed= 方法控制 watir-webdriver 中的测试执行速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法控制 watir 中的测试执行速度?如何降低测试执行速度?

Is there any way to control test execution speed in watir? How can I slow down the speed of test execution?

有人建议我使用以下方法 browser.speed = :slow 但是,对于 Watir::Browser 类,我目前使用的浏览器驱动程序没有这样的方法.

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.

推荐答案

Speed Defaults to Slow

根据文档,Watir::Browser#speed= 已经默认为 :slow.但是,据我所知,更改这个选项只对 Internet Explorer 有效,对其他浏览器没有影响.事实上,对于 Chrome 或 Firefox,构造函数不会接受速度参数,并且没有为该选项提供公共接口.

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.

但是,与 Ruby 的大多数事物一样,您始终可以直接访问变量并对其进行调整.这可能适用于您的用例,也可能不起作用,但您当然可以这样做:

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

其他选择

在实践中,测试浏览器通常涉及大量异步 JavaScript 事件,因此您可能希望等待事件或元素,而不是试图减慢测试本身的速度.为此,您可以使用显式或隐式等待.

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.

您可以在几秒钟内添加隐式等待.例如,为每个事件或元素等待最多 30 秒:

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

显式等待

Watir 支持 Watir::Wait#untilWatir::Wait#while.例如,要等到登录字段可见:

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? } 

使用睡眠

在底层,Watir 是 Ruby,因此您还可以使用 内核#sleep.这样做的主要缺点是它没有响应.即使您正在等待的事件或元素更早地触发或更改,您的代码也会在定义的时间段内休眠.这会使您的测试不必要地变慢.

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.

这篇关于如何使用 Watir::Browser#speed= 方法控制 watir-webdriver 中的测试执行速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆