因为工作需要,要对一个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.
加上这句话后就可以成功启动应用并进行后续操作了。