如何使用水豚执行这些操作?

问题描述 投票:0回答:1

如果可能,请让我知道如何做这两件事的不同变体,我将非常感激。我已经尝试了好几天来做这些事情。

编辑 - 这是一个爱好 2017 年 Rails 项目,我将在完成一些系统测试后更新,capybara 版本 3.6

  1. 验证这句话是否存在于电子邮件中
    expect(page).to etc)
    :

元素:

<b>Ok, first thing's first, please verify (at bottom).</b>

CSS选择器:

#\:hs > div:nth-child(2) > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr:nth-child(3) > td > b

完整的xpath

/html/body/div[7]/div[3]/div/div[2]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/table/tr/td/div[2]/div[2]/div/div[3]/div[8]/div/div/div/div/div[1]/div[2]/div[3]/div[3]/div/div[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[3]/td/b
  1. 单击导航栏中的此链接(已解决)

元素:

<a class="pjax navbar-brand" href="/">
        <img src="/assets/sass_hat-a30e21456cb7f6d75a0182235b1e5f72239642262f5b0bf2c8637b716207bd8a.png" alt="sass hat">
</a>

CSS选择器:

body > header > div > div.navbar-header > a

完整的xpath

/html/body/header/div/div[1]/a

编辑:得到第二个:

click_link(href: "/")

ruby-on-rails rspec capybara
1个回答
0
投票

我已对您单独发布的电子邮件问题发表了评论。

对于链接,Capybara 记录了可使用哪些选项来查找链接 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L52 其中指出

# * **:link** - Find links (`<a>` elements with an href attribute)
#   * Locator: Matches the id, {Capybara.configure test_id}, or title attributes, or the string content of the link,
#     or the alt attribute of a contained img element. By default this selector requires a link to have an href attribute.
#   * Filters:
#       * :title (String) - Matches the title attribute
#       * :alt (String) - Matches the alt attribute of a contained img element
#       * :href (String, Regexp, nil, false) - Matches the normalized href of the link, if nil will find `<a>` elements with no href attribute, if false ignores href presence
#

因此,您可以通过其中提到的任何内容找到,最后一个可能应该是 href 条目,因为如果/当您更改路线时,它会发生变化。为链接中的图片添加有意义的标题和/或 alt,然后您可以根据这些进行查询

click_link('sass hat')
© www.soinside.com 2019 - 2024. All rights reserved.