如何用 Cucumber 测试某个单词是否是链接?

发布于 2024-09-11 18:59:13 字数 328 浏览 8 评论 0原文

我想要这个自定义步骤:

Then I should see the link 'foo'

和他的相反:

But I should not see the link 'foo'

在页面中我可以有类似的内容:

lorem foo bar

或者

lorem <a href=''>foo</a> bar

我需要测试何时“foo”是链接,何时不是。谢谢。

I would like this custom step:

Then I should see the link 'foo'

and his opposite:

But I should not see the link 'foo'

In the page I can have something like:

lorem foo bar

or alternatively

lorem <a href=''>foo</a> bar

I need to test when 'foo' is a link and when isn't. Thank you.

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

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

发布评论

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

评论(4

鱼窥荷 2024-09-18 18:59:15

已经有预定义的 Webrat 步骤可以执行此操作(或者至少非常相似)。来自 web_steps.rb

Then /^(?:|I )should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector|
  within(selector) do |content|
    regexp = Regexp.new(regexp)
    if defined?(Spec::Rails::Matchers)
      content.should contain(regexp)
    else
      assert_match(regexp, content)
    end
  end
end

Then /^(?:|I )should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector|
  within(selector) do |content|
    if defined?(Spec::Rails::Matchers)
      content.should_not contain(text)
    else
      hc = Webrat::Matchers::HasContent.new(text)
      assert !hc.matches?(content), hc.negative_failure_message
    end
  end
end

There are already pre-defined Webrat steps which do this (or are at least very similar). From web_steps.rb

Then /^(?:|I )should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector|
  within(selector) do |content|
    regexp = Regexp.new(regexp)
    if defined?(Spec::Rails::Matchers)
      content.should contain(regexp)
    else
      assert_match(regexp, content)
    end
  end
end

and

Then /^(?:|I )should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector|
  within(selector) do |content|
    if defined?(Spec::Rails::Matchers)
      content.should_not contain(text)
    else
      hc = Webrat::Matchers::HasContent.new(text)
      assert !hc.matches?(content), hc.negative_failure_message
    end
  end
end
日记撕了你也走了 2024-09-18 18:59:15

使用 xpaths 可能会更幸运,你可以使用类似“//a/text()='foo'”的东西。你只需要为 webrat 启用 xpaths,我写了一篇关于在表中使用 xpaths 做类似事情的博客文章 http://www.opsb.co.uk/?p=165,它包含为 webrat 启用 xpath 所需的代码。

You might have more luck using xpaths for this, you could use something like "//a/text()='foo'". You just need to enable xpaths for webrat, I wrote a blog post about doing something similar with xpaths in tables here http://www.opsb.co.uk/?p=165, it includes the code required to enable xpaths for webrat.

裸钻 2024-09-18 18:59:15
Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
  page.should_not have_css("a", :text => linked_text)
end

Then /^I should see the link "([^\"]*)"$/ do |linked_text|
  page.should have_css("a", :text => linked_text)
end
Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
  page.should_not have_css("a", :text => linked_text)
end

Then /^I should see the link "([^\"]*)"$/ do |linked_text|
  page.should have_css("a", :text => linked_text)
end
痴梦一场 2024-09-18 18:59:14

尝试这样的事情(我还没有尝试运行它,所以可能需要进行一些小的调整):

Then /^I should see the link "([^\"]*)"$/ do |linked_text|
  # AFAIK this raises an exception if the link is not found
  find_link(linked_text)
end

Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
  begin
    find_link(linked_text)
    raise "Oh no, #{linked_text} is a link!"
  rescue
    # cool, the text is not a link
  end
end

Try something like this (I haven't tried running it, so minor tweaks might be needed):

Then /^I should see the link "([^\"]*)"$/ do |linked_text|
  # AFAIK this raises an exception if the link is not found
  find_link(linked_text)
end

Then /^I should not see the link "([^\"]*)"$/ do |linked_text|
  begin
    find_link(linked_text)
    raise "Oh no, #{linked_text} is a link!"
  rescue
    # cool, the text is not a link
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文