代码之家  ›  专栏  ›  技术社区  ›  Brian Postow

用水豚测试简单的_形

  •  0
  • Brian Postow  · 技术社区  · 7 年前

    我有一个简单的表格:

    = simple_form_for @client_email, url: { action: "update" }, html: {class: "search_form",  method: :put } do |f|
      = f.error_notification
    
      .input-row
        = f.input :old_domain, :label => "Old domain name", required: true , input_html: {name: 'search_term'}
        = f.input :new_domain, :label => "New domain name", required: true
    

    [底部有额外的资料可供提交]

    我想用水豚/黄瓜来测试一下。

    我可以把东西放入:旧域:

    @session.fill_in('search_term', with: search_term)
    

    但是,我不知道“新域名”该用什么。。。我试过在 f.input 行,但那没用。。。

    这个 :new_domain HTML中的行是:

    <div class="input string required client_email_new_domain">
       <label class="string required control-label" for="client_email_new_domain">
          <abbr title="required">*</abbr> New domain name</label>
    
       <input class="string required" id="client_email_new_domain" 
          name="client_email[new_domain]" type="text" /></div>
    

    预计到达时间: 我认为它应该寻找name字段,所以“client_email[new_domain]”,但我得到了capybara错误:

      Unable to find field "client_email[new_domain]" (Capybara::ElementNotFound)
      ./features/pages/change_client_domains_page.rb:22:in `to_domain'
      ./features/step_definitions/change_client_domains_steps.rb:18:in `/^I set to_domain to "(.*?)"$/'
      features/admin/change_client_domains.feature:42:in `And I set to_domain to "neworg.com"'
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Thomas Walpole    7 年前

    fill_in 接受输入id、名称、占位符或相关的标签文本,因此给定的HTML提供的以下任何一个都应该填充新的域输入

    fill_in 'client_email_new_domain', with: 'whatever'
    fill_in 'client_email[new_domain]', with: 'whatever'
    fill_in 'New domain name', with: 'whatever'
    

    如果其中任何一个都不起作用,那么实际上你并没有在一个页面上看到你认为存在的可见HTML。在这种情况下使用 @session.save_and_open_screenshot 看看页面的实际外观 @session.html 当您试图填充页面时,查看页面的HTML实际上是什么(假设@session实际上是您正在使用中测试的Capybara会话 page 否则为当前会话)。

    要注意的一件事是,如果要将任何JS小部件行为附加到隐藏原始输入的输入上。如果是,则需要与小部件添加到页面的可见元素交互,而不是与隐藏的原始输入元素交互。

    最后的可能性是 @session 实际上不是你访问页面的会话。不确定为什么要在实例变量中传递当前会话,除非要使用多个会话进行测试?