了解 Capybara 中的 native 和 send_keys [英] Understanding native and send_keys in Capybara

查看:36
本文介绍了了解 Capybara 中的 native 和 send_keys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解以下 Capybara 语法的含义.native 究竟是做什么的?send_keys 是做什么用的?另外,我想了解这个特定块的作用.

I am trying to understand the meaning of the following Capybara syntax. What exactly does native do? What is send_keys used for? Also, I would like to understand what this particular block does.

within('#step-3') do
 recipe_name = first(:xpath, '//*[@id="recipe-name"]').native
 recipe_name.clear
 recipe_name.send_keys('Email recipe')
end

推荐答案

Capybara 使用驱动程序来控制浏览器或浏览器模拟器(Rack::Test、Poltergeist、Selenium 等).每个驱动程序都必须实现 Capybara 定义的 API.该 API 包括 Element 类及其 .native 方法..native 返回驱动程序内部用来表示 DOM 元素的对象.Capybara 本身对该对象没有任何用处,但该对象的一些驱动程序实现具有可用于测试的特定于驱动程序的方法.

Capybara uses a driver to control a browser or browser simulator (Rack::Test, Poltergeist, Selenium, etc.). Each driver must implement the API that Capybara defines. That API includes the Element class and its .native method. .native returns the object that the driver uses internally to represent a DOM element. Capybara itself doesn't have any use for that object, but some drivers' implementations of that object have driver-specific methods that can be useful in tests.

.clear.send_keys 是 CSS 选择器为 #recipe-name 的 DOM 元素上的驱动程序特定方法.大概它是用户键入的元素.我们大概可以猜到 .clear 做了什么..send_keys 告诉元素用户已按顺序按下给定字符串中的每个键.

.clear and .send_keys are, then, driver-specific methods on the DOM element whose CSS selector is #recipe-name. Presumably it is an element that the user types into. We can probably guess what .clear does. .send_keys tells the element that the user has pressed each of the keys in the given string in order.

使用 .send_keys 而不是仅仅使用 fill_in '#recipe-name' with: 'Email recipe' 的意义在于某些浏览器行为,例如 Javascript 事件,仅在用户按下某个键时发生.显然 fill_in 将文本放入元素中的方式不会使浏览器认为已按下任何键.所以如果你在测试一些关心按键事件的东西,你需要使用 .send_keys.

The point of using .send_keys rather than just doing fill_in '#recipe-name' with: 'Email recipe' is that some browser behavior, such as Javascript events, only happens when the user presses a key. Apparently fill_in puts text into the element in a way that doesn't make the browser think that any keys have been pressed. So if you're testing something that cares about keypress events, you need to use .send_keys.

我在 我对有关测试 jQuery 自动完成字段的问题的回答中给出了一个使用 .send_keys 的示例.

I gave an example of using .send_keys in my answer to a question about testing a jQuery autocomplete field.

这篇关于了解 Capybara 中的 native 和 send_keys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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