Skip to content

Instantly share code, notes, and snippets.

@pobing
Last active September 28, 2015 02:18
Show Gist options
  • Save pobing/1369125 to your computer and use it in GitHub Desktop.
Save pobing/1369125 to your computer and use it in GitHub Desktop.
Rspec+capybara get page Element
模拟浏览器,人工测试
1. 文本框:
直接填充 : fill_in("#{@user.id}_name",:with=>"jdo")
通过节点赋值 : find(:xpath,"//html/body/div[2]/div[2]/div/div/div[3]").set("123456")
获取文本框值: find(:xpath,"//html/body/div[2]/div[2]/div/div/div[3]").value.should=="expect value"
2. 点击操作超链接 link
点击元素:
通过Id 找到名为tab_user 的元素点击 find(:id,"tab_user").click
直接点击 'edit' 的超链接 find_link("edit").click
3. 点击按钮 button
find_button('Submit').click
click_button('submit')
4. test confirm dialog 确认对话框
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
5.confirm/alert
-- 让返回结果为 true
page.evaluate_script('window.confirm = function() { return true; }')
-- 判断确认对话框返回值
When /^I (accept|dismiss) the "([^"]*)" alert$/ do |action, text|
alert = page.driver.browser.switch_to.alert
alert.text.should eq(text)
alert.send(action)
end
--完整示例
it "更新用户密码--成功" do
visit "/orgunits"
find(:id,"tab_user").click
find_link("chgpwd").click
page.should have_content("为用户“#{@user.login}”设置新的密码")
find(:xpath,"//html/body/div[2]/div[2]/div/div/div[2]/input").set("123456")
find(:xpath,"//html/body/div[2]/div[2]/div/div/div[3]/input").set("123456")
find(:xpath, '//html/body/div[2]/div[2]/div[2]/div/a').click
sleep Capybara.default_wait_time 异步休眠, 等待返回结果
alert =page.driver.browser.switch_to.alert
assert alert.text.should == "更新密码成功"
end
6. 断言 assert
判断当前路径:page.current_path.should==url
判断网页内容:page.should have_content("表单显示名")
当前url 不是此路径:page.should_not have_xpath('link')
判断网页无此内容:page.should_not have_content("Show")
7. 下拉选择框 select & radio
page.select 'entos', :from => 'flex_table_orgunit_id'
page.choose("flex_table_for_workflow_1")
page.find(:xpath,"//html/body/div[2]/div[2]/div/div/div[2]/select").select("部门")
page.find_by_id(field).select(value)
8. DIV 操作
获得DIV Text
page.find(:xpath,"//div").text
判断 Div Text
page.find(:xpath,"//html/body/div/div").text.should_not=="#{@orgunit.name}"
9.上传文件输入
Dir["/home/jidong/Downloads/财务部/2.xsn"].each { |f|
page.attach_file 'file' , f
page.click_button "Upload"
page.should have_content("表单名")
page.click_button "Submit"
page.should have_content("视图")
}
10. cannot fill in, no text field, text area or password field with id, name, or label
11. page.should have_no_xpath("//html/body/div/div[4]/div/table/tbody/tr/td[2]/div/div/div/div[2]/div[4]/div/div/div[2]/div[2]/div/div")
page.find(:xpath,"//html/body/div/div[4]/div/table/tbody/tr/td[2]/div/div/div/div[2]/div/div/div/div/div[2]/div/div").text.should_not=="#{@orgunit.name}"
12 .参考: http://hlee.iteye.com/blog/768459
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment