如何使用水豚来判断 Select 框是否有某些列为选项的值?

这是我的HTML:

<select id="cars"> 
  <option></option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

这就是我想做的:

Then the "cars" field should contain the option "audi"

推荐答案

try 改用水豚rspec matcher have_select(locator, options = {}):

#Find a select box by (label) name or id and assert the given text is selected
Then /^"([^"]*)" should be selected for "([^"]*)"$/ do |selected_text, dropdown|
  expect(page).to have_select(dropdown, :selected => selected_text)
end

#Find a select box by (label) name or id and assert the expected option is present
Then /^"([^"]*)" should contain "([^"]*)"$/ do |dropdown, text|
  expect(page).to have_select(dropdown, :options => [text])
end

Ruby-on-rails相关问答推荐

未捕获语法错误:try 编辑TRIX时,请求的模块未在Rails 7.1中提供名为默认的导出(在youtube.js:1:8)

SASS,Rails 7:找不到要导入的样式表

Web控制台不会出现在例外页面Rails 7.1.1上

Ruby on Rails form_with 不显示错误,卡在服务器端

在 Ruby on Rails html.erb 文件中循环

Rails Active Record - 从关系中获取 ids 数组

如何在 YAML 中声明带有单引号和双引号的字符串?

骨干model.destroy()即使工作正常也会调用错误回调函数?

ActiveRecord 回调列表

blueprint/screen.css 未预编译

上传图片 Ruby on Rails

判断 DateTime 值是今天、明天还是以后

Rails 3:如何正确显示textarea中的文本?

Ruby 元素是否存在于数组中

Rails 服务对象与 lib 类

如何显示 RSpec 测试生成的 SQL 查询日志(log)?

导轨链接到:远程

我如何在具有活动记录的 Rails 中使用两个不同的数据库?

submit_tag rails 表单中的 bootstrap 图标

如何检测rails是否在根url?