【问题标题】:Capybara Selenium click_link: undefined method `start_with?' for nil:NilClassCapybara Selenium click_link:未定义的方法`start_with?对于零:NilClass
【发布时间】:2014-02-05 15:31:15
【问题描述】:

我正在使用 Capybara 2.1.0 和 Selenium-Webkit 2.38.0 gem 来测试 Rails 3.2.13 应用程序。

我有以下测试:

visit my_favorite_path
click_link_or_button 'Share'
page.should have_link 'Send Email'
click_link_or_button 'Send Email'
last_email.should be_present

第二个“click_link_or_button”失败并出现以下(非常不透明)错误:

Failure/Error: click_link_or_button 'Send Email'
NoMethodError:
   undefined method `start_with?' for nil:NilClass

当我用click_link 'Send Email'first(:link, 'Send Email').click 替换有问题的行时,我得到了同样的错误。

这可能是什么问题?测试的第三行通过,看似确认链接存在,但随后尝试单击链接失败,出现这个非常无益的错误。

【问题讨论】:

    标签: ruby-on-rails selenium ruby-on-rails-3.2 capybara


    【解决方案1】:

    好吧,我傻了,我忘了在测试中包含js: true,这似乎已经解决了问题:

    it 'should send an email when I ask it to', js: true do
      visit my_favorite_path
      click_link_or_button 'Share'
      page.should have_link 'Send Email'
      click_link_or_button 'Send Email'
      last_email.should be_present
    end
    

    我真的很想删除这个问题,但似乎我不能再删除了。无论如何,也许它会对发现这个的其他人有所帮助。

    【讨论】:

    • 删除问题不是一个好主意;我用你的回答解决了我的问题。 :)
    【解决方案2】:

    试试这个:

    click_button "Send Email"
    

    或者可能是某些验证失败检查您的模型和控制器代码。使用 start_with 的地方。

    【讨论】:

    • grep -r 'start_with?' * 在我的应用程序的根目录中一无所获,它不是一个按钮,它实际上是一个链接 (<a>),所以 click_button 不起作用
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多