Tingting Dong

Tingting Dong

QA,抠脚女汉子,热爱一切美好事物的2货处女座...

LinkedIn

Capybara常见用法小结

在capybara里面,如果没有任何设置,那么css是默认的,如何更改:

Capybara.default_selector = :xpath


  • 检查page是否含有元素
page.has_xpath? "//div[@id='ele']"
or
page.has_no_xpath?

返回 true 或者 false
实际上有很多
   has_selector?(*args) 
   has_no_selector?(*args)
   has_xpath?(path, options={})
   has_no_xpath?(path, options={})
   has_css?(path, options={})
   has_no_css?(path, options={})
       has_text?(content)
   has_content?(content)
   has_no_text?(content)
   has_no_content?(content)
   has_link?(locator, options={})  (id,text, img alt)
   has_no_link?(locator, options={})
   has_button?(locator)  (id, name, value)
   has_no_button?(locator)
   has_field?(locator, options={})  (id, name , label)
   has_no_field?(locator, options={})
   has_checked_field?(locator)
   has_no_checked_field?(locator)
   has_unchecked_field?(locator)
   has_no_unchecked_field?(locator)
   has_select?(locator, options={})   (id,name,label)
   has_no_select?(locator, options={})
   has_table?(locator, options={})    (id or caption element within a tableelement )
   has_no_table?(locator, options={})
  • 点击一个元素, 如果没有这个元素会报错,所以最好先检查page有没有
find("css style is default one").click
find(:xpath, "//div[@id='ele']").click

返回一个元素
  • 得到一个元素的text
<div id='ele'>abc</div>
find(:xpath, "//div[@id='ele']").text   -返回abc
  • 得到一个元素的某个属性值
find(:xpath,"//div[contains(@class,'fb_preview')]")['src']
  • 得到page里面所有元素a的数量(all返回的是一个集合,所以可以用each遍历,得到每一个元素)
<div class='oo'>pp</div>
<div class='oo'>ee</div>
<div class='oo'>rr</div>
<div class='oo'>tt</div>
all(:xpath,"//div[@class='oo']").count
返回4
  • 设置input/testarea的值
find(:xpath, "//input[@id='id']").set "value"
  • 得到input里面输入的值
find("input[name='name']").value
  • 选择select框的值
find(:xpath, "//select[@name='age_min']").find(:xpath, "option[@value='#{age_start}']").select_option
  • 按键盘的按键
find(:xpath, "//input[@id='publishDate']").native.send_keys(:return)
body.native.send_keys(:control, 'w')
  • 切换窗口
main = page.driver.browser.window_handles.first
popup = page.driver.browser.window_handles.last

page.driver.browser.switch_to.window(popup)

page.driver.browser.close
page.driver.browser.switch_to.window(main)

其中查找有多少个打开的窗口
page.driver.browser.window_handles.count
  • 刷新页面
page.driver.browser.navigate.refresh
or
Capybara.current_session.driver.browser.navigate.refresh
  • 把鼠标移动到某个元素
resourceTable = find("#resource_name_header")
page.driver.browser.action.move_to(resourceTable.native).perform
  • 清除缓存cookie
Capybara.current_session.driver.reset!
  • 窗口最大化
page.driver.browser.manage.window.maximize
  • 打开浏览器窗口
     def self.open_browser
      Capybara.register_driver :selenium do |app|
       profile = Selenium::WebDriver::Firefox::Profile.new
       profile['app.update.auto'] = false
       profile['app.update.enabled'] = false
       client = Selenium::WebDriver::Remote::Http::Default.new
       client.timeout = 120 # seconds
       Capybara::Selenium::Driver.new(app, :profile =profile, :http_client =client)
      end
     end
  • 启动headless, 也就是启动后台运行,在这里还设置了虚拟的窗口大小
ENV['DIMENSIONS'] ||= '1920x1080x24'
headless = Headless.new(:reuse =true, :dimensions =ENV['DIMENSIONS'])
headless.start
  • 访问一个页面
visit 'http://www.linkedin.com'
  • 执行一段script
page.execute_script("$('#channel_filters_scroll').css('overflow','');")
page.execute_script("$('#channel_filters_scroll').click();")
  • 拖拽
把这个filter的下拉条从0的位置拖拽到50
bar = find(:xpath, "//select[@id='filter']/下拉条")
page.driver.browser.action.drag_and_drop_by(bar.native,0,50).perform
or
bar.drag_to 'another element'
  • 忽略隐藏的element, 默认是忽略
Capybara.ignore_hidden_elements = true
  • 前置条件,比如在某个元素中找其他元素
<div id='id'>
  <label class='label'></label>
</div>
within(:xpath, "//div[@id='id']") do
  find(:xpath, "label")   //要接着within的xpath写,不能以//开头了
end
  • 默认超时时间
Capybara.default_timeout 
  • 设置默认访问的url
Capybara.app_host = ''
设置好了之后直接visit '/' 即可
也可以用 visit '/post' 等访问子页面
  • 查看某个元素是否可见
find('.section').visible?

*capybara里面的一些简易用法(但是貌似不是那么好用啊)

  • link and button:
click_link_or_button
click_on
click_link
In the case of links and buttons, Capybara looks at the following element properties when attempting to locate the element to click on:
The id attribute of the anchor, button, or input tag
The title attribute of the anchor, button, or input tag
Text within the anchor, button, or input tag
The value attribute of the input element where its type is one of'button', 'reset', 'submit', or 'image'
The alt attribute where an image is used as an anchor or input
  • input
fill_in 'Forename', :with ='Matthew'
which fill_in 'xxx' is:
The id attribute of the input element
The name attribute of the input element
A related label element
  • select
select 'Mr', :from ='title'
unselect 'Mr', :from ='title'
idand name attributes, to locate the element 
The value to select from the list must be the text of one of the child option elements. 
  • radio button
choose 'Over 16' (id, name)
  • checkbox
check 'consent' (id, name)
uncheck ....
  • within
within_fieldset – The first argument should be the id or legend attribute within a form fieldset element
within_table – The first argument should be the id or caption attribute within a table element
within_frame(frame_id) – The first argument should be the id value of aniframe element (selected drivers, for example, Selenium)
也可以这样within_frame(find(:xpath, "//iframe[contains(@id,'app_runner_fb')]")),这样就不需要id了
within_window – The first argument should be the window handle (selected drivers for example, Selenium)
  • *find
find_field: This finder searches for form fields by the related labelelement, or the name/id attribute
field_labeled: This finder is the same as find_field
find_link: This finder finds an anchor or an image link using the text,id, or img alt attribute
find_button: This finder finds a button by the id, name, or valueattribute
find_by_id: This finder finds any element by the id attribute

*一个很好的例子,超时和yield的应用

     def wait_for(wait = 8)
       timeout = Time.new + wait
       while (Time.new < timeout)
        return if (yield)
       end
       raise "Condition not met within #{wait} seconds"
     end     
     wait_for(10) { find('.section').visible? }


comments powered by