【发布时间】:2018-09-11 13:26:18
【问题描述】:
我正在尝试测试使用 Vue.js 的 Rails 5.1 应用程序的一部分。 我遇到的问题是,例如在生成列表时,Vue 代码如下所示:
<div v-for="(amenity, idx) in amenities">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" v-bind:id="amenity.text" v-model="checkedAmenities[idx]" :value="amenity.id">
<label class="custom-control-label" v-bind="{ 'for': amenity.text }" >{{ amenity.text }}</label>
</div>
</div>
它生成的实际 HTML 如下所示:
<div>
<div class="custom-control custom-checkbox">
<input id="Air conditioning" class="custom-control-input" value="0" type="checkbox"> <label for="Air conditioning" class="custom-control-label">Air conditioning</label>
</div>
</div>
我最终在我的规范中遇到了这个错误:
Capybara::ElementNotFound:
Unable to find visible checkbox "Some amenity" that is not disabled
这是因为设施是在 Vue 组件中定义的,但在我相信的 JS 解释器解析之前不会在 HTML 中输出。
我正在尝试使用 Chrome Headless 进行测试。我创建了以下 chrome_driver.rb 文件:
Capybara.register_driver(:headless_chrome) do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w[headless disable-gpu] }
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
end
在我的 rails_helper.rb 中:
require 'selenium/webdriver'
RSpec.configure do |config|
# ...
Capybara.javascript_driver = :headless_chrome
end
我的 RSpec 测试如下所示:
scenario 'successfully', js: true do
# do some things here..
check('Some amenity')
click_button 'Next'
click_link 'Create Listing'
expect(page).to have_content 'Listing was created successfully!'
end
但如前所述,我得到的错误是:
Capybara::ElementNotFound:
Unable to find visible checkbox "Some amenity" that is not disabled
知道如何让 Chrome 无头解析 Vue.js 代码,以便在前端填充便利设施吗?
提前致谢!!
【问题讨论】:
标签: ruby-on-rails rspec capybara google-chrome-headless