基于Ruby的watir-webdriver自动化测试方案与实施

 🔥 交流讨论:欢迎加入我们一起学习!

🔥 资源分享耗时200+小时精选的「软件测试」资料包

🔥 教程推荐:火遍全网的《软件测试》教程  

📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

编写脚本首先要学会捕获元素,接下来就要学习页面元素的捕获。

页面元素

attribute_value

获取当前控件的属性

Value = ie.link(:id=>'xxx’).attribute_value("href")

rand_select

随机选择select list中的某一项

ie.select_list(:name=>’’).rand_select

popupwin

点击弹窗上的‘确定’按钮

ie.popupwin.button(:name=>"确定").click

sikuli_image

点击图片控件

ie.sikuli_image(:image=>"1.png").clickie.sikuli_image(:image=>"1.png;2.png").click#可以指定多张图片来识别

double_click

双击事件

ie .sikuli_image(:image=>"1.png").double_click

right_click

右击事件

exist?

判断用户元素是否存在

复制代码

edit = ie.text_field(:name,"username")                                            if edit.exist?() #The highlightededit.flash                         ie.text_field(:name, "password").set(pwd)            ie.button(:class, "x-login-submit").click endend

复制代码

Text Fields

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo't = b.text_field :id => 'entry_0't.exists?t.set 'your name't.value

复制代码

Select Lists – Combos

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo's = b.select_list :id => 'entry_1's.select 'Ruby's.selected_options# 返回选中的选项  
puts b.select(:name, 'sex').selected_options  
 

复制代码

Radios

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'r = b.label(:text => 'What is ruby?').parent.radio :value => 'A gem'r.exists?r.setr.set?

复制代码

Checkboxes

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'c = b.label(:text => 'What versions of ruby?').parent.checkbox :value => '1.9.2'c.exists?c.setc.set?# 选择checkbox  
b.checkbox(:name, 'check_me').set true 
# 清除选择  
b.checkbox(:name, 'check_me').set false  
 
 

复制代码

Buttons

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'btn = b.button :value, 'Submit'btn.exists?btn.click

复制代码

Links

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'l = b.link :text => 'Google Docs'l.exists?l.click

复制代码

Divs & Spans

复制代码

require 'watir-webdriver'b = Watir::Browser.start 'bit.ly/watir-webdriver-demo'd = b.div :class => 'ss-form-desc ss-no-ignore-whitespace'd.exists?d.texts = b.span :class => 'ss-powered-by's.exists?s.text

复制代码

实例

 按钮

?  ie.button(:name=>"",:id=>"",:index=>n,:type=>"").click

?  ie.button(:name=>"",:id=>"",:index=>n,:type=>"").doclick

 输入框

?  ie.text_field(:name=>"").set "变量"

?  ie.text_field(:name=>"").value 取text_field值不是用text而是value!

 下拉框

?  ie.select_list(:name=>"").select "下拉框值"

?  ie.select_list(:name=>"").select "#1" #表示第一项内容

?  ie.select_list(:name=>"").rand_select

?  ie.select_list(:name=>"").getSelectedItems|getAllContents->返回Array

 单选框

?  ie.radio(:id=>"",:name=>"",:index=>n).set(选中当前radio)

?  ie.radio(:id=>"",:name=>"",:index=>n).clear(取消选中当前radio)

         ie.div(:class=>"iradio_minimal-blue checked").radios[1]

 复选框

?  ie.check_box(:id=>"",:name=>"",:index=>n).set(true|false)(true表示选中,false表示不选中)

?  ie.check_box(:id=>"",:name=>"",:index=>n).clear(取消选中当前checkbox)

 链接

?  ie.link(:text=>"").click/doclick

?  ie.link(:text=>"").href(返回当前link指向的链接)

 cell (TD标签,用时一般需要先找到上层控件如table、div等)

?  ie.table(:class=>"",:index=>n).cell(:class=>"",:index=>n).text

?  ie.table(:index=>n).rows 行  列 .text (行、列从1开始)

?  ie.div(:class=>"",:index=>n).cell(:class=>"",:index=>n).text

 span

?  ie.table(:id=>"").span(:class=>"").text

 弹出框

?  ie.popupwin.get_static_text (返回当前提示框的文本)

?  ie.popupwin.button(:name=>"确定").click/doclick (前一个点击按钮必须用doclick)

?  ie.file_dialog(:index=>1/2).set_file(file_path_download,true) (保存文件的弹出窗口)

alert

复制代码

if b.alert.exists?  puts "ok"
elseputs "no"
endputs b.alert.text #b.alert.ok 
b.alert.close  b.close

复制代码

 图片

?  ie.image(:src=>/word3a_nor.gif/).click/doclick

back

后退

ie.back

forward

前进

ie.forward

   

refresh

刷新页面

ie.refresh

在Watir-WebDriver中处理frame是非常简单的,就跟处理其他页面元素一样:

b.frame(:id => "content_ifr").send_keys "hello world"

文件的下载

最简单最好的处理文件下载对话框的方式就是完全的避免对话框弹出。

可以在代码里告诉浏览器自动的将文件下载到指定目录,然后在测试用例中访问该目录进行验证。

复制代码

Firefoxdownload_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?profile = Selenium::WebDriver::Firefox::Profile.newprofile['browser.download.folderList'] = 2 # custom locationprofile['browser.download.dir'] = download_directoryprofile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"b = Watir::Browser.new :firefox, :profile => profile

复制代码

关于Firefox的所有配置项可以通过在地址栏中输入'about:config'进行查看。

If you want to know a way to work out the file types (eg. application/pdf) then you can read the following blog post for an step by step guide. 如果你想知道如何处理特定类型的文件,请阅读这篇博文。

复制代码

Chromedownload_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if  Selenium::WebDriver::Platform.windows?profile = Selenium::WebDriver::Chrome::Profile.newprofile['download.prompt_for_download'] = falseprofile['download.default_directory'] = download_directoryb = Watir::Browser.new :chrome, :profile => profile

复制代码

浏览器新窗口

当一个新的浏览器窗口打开时,你可以使用'use'方法来处理这个新窗口。

browser.window(:title => "annoying popup").use dobrowser.button(:id => "close").clickend

JS弹出框

在web应用中,JavaScript对话框是十分常见的。

Watir-WebDriver内建了处理这些对话框的方法,并且可以返回对话框中显示的内容。首先,加载这个扩展:

View Code

可选方法

如果你使用上面的方法时遇到了麻烦,你可以自行覆盖JavaScript functions,这样一来原来应该显示的对话框就可以在触发时不显示了。

# 使alert方法返回空

browser.execute_script("window.alert = function() {}")

# 使prompt返回特定的字符串,用来模拟用户的输入

browser.execute_script("window.prompt = function() {return 'my name'}")

# 使prompt方法返回null用来模拟用户点击了Cancel(取消)按钮

browser.execute_script("window.prompt = function() {return null}")

# 使confirm方法返回true用来模拟用户点击了OK(确定)按钮

browser.execute_script("window.confirm = function() {return true}")

# 使confirm方法返回false用来模拟用户点击了Cancel(取消)按钮

browser.execute_script("window.confirm = function() {return false}")

页面性能

Watir-WebDriver-Performance gem 提供在访问页面的同时进行页面性能度量的功能,其使用的是W3C页面性能度量指标。这是一个完美的捕获响应性能指标的解决方案,其使用方法非常直观和简单,不过目前只支持Chrome和IE9l浏览器。

复制代码

require 'watir-webdriver'
require 'watir-webdriver-performance'b = Watir::Browser.new :chrome10.times dob.goto 'http://17test.info'load_secs = b.performance.summary[:response_time]/1000puts "Load Time: #{load_secs} seconds."end

复制代码

其统计结果如下:

Load Time: 3.701 seconds.

截屏

Watir-WebDriver内建的截图功能很赞也很好用。

browser.driver.save_screenshot 'screenshot.png'

The great thing about this is it gives you a screen shot of the entire page, not just above the fold. 截图功能最棒的地方在于它能捕获到整个页面,而不是屏幕上显示的那部分。

如果你正在使用Cucumber,那么你可以简单的将下面的代码添加到env.rb文件中,这样你可以在html的报告中插入截图:

复制代码

After do |scenario|browser.driver.save_screenshot 'screenshot.png'embed 'screenshot.png', 'image/png'end

复制代码

模拟特殊按键

使用.send_keys方法可以模拟特殊的键盘按键(比如shift),其参数是你所需要模拟的按键的符号表示(symbolic)。

b.send_keys :enter

也可以这样做:

b.element.send_keys [:control, 'a'], :backspace

你还可以修改click方法的行为,使得点击可以配合按键一起进行:

b.element.click(:shift, :control)

支持的按键键名列表如下:

View Code

富文本编辑器

有两种方法可以通过Watir-WebDriver向所见即所得编辑器(应该指的是富文本编辑器)中输入文字:

定位编辑器所在的iFrame,然后使用.send_keys方法(缺点是浏览器必须在前台运行)

在浏览器上执行javascript,通过js脚本去设置编辑器的值

CKEditor

复制代码

require 'watir-webdriver'b = Watir::Browser.new :firefoxb.goto 'http://ckeditor.com/demo'b.execute_script("CKEDITOR.instances['editor1'].setData('hello world');")b.frame(:title => 'Rich text editor, editor1, press ALT 0 for help.').send_keys 'hello world again'TinyMCE Editorrequire 'watir-webdriver'b = Watir::Browser.newb.goto 'http://tinymce.moxiecode.com/tryit/full.php'b.execute_script("tinyMCE.get('content').execCommand('mceSetContent',false, 'hello world' );")b.frame(:id => "content_ifr").send_keys 'hello world again'

复制代码

QA

最后我邀请你进入我们的【软件测试学习交流群:785128166】, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路

作为一个软件测试的过来人,我想尽自己最大的努力,帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源,现在免费分享给大家,有需要的小伙伴可以关注【公众号:程序员二黑】自提!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/502098.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Apache Flink连载(三十五):Flink基于Kubernetes部署(5)-Kubernetes 集群搭建-1

🏡 个人主页:IT贫道-CSDN博客 🚩 私聊博主:私聊博主加WX好友,获取更多资料哦~ 🔔 博主个人B栈地址:豹哥教你学编程的个人空间-豹哥教你学编程个人主页-哔哩哔哩视频 目录 ​编辑

SpringBoot 手写 Starter

spring-boot-starter 模块 1.介绍 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配…

配置之道:深入研究Netty中的Option选项

欢迎来到我的博客,代码的世界里,每一行都是一个故事 配置之道:深入研究Netty中的Option选项 前言Option的基础概念ChannelOption与Bootstrap Option常见的ChannelOption类型ChannelConfig的使用Option的生命周期不同传输协议的Option 前言 在…

kotlin开发框架,50家大厂面试万字精华总结

与其它行业一样,凡是有高级和普通,虽然都是敲代码但也有大牛和普通之分,大牛程序员,一个人比一个团队做项目都做得快,**最为出名的当属十几年前求伯君在做wps时,一个人完成了微软二十人团队没有完成的项目需…

并查集基础,死去的回忆突然攻击我

并查集普及【模板】并查集 - 洛谷 #include<iostream> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; #define int long long typedef pair<int,int> PII; #define xx first #define y…

种花

分情况&#xff1a; 第一盆k种选择&#xff0c;之后全部k-1种选择 每次相乘结果对1e97取模 #include <iostream> #include <vector> #include <algorithm> using namespace std; #define endl \n const int N 1e9 7;int main() {ios::sync_with_stdio(f…

简评ORACLE/MYSQL/PG的逻辑复制

postgresql逻辑复制 ​​​​ &#xff08;https://www.pgconf.asia/JA/2017/wp-content/uploads/sites/2/2017/12/D2-A7-EN.pdf&#xff09; PostgreSQL把所有逻辑解析相关的事情全部放在数据库中的复制槽进行管理&#xff0c;大包大揽。早期版本的逻辑复制支持的还不太好&a…

ICML23 - Synthetic Data for Model Selection

前言 如果你对这篇文章感兴趣&#xff0c;可以点击「【访客必读 - 指引页】一文囊括主页内所有高质量博客」&#xff0c;查看完整博客分类与对应链接。 本文关注的问题为&#xff1a;是否可以使用合成数据&#xff08;Synthetic Data&#xff09;用于模型选择&#xff1f;即不…

LeetCode383. 赎金信(C++)

LeetCode383. 赎金信 题目链接代码 题目链接 https://leetcode.cn/problems/ransom-note/description/ 代码 class Solution { public:bool canConstruct(string ransomNote, string magazine) {int record[26] {0};if(ransomNote.size() > magazine.size()) return fa…

浅析扩散模型与图像生成【应用篇】(四)——Palette

4. Palette: Image-to-Image Diffusion Models 该文提出一种基于扩散模型的通用图像转换&#xff08;Image-to-Image Translation&#xff09;模型——Palette&#xff0c;可用于图像着色&#xff0c;图像修复&#xff0c;图像补全和JPEG图像恢复等多种转换任务。Palette是一种…

【Web自动化测试——代码篇十二】自动化测试模型——数据驱动测试和关键字驱动测试

&#x1f525; 交流讨论&#xff1a;欢迎加入我们一起学习&#xff01; &#x1f525; 资源分享&#xff1a;耗时200小时精选的「软件测试」资料包 &#x1f525; 教程推荐&#xff1a;火遍全网的《软件测试》教程 &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1…

基于springboot+vue的可盈保险合同管理系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…