Cheat Sheet速查表
跳转到主要内容

Capybara 速查表

本文根据 Devhints / Capybara 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2020-06-13。

导航

visit articles_path

点击链接和按钮

click_on 'Link Text'
click_button
click_link

与表单交互

attach_file 'Image', '/path/to/image.jpg'
fill_in 'First Name', with: 'John'
check 'A checkbox'
uncheck 'A checkbox'
choose 'A radio button'
select 'Option', from: 'Select box'
unselect

限制

within '.classname' do
  click '...'
end
within_fieldset :id do
  ...
end

查询

谓词

page.has_css?('.button')
expect(page).to have_css('.button')
page.should have_css('.button')
肯定(正向)否定(反向)
has_content?has_no_content?
has_css? (选择器)has_no_css?
has_xpath? (路径)has_no_xpath?
has_link? (选择器)has_no_link?
has_button? (选择器)has_no_button?
has_field? (选择器)has_no_field?
has_checked_field? (选择器)has_unchecked_field?
has_table? (选择器)has_no_table?
has_select? (选择器)has_no_select?

在 RSpec 中,这些也对应于诸如……之类的匹配器 page.should have_content.

选择器

expect(page).to have_button('Save')
expect(page).to have_button('#submit')
expect(page).to have_button('//[@id="submit"]')

该 selector 参数可以是文本、CSS 选择器或 XPath 表达式。

RSpec 断言

page.has_button?('Save')
expect(page).to have_no_button('Save')

在 RSpec 中,你可以使用 page.should 断言。

关于否定(反向断言)

expect(page).to have_no_button('Save')
expect(page).not_to have_button('Save')

上述两条语句在功能上是等价的。

RSpec

匹配器

expect(page).to \
  have_current_path(expected_path)
  have_selector '.blank-state'
  have_selector 'h1#hola', text: 'Welcome'
  have_button 'Save'
  have_checked_field '#field'
  have_unchecked_field
  have_css '.class'
  have_field '#field'
  have_table '#table'
  have_xpath '//div'
  have_link 'Logout', href: logout_path
  have_select 'Language',
    selected: 'German'
    options: ['Engish', 'German']
    with_options: ['Engish', 'German'] # partial match
  have_text 'Hello',
    type: :visible # or :all
    # alias: have_content

通用选项

所有匹配器均具备以下选项:

  text: 'welcome'
  text: /Hello/
  visible: true
  count: 4
  between: 2..5
  minimum: 2
  maximum: 5
  wait: 10

其他功能

查找

find(selector)
find_button(selector)
find_by_id(id)
find_field(selector)
find_link(selector)
locate

作用域

within '#delivery' do
  fill_in 'Street', with: 'Hello'
end
within :xpath, '//article'
within_fieldset
within_table
within_frame
scope_to
find('#x').fill_in('Street', with: 'Hello')
# same as within

脚本编写

execute_script('$("input").trigger("change")')
evaluate_script('window.ga')

执行 JavaScript。

调试

save_and_open_page

在浏览器中打开网页。

页面

page
  .all('h3')
  .body
  .html
  .source
  .current_host
  .current_path
  .current_url

AJAX

using_wait_time 10 do
  ...
end

杂项

drag
field_labeled

页面对象

page.status_code == 200
page.response_headers

参见: https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Session

Poltergeist

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.javascript_driver = :poltergeist

使用 poltergeist 用于集成 PhantomJS。

黑名单

config.before :each, :js do
  page.driver.browser.url_blacklist = [
    'fonts.googleapis.com',
    'use.typekit.net',
    'f.vimeocdn.com',
    'player.vimeo.com',
    'www.googletagmanager.com'
  ].flat_map { |domain| [ "http://#{domain}", "https://#{domain}" ] }
end

调试

启用 inspector: true 然后:

page.driver.debug

若要暂停执行一段时间:

page.driver.pause

Selenium

接受 confirm() 和 alert()

accept_alert { ... }
dismiss_confirm { ... }
accept_prompt(with: 'hi') { ... }

或者:

page.driver.browser.switch_to.alert.accept

更新会话

page.set_rack_session(foo: 'bar')

另请参阅