因為工作需要,要對一個windows的桌面程序做一個登陸的自動化測試。網上搜索了一番,決定使用Appium+WinAppDriver。
於是裝好環境后,參考了一下官網給出的例子(以下代碼來自WinAppDriver給出Java sample):
public static void setup() { try { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"); CalculatorSession = new WindowsDriver(new URL("http://127.0.0.1:4723"), capabilities); CalculatorSession.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); CalculatorResult = CalculatorSession.findElementByAccessibilityId("CalculatorResults"); Assert.assertNotNull(CalculatorResult); }catch(Exception e){ e.printStackTrace(); } finally { } }
可是很不幸,不知道為啥,我在標紅的那一步就出了exception:Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create a new remote session. Please check the server log for more details. Original error: Failed to locate opened application window with appId: C:\xxx\xxx.exe, and processId: 19836 (WARNING: The server did not provide any stacktrace information)
個人猜測是因為我測試的桌面應用程序啟動太慢了,導致WinAppDriver查找應用的時候超時了,可是上網找了很久都沒有找到合適的解決方法,最后在WinAppDriver v1.2rc的發布comment里面發現有一個參數可以解決這個問題:
capabilities.setCapability("ms:waitForAppLaunch","10"); 10是以秒為單位設置WinAppDriver等待應用加載的時間,以下是原文和連接:
https://github.com/Microsoft/WinAppDriver/releases
The ms:waitForAppLaunch capability enables WinAppDriver to wait for a defined amount of time after an app launch is initiated prior to attaching to the application session. The limit for this is 50 seconds.
C#: appCapabilities.SetCapability("ms:waitForAppLaunch", "25"); to add app delay of 25 seconds.
加上這句話后就可以成功啟動應用並進行后續操作了。
