精選文章

bcp大量匯出與匯入資料庫資料方法

--匯出-- bcp "select * from [資料庫名稱].[dbo].[資料表名稱]" queryout 匯出檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼" " "...

2019年9月26日 星期四

Selenium-使用不同的瀏覽器

說明:
在前篇初始語法裡,使用瀏覽器是Chrome,但Selenium支援很多不同的瀏覽器, 這裡要教如何改更瀏覽器,可以實現同個案例,可以使用不同的瀏覽器進行驗證。

驅動瀏覽器
在初始語法裡,是寫在 [ClassInitialize],
就是每次執行此測試類別時,都會去執行,驅動Chrome如下
driver = new ChromeDriver();
想要使用其他瀏覽器,只要更改此段語法即可,在更改其他瀏覽器時,在using也要新增該瀏覽器的提示詞。

以第1個範例的程式碼進行修改-使用Firefox
將語法修改如下:
using OpenQA.Selenium.Chrome; => using OpenQA.Selenium.Firefox;
driver = new ChromeDriver(); => driver = new FirefoxDriver();
並執行測試,結果發生錯誤訊息,原因是,沒有安裝Firefox驅動,可以到NuGet安裝「Selenium.Firefox.WebDriver」
相關的瀏覽器驅動可以到https://www.seleniumhq.org/download/,有下載網址。
錯誤訊息:
類別初始設定方法 UnitTestProject1.UnitTest1.InitializeClass 擲回例外狀況。OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.。
使用IE
操作與Firefox相同,也要去下載驅動,都完成後執行測試,還是發生錯誤訊息,
需要去調整IE的安全性,操作如下:
工具->網際網路選項->安全性,
將「網際網路」、「近端內部網路」、「信任的網站」、「限制的網站」
以上四個區域的「啟用受保護模式(需要重新啟動Internet Explorer)」,
都打勾或都不勾

錯誤訊息:
System.InvalidOperationException: System.InvalidOperationException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (SessionNotCreated)。

沒有留言: