PowerShell調用Web測試載入器Selenium執行個體_PowerShell

來源:互聯網
上載者:User

什麼是Selenium

Selenium是一款著名的web應用程式測試載入器,它能通過在瀏覽器中類比使用者的動作來完成測試,其api支援java,c#,python,ruby,php,perl,javascript這些主流程式設計語言和指令碼語言。Selenium還支援IE,Firefox,和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:\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/)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.