手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >PowerShell调用Web测试工具Selenium实例
PowerShell调用Web测试工具Selenium实例
摘要:什么是SeleniumSelenium是一款著名的web应用程序测试工具,它能通过在浏览器中模拟用户的动作来完成测试,其api支持java,...

什么是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('http://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:testa.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/)

【PowerShell调用Web测试工具Selenium实例】相关文章:

Powershell小技巧之获取字符串的行数

Powershell实现按类型排序

Powershell后台作业、异步操作实例

Powershell使用C#实现缩写路径

PowerShell入门教程之PowerShell管道介绍

PowerShell中调用WPF生成炫酷窗口实例

Powershell小技巧之删除不规则字符

Windows Powershell 变量的幕后管理

Windows Powershell 变量的类型和强类型

PowerShell时间记录脚本

精品推荐
分类导航