Capybaracheatsheet
贡献者:BAI
Capybara
访问
RUBYvisit articles_path
单击链接和按钮
RUBYclick_on 'Link Text' click_button click_link
Form 交互
RUBYattach_file 'Image', '/path/to/image.jpg'fill_in 'First Name', with: 'John'
RUBYcheck 'A checkbox'uncheck 'A checkbox'
RUBYchoose 'A radio button'
RUBYselect 'Option', from: 'Select box'unselect
限制
RUBYwithin '.classname' do click '...'end
RUBYwithin_fieldset :id do ...end
查询
谓词
RUBYpage.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
.
选择器
RUBYexpect(page).to have_button('Save')
RUBYexpect(page).to have_button('#submit')
RUBYexpect(page).to have_button('//[@id="submit"]')
选择器 selector
参数可以是文本、CSS 或 XPath.
RSpec 断言
RUBYpage.has_button?('Save')
RUBYexpect(page).to have_no_button('Save')
Rspec 中也可以用 page.should
断言.
反向用法
RUBYexpect(page).to have_no_button('Save') # OK
RUBYexpect(page).not_to have_button('Save') # Bad
RSpec
匹配器
RUBYexpect(page).to \
RUBYhave_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'
RUBYhave_link 'Logout', href: logout_path
RUBYhave_select 'Language', selected: 'German' options: ['Engish', 'German'] with_options: ['Engish', 'German'] # 部分匹配
RUBYhave_text 'Hello', type: :visible # or :all # 也可以用 have_content
常见的选项
所有匹配器都有下面这些选项:
RUBYtext: 'welcome' text: /Hello/ visible: true count: 4 between: 2..5 minimum: 2 maximum: 5 wait: 10
其他特性
查找
RUBYfind(selector)find_button(selector)find_by_id(id)find_field(selector)find_link(selector)locate
Scope
RUBYwithin '#delivery' do fill_in 'Street', with: 'Hello'end
RUBYwithin :xpath, '//article'within_fieldsetwithin_tablewithin_framescope_to
RUBYfind('#x').fill_in('Street', with: 'Hello')# 和 within 一样
脚本
RUBYexecute_script('$("input").trigger("change")')evaluate_script('window.ga')
执行 Javascipt
RUBYsave_and_open_page
Page
ALL('H3')page .all('h3') .body .html .source .current_host .current_path .current_url
AJAX
RUBYusing_wait_time 10 do ...end
其他
RUBYdrag field_labeled
Page 对象
RUBYpage.status_code == 200page.response_headers
详见: Session
Poltergeist
RUBYCapybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, :inspector => true)endCapybara.javascript_driver = :poltergeist
详见 poltergeist 集成 PhantomJS.
黑名单
RUBYconfig.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
调试
RUBYpage.driver.debug
RUBYpage.driver.pause
Selenium
接收 confirm() 和 alert()
..accept_alert { ... }dismiss_confirm { ... }accept_prompt(with: 'hi') { ... }
也可写作
RUBYpage.driver.browser.switch_to.alert.accept
更新会话 session
RUBYpage.set_rack_session(foo: 'bar')