click_on和click_link之间的区别? [英] Difference between click_on and click_link?

查看:272
本文介绍了click_on和click_link之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写请求规范,并且正在使用poltergeist-1.0.2和capybara-1.1.2。我有以下代码:

I am writing request specs and I am using poltergeist-1.0.2 and capybara-1.1.2. I have the following code:


    login_as @user, 'Test1234!'
    click_on 'Virtual Terminal'

该登录名带有Flash消息,向用户显示他已成功登录。当我使用click_link时,规范失败,因为Capybara找不到元素虚拟终端,但是当我使用click_on时,一切都会通过。 虚拟终端不是按钮,而是链接。

the login has flash message that shows to the user that he is successfully logged in. When I am using click_link, the spec fails because Capybara can't find element 'Virtual Terminal' but when I am using click_on everything passes. 'Virtual Terminal' is not a button, it is a link.

click_on和click_link有什么区别。

What is the difference between click_on and click_link.

推荐答案

单击链接使用一个查找程序,该查找程序指定您要查找与您提供的定位器的链接,然后按如下所示单击它:

Click link uses a finder which specifies that you are looking for a link with the locator you provided and then does click on it as such:

def click_link(locator, options={})
  find(:link, locator, options).click
end

单击而是使用查找器,该查找器应指定为链接或按钮,例如:

Click on instead uses a finder that specifies that it should be a link or a button as:

def click_link_or_button(locator, options={})
  find(:link_or_button, locator, options).click
end
alias_method :click_on, :click_link_or_button

来源:水豚动作

这又将我们引向选择器: link和:link_or_button,其定义如下:

This leads us in turn to the selectors :link and :link_or_button and that is defined as follows:

Capybara.add_selector(:link_or_button) do
  label "link or button"
  xpath { |locator| XPath::HTML.link_or_button(locator) }
  filter(:disabled, :default => false) { |node, value| node.tag_name == "a" or not(value ^ node.disabled?) }
end

Capybara.add_selector(:link) do
  xpath { |locator| XPath::HTML.link(locator) }
  filter(:href) do |node, href|
    node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href.to_s)])
  end
end

来源:Capubara选择器

Xpath定位器仅在搜索链接或链接和按钮方面有所不同,如以下源代码所示:

The Xpath locators only differ in searching for link or link and button as shown in this sourcecode:

def link_or_button(locator)
  link(locator) + button(locator)
end

def link(locator)
  link = descendant(:a)[attr(:href)]
  link[attr(:id).equals(locator) | string.n.contains(locator) |  attr(:title).contains(locator) | descendant(:img)[attr(:alt).contains(locator)]]
end

def button(locator)
  button = descendant(:input)[attr(:type).one_of('submit', 'reset', 'image', 'button')][attr(:id).equals(locator) | attr(:value).contains(locator) | attr(:title).contains(locator)]
  button += descendant(:button)[attr(:id).equals(locator) | attr(:value).contains(locator) | string.n.contains(locator) | attr(:title).contains(locator)]
  button += descendant(:input)[attr(:type).equals('image')][attr(:alt).contains(locator)]
end

来源: Xpath html

如您所见,按钮定位器实际上发现了许多不同的类型,如果我有html源代码,则可以确定链接是否存在。

As you can see the button locator actually finds a lot of different types which your link might fall under, if I had the html source code I could tell if it does or not.

这篇关于click_on和click_link之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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