首页 > 解决方案 > 使用 Winium 实现“Chrome Legacy Window”(Chromium)的自动化

问题描述

我正在尝试使用 Winium 自动化 Windows 应用程序 GUI。该应用程序同时使用 WPF 窗口和“Chrome Legacy Window”(Chromium)窗口。

我正在使用工具“Automation Spy”来检查 WPF 窗口内的 GUI 元素的 ID,以便与 Winium 一起使用。Automation Spy 无法检查“Chrome Legacy Window”窗口中的元素,就像 Winium 无法访问这些元素一样。

“Chrome Legacy Window”是一个 WEB 窗口,因此需要使用 Selenium 进行自动化。

如何使用 Selenium 连接 Chromium 窗口,该窗口不是 Firefox、Chrome 等类似浏览器?

标签: javaseleniumautomated-testswinium

解决方案


“远程调试端口”是我的问题的解决方案。

  1. 我在我的 CEF(Chromium 嵌入式框架)命令中添加了“remote-debugging-port=XXXX”: https ://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html 这让我通过 localhost:XXXX 查看和管理我的应用程序的 CEF 窗口。

  2. 我同时使用 Winium 和 Selenium 来测试我的应用程序。Winium 用于我所有的 WPF 窗口,而 selenium 用于我的所有 CEF 窗口。我的 GUI 测试从 Winium 驱动程序开始,以打开我的应用程序并导航 WPF 窗口。每次我需要调试 CEF 窗口时,我都会使用 selenium 和“remote-debugging-port”参数打开一个 Chrome 驱动程序,它允许我单击该窗口内的元素。当我完成这个 chromium 窗口时,我将关闭 selenium 驱动程序并继续使用 Winium。

通过 IntelliJ IDEA 使用 Selenium 和 Winium

Selenium 是一个可移植的框架,用于测试和自动化 Web 应用程序。Winium 是一个基于 Selenium 的工具,用于在 Windows 上测试和自动化桌面应用程序。为了在 IntelliJ IDEA 项目中使用这些模块,请按照下列步骤操作:

  1. 下载 selenium-server-standalone jar 文件(例如 selenium-server-standalone-3.141.59.jar) https://www.seleniumhq.org/download/
  2. 下载winium-webdriver jar 文件(如winium-webdriver-0.1.0-1.jar) http://central.maven.org/maven2/com/github/2gis/winium/winium-webdriver/0.1.0-1/
  3. 将两个 jars 添加到您的项目结构中:文件 → 项目结构 → 依赖项 → +
  4. 现在所有 Selenium 和 Winium 导入都应该可以工作了。例如:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.winium.DesktopOptions;
    import org.openqa.selenium.winium.WiniumDriver;
    import org.openqa.selenium.winium.WiniumDriverService;
    

将 ChromeDriver 与 Selenium 一起使用

按着这些次序:

  1. 安装 Java JDK 并将其 bin 目录添加到系统 PATH 变量中。
  2. 创建一个包含所有文件的目录。本教程使用 c:\temp。
  3. 下载 ChromeDriver 并解压(例如 chromedriver_win32.zip 提供 chomedriver.exe) https://sites.google.com/a/chromium.org/chromedriver/downloads
  4. 下载 selenium-server-standalone-XXX-alpha-1.zip(例如 selenium-server-standalone-4.0.0-alpha-1.zip) http://selenium-release.storage.googleapis.com/index.html
  5. 从 Cefbuilds 下载 CEF 二进制分发客户端并提取(例如 cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64.tar.bz2) http://opensource.spotify.com/cefbuilds/index.html
  6. 您的目录结构现在应该类似于以下内容:
c:\temp\
  cef_binary_3.2171.1979_windows32_client\
    Release\
      cefclient.exe  (and other files)
  chromedriver.exe
  Example.java
  selenium-server-standalone-2.44.0.jar

有关更多信息,您可以阅读此 wiki: https ://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md

将 WiniumDriver 与 Winium 一起使用

按着这些次序:

  1. 下载 Winium.Desktop.Driver.zip 并将其解压到 c:\temp https://github.com/2gis/Winium.Desktop/releases

Java 代码示例

启动一个 winium 驱动程序并打开您的应用程序:

DesktopOptions desktopOption = new DesktopOptions();
desktopOption.setApplicationPath("Path_to_your_app.exe");
File drivePath = new File("C:\\temp\\Winium.Desktop.Driver.exe");
WiniumDriverService service = new WiniumDriverService.Builder().usingDriverExecutable(drivePath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
service.start();
WiniumDriver winiumDriver = WiniumDriver(service, desktopOption);

使用 winium 导航和测试 WPF 窗口。例如:

winiumDriver.findElement(By.id("someElementID")).click();

创建一个 ChromeOptions 对象,该对象包含所有需要的 selenium 信息,例如 chromium 客户端和远程调试端口。确保将“XXXX”端口更改为您的远程调试端口。

System.setProperty("webdriver.chrome.driver", "c:/temp/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("c:/temp/cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64_client/Release/cefclient.exe");
chromeOptions.addArguments("remote-debugging-port=XXXX");

使用 chrome 选项对象打开 chrome 驱动程序 (selenium)。

WebDriver seleniumDriver = ChromeDriver(chromeOptions);

在铬窗内使用元素。例如:

seleniumWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("someButtonClassName")));
seleniumDriver.findElement(By.className("someButtonClassName")).click();

完成 CEF 窗口后,关闭 selenium 驱动程序并继续使用 winium 驱动程序:

seleniumDriver.quit();
winiumDriver.findElement(By.id("someElementID")).click();

关闭主要的winium驱动程序:

winiumDriver.quit();

推荐阅读