确保 Capybara 中不存在某个元素

发布于 2024-11-05 09:09:12 字数 591 浏览 1 评论 0原文

使用 Capybara,我需要断言表单元素不存在,例如,“那么我不应该看到“用户名”文本字段”。由于如果找不到元素, find 会抛出异常,因此这是我想出的最好的方法。有更好的办法吗?

Then /^I should not see the "([^\"]+)" ([^\s]+) field$/ do |name, type|
  begin
    # Capybara throws an exception if the element is not found
    find(:xpath, "//input[@type='#{type}' and @name='#{name}']")
    # We get here if we find it, so we want this step to fail
    false
  rescue Capybara::ElementNotFound
    # Return true if there was an element not found exception
    true
  end 
end

我是水豚新手,所以我可能会遗漏一些明显的东西。

Using Capybara, I need to assert that a form element is not present, for example, 'Then I should not see the "Username" text field'. As find throws an exception if the element isn't found, this is the best I've come up with. Is there a better way?

Then /^I should not see the "([^\"]+)" ([^\s]+) field$/ do |name, type|
  begin
    # Capybara throws an exception if the element is not found
    find(:xpath, "//input[@type='#{type}' and @name='#{name}']")
    # We get here if we find it, so we want this step to fail
    false
  rescue Capybara::ElementNotFound
    # Return true if there was an element not found exception
    true
  end 
end

I'm new to Capybara, so I may be missing something obvious.

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

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

发布评论

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

评论(7

蓝戈者 2024-11-12 09:09:12

您可以通过使用水豚 has_no_selector 来做到这一点?方法与 rspecs 魔法匹配器相结合。然后您可以通过以下方式使用它:

 page.should have_no_selector(:xpath, "//input[@type='#{type}' and @name='#{name}']")

您可以在 capybara 文档页面 此处<查看可以执行的断言的更多详细信息/a> 在标题为“查询”的部分下

You can do this by making use of capybaras has_no_selector? method combined with rspecs magic matchers. You can then use it in this way:

 page.should have_no_selector(:xpath, "//input[@type='#{type}' and @name='#{name}']")

You can see more details of the assertions you can perform on the capybara documentation page here under the section entitled Querying

合约呢 2024-11-12 09:09:12

您实际上可以使用 Capybara 匹配器定义的现有方法。

assert has_no_field?('Username')

此外,还有其他可用的方法可以帮助您在页面中查找不同类型的元素

has_link? , has_no_link?
has_button?, has_no_button?
has_field?, has_no_field?
has_checked_field?, has_no_checked_field?
has_select?, has_no_select?

等等。 。 。

You can actually use already existing methods defined by Capybara matchers.

assert has_no_field?('Username')

Furthermore there are additional methods available the can help you in finding different types of elements in your page

has_link? , has_no_link?
has_button?, has_no_button?
has_field?, has_no_field?
has_checked_field?, has_no_checked_field?
has_select?, has_no_select?

And many more . . .

枯寂 2024-11-12 09:09:12

这是我正在使用的:

expect(page).to have_no_css('.test')

Here's what I am using:

expect(page).to have_no_css('.test')
醉城メ夜风 2024-11-12 09:09:12

我尝试了 Derek 建议的解决方案,但是,我遇到了一些误报。

这也是

page.should have_no_selector(:xpath, "//input[@type='#{type}' and @name='#{name}']")

即使由于某种原因应该失败,

通过的。我发现 raise_error 的 RSpec 语法取得了成功,

expect { find(:xpath, "//input[@type='#{type}' and @name='#{name}']") }.to raise_error

我认为这更接近您所要求的。所以我提出这个作为答案。

I tried the solution suggested by Derek, however, I ran into some false negatives.

That is

page.should have_no_selector(:xpath, "//input[@type='#{type}' and @name='#{name}']")

passes even when it should fail for some reason.

I have found success with the RSpec syntax of raise_error

expect { find(:xpath, "//input[@type='#{type}' and @name='#{name}']") }.to raise_error

I think this is closer to what you're asking anyway. So I put this forward as an answer.

撩心不撩汉 2024-11-12 09:09:12

执行此操作的最佳方法是找到该元素并断言它不存在。这不仅创建了可重用的代码,而且如果找不到它,整个测试也不会失败。

这就是我所做的...

定义您的对象

def username
    page.find('.username')
end

然后与规范文件中的对象进行交互

username.should be_true

您真的不应该关心该元素是否在页面上(大多数时候)。当你编写测试时,你关心的是你是否可以与该元素交互...这就是为什么我首先定义对象,然后使用“用户名”与其交互...看看有多少可重用的代码你可以生成?现在您可以单击、悬停、断言元素存在、向元素填充文本等。

此外,您绝对应该尽可能考虑使用 css 而不是 xpath。 xpath 更容易破解并且更难阅读。

The best way to do this is to find the element and the assert that it is not present. Not only does this create reusable code, but the whole test won't blow up if it can't find it.

This is what I do...

define your object

def username
    page.find('.username')
end

Then interact with the object in your spec file

username.should be_true

You really shouldn't care whether or not the element is on the page (most of the time). What you care about when you're writing the tests is whether or not you can interact with that element...that's why I define the object first, and then use 'username' to interact with it...see how much reusable code you can generate? Now you can click, hover, assert the element is present, fill in text to the element, etc.

Also, you should definitely consider using css over xpath whenever possible. xpath is easier to break and harder to read.

最近可好 2024-11-12 09:09:12

我也有同样的问题。但由于我将 Capybara 与 Minitest 一起使用,接受的解决方案对我不起作用。我想如果有人和我有同样的情况会发现这个问题,我应该添加它。

也可以使用:

assert page.has_no_xpath?('//input[@type='#{type}' and @name='#{name}'"]')

I had the same problem. But since I am using Capybara with Minitest the accepted solution did not work for me. I thought I should add it, if someone in the same situation as me would find this question.

It is also possible to use:

assert page.has_no_xpath?('//input[@type='#{type}' and @name='#{name}'"]')

ㄟ。诗瑗 2024-11-12 09:09:12

在规范文件中

expect(@app.some_page_name.is_visible?('search_button')) == true

在页面文件中

element :search_button, :xpath, "//div[@class='dummy']"

在基本页面文件中

def is_visible?(value)
  begin
    eval(value).visible?
    return true
  rescue => e
    p e.message
    return false
  end
end

In spec file

expect(@app.some_page_name.is_visible?('search_button')) == true

In page file

element :search_button, :xpath, "//div[@class='dummy']"

In base_page file

def is_visible?(value)
  begin
    eval(value).visible?
    return true
  rescue => e
    p e.message
    return false
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文