ctrlcv-dev.comgithub

Capybaracheatsheet

贡献者:BAI

Capybara

访问

RUBY
visit articles_path

单击链接和按钮

RUBY
click_on 'Link Text'
click_button
click_link

Form 交互

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

限制

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

查询

谓词

RUBY
page.has_css?('.button')
expect(page).to have_css('.button')
page.should have_css('.button')
正向反向
has_content?has_no_content?
------
has_css? (selector)has_no_css?
------
has_xpath? (path)has_no_xpath?
------
has_link? (selector)has_no_link?
------
has_button? (selector)has_no_button?
------
has_field? (selector)has_no_field?
------
has_checked_field? (selector)has_unchecked_field?
------
has_table? (selector)has_no_table?
------
has_select? (selector)has_no_select?

在 Rspec 中也可以用下面这样的格式 page.should have_content.

选择器

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

选择器 selector 参数可以是文本、CSS 或 XPath.

RSpec 断言

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

Rspec 中也可以用 page.should 断言.

反向用法

RUBY
expect(page).to have_no_button('Save') # OK
RUBY
expect(page).not_to have_button('Save') # Bad

RSpec

匹配器

RUBY
expect(page).to \
RUBY
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'
RUBY
have_link 'Logout', href: logout_path
RUBY
have_select 'Language',
selected: 'German'
options: ['Engish', 'German']
with_options: ['Engish', 'German'] # 部分匹配
RUBY
have_text 'Hello',
type: :visible # or :all
# 也可以用 have_content

常见的选项

所有匹配器都有下面这些选项:

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

其他特性

查找

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

Scope

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

脚本

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

执行 Javascipt

RUBY
save_and_open_page

Page

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

AJAX

RUBY
using_wait_time 10 do
...
end

其他

RUBY
drag
field_labeled

Page 对象

RUBY
page.status_code == 200
page.response_headers

详见: Session

Poltergeist

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

详见 poltergeist 集成 PhantomJS.

黑名单

RUBY
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

调试

RUBY
page.driver.debug
RUBY
page.driver.pause

Selenium

接收 confirm() 和 alert()

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

也可写作

RUBY
page.driver.browser.switch_to.alert.accept

更新会话 session

RUBY
page.set_rack_session(foo: 'bar')

其他参考资料