Winforms應用程序的Selenium驅動程序
任何貢獻都受到歡迎:)
這個項目正在使用
為什麼另一個驅動程序,當已經有https://github.com/microsoft/winappdriver時?我正在測試的應用程序非常大,其中包含許多元素,並且該驅動程序在某些XPath查詢(請參閱此問題)上60秒後就在時間,然後將所有內容放在一起後,查詢完成了2秒鐘。
該項目依賴於MS UI自動化,該自動化是Windows可訪問性API的一部分。
官方文檔提到了對各種平台的支持:
在以下操作系統上支持UI自動化:Windows XP,Windows Server 2003,Windows Server 2003 R2,Windows Vista,Windows 7,Windows 10,Windows Server 2008,Windows Server 2008 R2,Windows Server 2012,Windows Server 2012 R2,Windows Server 2016和Windows Server 2016和Windows Server 2019。
我無法確認它將在這些平台上工作,因為我只在計算機上贏得了10個企業,但是所有必需的庫都是項目的一部分。
據我所知,不需要開發人員模式。
目前,沒有安裝程序。克隆存儲庫並從源頭構建可執行文件。使用WinAppDriver.ServerbinDebugWinAppDriver.Server.exe下的可執行文件
調用服務器時,必須使用最新的客戶端驅動程序版本3.141.0.0。
Ok Yes ,可以通過設置acceptAlertButtonCaptions功能,並使用半分離的值列表來添加其他字幕Cancel , No ,可以通過設置dismissAlertButtonCaptions功能添加其他字幕,並使用半分離的值列表不受支持的硒命令
X Path支持:
//node[3] //node[@attribute = 'X'] 僅實現此Appium命令
將引用添加到Selenium.WebDriver v3.141.0(https://www.nuget.org/packages/selenium.webdriver/3.141.0),您準備出發。
驅動程序可以啟動測試過程中的系統,也可以連接到運行過程。使用功能來定義要附加的過程。
如果沒有提供命令行參數,將在默認IP地址http://127.0.0.1:4444 4444上啟動服務器。
支持以下功能:
mode - start (默認值)或attachprocessId附加過程的IDprocessName附加過程的名稱exePath或app啟動該過程的可執行文件的路徑(目前無法提供參數)appWorkingDir設置新過程的工作目錄mainWindowTitle正則表達式,以幫助WinAppDriver縮小該過程以附加到sendKeyStrategy ( oneByOne | grouped | setValue ) - 用於在文本字段中鍵入文本的策略,默認值是oneByOne ,當前在會話期間無法更改此功能 public static RemoteWebDriver CreateSessionByStartingTheApplication()
{
DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.SetCapability("app", "<name of the program to start>");
// or "exePath" desktopCapabilities.SetCapability("exePath", "<path to the executable to start the process>");
// following capabilities should be provided for UWP applications like Calculator or Clocks & Alarms
// optional - to set the working directory
desktopCapabilities.SetCapability("appWorkingDir", "<path to run the process in>");
// optional - to identify the process
desktopCapabilities.SetCapability("processName", "<name of the process>");
// optional - to identify the main window
desktopCapabilities.SetCapability("mainWindowTitle", "<name of the process>");
return new RemoteWebDriver(
new CommandExec(new Uri("http://127.0.0.1:4444"),
TimeSpan.FromSeconds(60)),
desktopCapabilities);
}
public static RemoteWebDriver CreateSessionByAttachingToRunningProcess()
{
DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.SetCapability("mode", "attach");
// attach to process using process name
desktopCapabilities.SetCapability("processName", "<name of the process to attach to>");
// with (optional)
desktopCapabilities.SetCapability("windowTitle", "<regular expression to narrow down the list of matching processes>");
// or attach to process using process id
desktopCapabilities.SetCapability("processId", "<id of the process to attach to>");
return new RemoteWebDriver(
new CommandExec(new Uri("http://127.0.0.1:4444"),
TimeSpan.FromSeconds(60)),
desktopCapabilities);
}
推薦的元素位置使用XPath表達式(儘管表達式支持有限)
var webBrowser = session.FindElement(By.XPath("//Pane[@AutomationId='webBrowser']"));
支持的元素位置機制
TextBox#idWin應用程序中的Windows不像瀏覽器中的Windows。 Windows( ControlType.Window )可以嵌套在控制樹內,例如在Tab元素中。
var window = session.FindElement(By.XPath("/Window/Pane/Window[@AutomationId='WindowName']"));或通過切換到session.SwitchTo().Window("WindowName"); ,在第一個示例中,您將獲得元素參考,在另一個示例中,內部上下文切換到新窗口,並且在窗口cloded session.Close()時可以處理以下操作期間緩存的元素。
請注意,諸如OpenQA.Selenium.Support.UI.SelectElement之類的元素包裝器無效,因為內部select和option元素是期望的。
有一個醜陋的黑客,它繞過使用XPath Expression搜索子窗口時切換窗口的需求。默認情況下,搜索始於窗口的根部元素 - 主窗口或用戶切換到 - 搜索子窗口的窗口失敗,因為它不是當前根的直接子。修復程序開始搜索頂層的窗口。