【问题标题】:Rspec: Selecting a radio button for Sinatra form?Rspec:为 Sinatra 表单选择一个单选按钮?
【发布时间】:2017-02-28 00:20:48
【问题描述】:

我正在为 Sinatra 应用编写测试规范。如何让 rspec 点击表单中的单选按钮?

<form action="/reports/new" method="POST">
<div class="form-group">
    <label class="col-md-4 control-label" for="radios">Borough</label>
      <div>
          <label>
            <input name="Brooklyn" id="borough_1" value="1" type="radio">Brooklyn</input>
          </label>
          <label>
            <input name="Bronx" id="borough_2" value="2" type="radio">Bronx</input>
          </label>
          <label>
            <input name="Manhattan" id="borough_3" value="3" type="radio">Manhattan</input>
          </label>
          <label>
            <input name="Queens" id="borough_4" value="4" type="radio">Queens</input>
          </label>
          <label>
            <input name="Staten Island" id="borough_5" value="5" type="radio">Staten Island</input>
          </label>
        <button value="Submit" type="submit">Submit</button>
      </div>
  </div>
</form>

编辑:我确实检查了这个资源,但它没有帮助我:stackoverflow.com/questions/11483967/… 我试过choose('Manhattan')。但是,我不断收到此错误:Failure/Error: choose('Manhattan') Capybara::ElementNotFound: Unable to find radio button "Manhattan"

好消息是 rspec 至少在寻找单选按钮。

        visit '/reports/new'
        fill_in(:title, :with => "Ben and Jerries Ice Cream")
        fill_in(:business, :with => "Starbucks")
        fill_in(:location, :with => "146 Rikers Street")
        fill_in(:content, :with => "Some great food")
        fill_in(:date, :with => "2016-09-12")
        choose('Manhattan')

【问题讨论】:

  • 到目前为止你尝试过什么?有severalanswersavailable用google快速搜索;你有没有研究和试验过这些?
  • @Joel B,请在您的问题中添加您上一条评论的详细信息,这将对我们将来有所帮助。
  • 您确定 Capybara 正在查看正确的网址吗?你是在choose方法之前使用Capybara的visit方法,还是类似的?
  • 看起来你做对了。当您在浏览器中转到“报告/新建”时,打开一个 JavaScript 控制台并执行 $('#borough_3');你应该找到它。值得仔细检查吗?
  • choose 会按名称或 id 查找,没关系。

标签: ruby-on-rails ruby rspec tdd sinatra


【解决方案1】:

chooseidnamelabel 查找内容。来自文档:

#选择([定位器],选项)⇒对象

找到一个单选按钮并将其标记为选中。单选按钮可以是 通过名称、ID 或标签文本找到。

确保:a) Capybara 将“报告/新建”并且 b) 您的页面正在按预期呈现这些元素。例如,尝试自己去“报告/新建”并在控制台中选择该元素(假设您有 jQuery):

$('#borough_3');

或者直接在源码中查找。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2012-07-30
    • 2016-01-09
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多