当前位置: 移动技术网 > IT编程>脚本编程>Shell > PowerShell调用Web测试工具Selenium实例

PowerShell调用Web测试工具Selenium实例

2017年12月08日  | 移动技术网IT编程  | 我要评论

什么是selenium

selenium是一款著名的web应用程序测试工具,它能通过在浏览器中模拟用户的动作来完成测试,其api支持java,c#,python,ruby,php,perl,javascript这些主流编程语言和脚本语言。selenium还支持ie,火狐,和chrome等主流浏览器。

powershell 如何直接调用selenium

powershell直接调用selenium,其实类似powershell调用c#方法。以ie浏览器为例,至少需要具备两个文件:

1.iedriverserver.exe 启动ie代理,模拟用户操作
2.webdriver.dll 暴露api给用户,对浏览器进行控制

纯powershell调用selenium的例子

复制代码 代码如下:

add-type -path .\webdriver.dll
$sens='openqa.selenium'
 
# 初始化ie驱动实例
$iedriver= new-object "$sens.ie.internetexplorerdriver"
 
$nav=$iedriver.navigate()
$nav.gotourl('//www.jb51.net')
 
# 设置文本框的值
$search=$iedriver.findelementbyid('s')
$search.sendkeys('测试框架')
 
# 提交表单
$search.submit()
 
$iedriver.findelementbyclassname('class')
$iedriver.findelementbyid('id')
$iedriver.findelementbylinktext(' a link')
$iedriver.findelementbypartiallinktext(' powershell')
$iedriver.findelementbyname('username')
$iedriver.findelementbycssselector('')
# 截屏并保存
$screenshot=$iedriver.getscreenshot()
[io.file]::writeallbytes('d:\test\a.jpg',$screenshot.asbytearray)
 
# 关闭ie进程
$iedriver.quit()

为什么要使用selenium

看了上面的调用,有的哥们要开始质疑了这里面主要的方法是:找element。和internetexplorer.application中的html document类中的关键方法类似啊,byid,byname,byclass,bytag。这么想,没错。
但是经本人测试,如果web页面稍显复杂,internetexplorer.application中的除了getelementbyid可以勉强接受,其它的方法慢的一塌糊涂。而selenium却表现非常良好。
另外selenium支持css selector查找结点,这无疑是最给力的了,让jquery也投入战斗。
最后一点当然是selenium对于浏览器的兼容性优势了。

借助selenium powershell extensions

这是一个对selenium进行封装的powershell开源扩展工具,如果你觉得不爽,也可以自己重构或者直接重写。代码托管在:
1.github(http://www.pstips.net/goto/https://github.com/apetrovskiy/stups)
2.codeplex(http://www.pstips.net/goto/http://sepsx.codeplex.com/)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网