用水豚测试焦点

发布于 2024-12-12 14:42:13 字数 640 浏览 1 评论 0原文

我的视图模板中有一个简单的 link_to_function

<%= link_to_function "add new category", "$('#category_name').focus()" %>

,我想使用请求规范使用水豚来测试它。基本上规范应该看起来像这样

it "focuses category form when I click 'add new category'" do
  visit new_article_path
  click_link "add new category"

  # unfortunately there's nothing like 'has_focus?'
  find_field("category_name").should have_focus  
end

,问题是,我找不到任何可以检查元素是否具有焦点的东西。 我发现的唯一一件事是它

page.evaluate_script('document.focus')[:id]

不受 capybara-wekbit 驱动程序的支持,我用它来避免每次测试运行时打开浏览器。

I have a simple link_to_function in my view template

<%= link_to_function "add new category", "$('#category_name').focus()" %>

and I want to test this with capybara using request specs. Basically the spec should look something like this

it "focuses category form when I click 'add new category'" do
  visit new_article_path
  click_link "add new category"

  # unfortunately there's nothing like 'has_focus?'
  find_field("category_name").should have_focus  
end

the problem is, I wasn't able to find anything, that would check if the element has focus.
The only thing I did find was this

page.evaluate_script('document.focus')[:id]

which however isn't supported by the capybara-wekbit driver, which I'm using to avoid opening browser for each test run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱暴击 2024-12-19 14:42:13

我只是使用了以下代码(使用 phantomjs 驱动程序,但我相信它也适用于 webkit):

page.evaluate_script("document.activeElement.id") == "some_id"

PS 一年问题没有答案。他们应该给我徽章吗? :)

I just used the following code (with phantomjs driver, but I believe that it works with webkit also):

page.evaluate_script("document.activeElement.id") == "some_id"

P.S. One year question without an answer. Should they give me a badge? :)

呆橘 2024-12-19 14:42:13

您应该使用 :focus 选择器,例如:

page.should have_selector('#category_name:focus')

You should use the :focus selector, e.g:

page.should have_selector('#category_name:focus')
紙鸢 2024-12-19 14:42:13

使用 Selenium 驱动程序,您可以获得焦点元素:

page.driver.browser.switch_to.active_element

然后您可以用它做您喜欢的事情

page.driver.browser.switch_to.active_element.send_keys "some text"

请注意,它返回一个 Selenium::WebDriver::Elementfind 返回一个Capybara::Node::Element 所以比较它们时要小心

expect(page.driver.browser.switch_to.active_element).to eql(find('#some-element').native)

With the Selenium driver you can get the focused element:

page.driver.browser.switch_to.active_element

Then you can do what you like with it

page.driver.browser.switch_to.active_element.send_keys "some text"

Note that it returns a Selenium::WebDriver::Element whereas find returns a Capybara::Node::Element so be careful when comparing them

expect(page.driver.browser.switch_to.active_element).to eql(find('#some-element').native)
彩扇题诗 2024-12-19 14:42:13

2024 年 5 月更新:

方法 1(推荐为最具可读性):

expect(page).to have_css('#category_name', focused: true)

水豚::选择器

方法2:

expect(page.driver.active_element.native).to eql(find('#category_name').native)

另外,Capybara::Selenium::FirefoxNode似乎有focused? 方法,所以像这样使用 Firefox 驱动程序时应该可以:

expect(find('#category_name').native.focused?).to be true

PS 这是上面代码的 Minitest 版本。我在 StackOverflow 上找不到任何关于它的问题,所以我把它留在这里,以防万一。

assert_css '#category_name', focused: true
assert_equal find('#category_name').native, page.driver.active_element.native

May 2024 update:

Method 1 (recommended as being the most readable):

expect(page).to have_css('#category_name', focused: true)

Capybara::Selector.

Method 2:

expect(page.driver.active_element.native).to eql(find('#category_name').native)

Also, Capybara::Selenium::FirefoxNode seems to have the focused? method, so something like this should be possible when using Firefox driver:

expect(find('#category_name').native.focused?).to be true

P.S. Here is the Minitest version of the code above. I couldn't find any question about it on StackOverflow, so I am leaving it here, just in case.

assert_css '#category_name', focused: true
assert_equal find('#category_name').native, page.driver.active_element.native
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文