黄瓜、水豚和 ElementNotFound

发布于 2024-09-17 12:35:56 字数 391 浏览 4 评论 0原文

在我的 Rails 应用程序中,我有一个带有执行 javascript 函数的链接的页面:

<%= link_to_function("Add an address", "add_fields(this)".html_safe) %>

在我的黄瓜功能中,我有:

And I press "Add an address"

我得到的消息是:

Capybara::ElementNotFound: no button with value or id or text 'Add an address' found

我可能丢失了一些东西,但我找不到它是什么..

In my rails application, I have a page with a link that execute a javascript function :

<%= link_to_function("Add an address", "add_fields(this)".html_safe) %>

In my cucumber feature I have :

And I press "Add an address"

And the message I get is :

Capybara::ElementNotFound: no button with value or id or text 'Add an address' found

I'm probably missing something, but I can't find what it is..

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

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

发布评论

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

评论(5

断肠人 2024-09-24 12:35:56

您应该执行以下操作中的一项且仅执行一项:

  • 将提交按钮重命名为“创建”
  • 将测试更改为“然后我按“保存””
  • 向您的按钮添加一个 id,并更改测试,例如这个:

    查看
    = f.submit '保存', :id => :foo

    测试
    我按“foo”

You should do one, and only one, of the following:

  • Rename your submit button to 'Create'
  • Change your test to 'And I press "Save"'
  • Add to your button an id, and also change the test, like this:

    view
    = f.submit 'Save', :id => :foo

    test
    And I press "foo"

狠疯拽 2024-09-24 12:35:56

由 joaomilho 解决:

您应该执行以下操作中的一项且仅执行一项:

将提交按钮重命名为“创建”
将您的测试更改为“然后我按“保存””
向按钮添加一个 id,并更改测试,如下所示


= f.submit '保存', :id => :foo

测试
我按“foo”

1个场景(1个通过)
3步(3步通过)
0m2.510s

这里有同样的行为,我正在使用:

Rails 3 Cucumber/Capybara/Haml

Feature: Manage posts
  In order to [goal]
  [stakeholder]
  wants [behaviour]

  @wip
  Scenario: Register new post             # features/manage_posts.feature:6
    Given I am on the new post page       # features/step_definitions/web_steps.rb:19
    When I fill in "Title" with "title 1" # features/step_definitions/web_steps.rb:40
    And I fill in "Body" with "body 1"    # features/step_definitions/web_steps.rb:40
    And I uncheck "Published"             # features/step_definitions/web_steps.rb:83
    And I press "Create"                  # features/step_definitions/web_steps.rb:27     
    Then I should see "title 1"           # features/step_definitions/web_steps.rb:108
    And I should see "body 1"             # features/step_definitions/web_steps.rb:108
    And I should see "false"              # features/step_definitions/web_steps.rb:108

步骤:

When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector|   with_scope(selector) do
    click_button(button)
    selenium.wait_for_page_to_load   
    end  
end

查看新内容:

%h1 New post

= render 'form'

= link_to 'Back', posts_path

错误:

 no button with value or id or text 'Create' found (Capybara::ElementNotFound)
      ./features/step_definitions/web_steps.rb:29
      ./features/step_definitions/web_steps.rb:14:in `with_scope'
      ./features/step_definitions/web_steps.rb:28:in `/^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/'
      features/manage_posts.feature:11:in `And I press "Create"'

_form:

= form_for @post do |f| 
  -if @post.errors.any?
    #errorExplanation
      %h2= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:"
      %ul 
        - @post.errors.full_messages.each do |msg|
          %li= msg 

  .field
    = f.label :title
    = f.text_field :title
  .field
    = f.label :body
    = f.text_area :body
  .field
    = f.label :published
    = f.check_box :published
  .actions
    = f.submit 'Save'

Solved by joaomilho:

You should do one, and only one, of the following:

Rename your submit button to 'Create'
Change your test to 'And I press "Save"'
Add to your button an id, and also change the test, like this:

view
= f.submit 'Save', :id => :foo

test
And I press "foo"

1 scenario (1 passed)
3 steps (3 passed)
0m2.510s

Same behavior here, I'm using:

Rails 3 Cucumber/Capybara/Haml

Feature: Manage posts
  In order to [goal]
  [stakeholder]
  wants [behaviour]

  @wip
  Scenario: Register new post             # features/manage_posts.feature:6
    Given I am on the new post page       # features/step_definitions/web_steps.rb:19
    When I fill in "Title" with "title 1" # features/step_definitions/web_steps.rb:40
    And I fill in "Body" with "body 1"    # features/step_definitions/web_steps.rb:40
    And I uncheck "Published"             # features/step_definitions/web_steps.rb:83
    And I press "Create"                  # features/step_definitions/web_steps.rb:27     
    Then I should see "title 1"           # features/step_definitions/web_steps.rb:108
    And I should see "body 1"             # features/step_definitions/web_steps.rb:108
    And I should see "false"              # features/step_definitions/web_steps.rb:108

Step:

When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector|   with_scope(selector) do
    click_button(button)
    selenium.wait_for_page_to_load   
    end  
end

View New:

%h1 New post

= render 'form'

= link_to 'Back', posts_path

Error:

 no button with value or id or text 'Create' found (Capybara::ElementNotFound)
      ./features/step_definitions/web_steps.rb:29
      ./features/step_definitions/web_steps.rb:14:in `with_scope'
      ./features/step_definitions/web_steps.rb:28:in `/^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/'
      features/manage_posts.feature:11:in `And I press "Create"'

_form:

= form_for @post do |f| 
  -if @post.errors.any?
    #errorExplanation
      %h2= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:"
      %ul 
        - @post.errors.full_messages.each do |msg|
          %li= msg 

  .field
    = f.label :title
    = f.text_field :title
  .field
    = f.label :body
    = f.text_area :body
  .field
    = f.label :published
    = f.check_box :published
  .actions
    = f.submit 'Save'
鹿港小镇 2024-09-24 12:35:56

我相信你想要

And I follow "Add an Address"

I believe you want

And I follow "Add an Address"
甜心小果奶 2024-09-24 12:35:56

Sebastian:尝试向您的链接添加一个 ID,并在测试中引用它。

Sebastian: Try to add an id to your link, and reference it in your test.

往事随风而去 2024-09-24 12:35:56

最初的问题不是您创建链接但尝试按按钮吗?

仔细阅读水豚文档,你会发现方法是不同的。

Wasn't the original problem that you were creating a link but trying to press a button?

Read the capybara docs carefully, and you'll see the methods are different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文