"Chrome Legacy Window" (Chromium) 使用 Winium 的自动化

Automation for "Chrome Legacy Window" (Chromium) using Winium

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

我正在使用工具 "Automation Spy" 检查 WPF windows 中 GUI 元素的 ID,以便与 Winium 一起使用。 Automation Spy 无法以与 Winium 无法访问这些元素相同的方式检查 "Chrome Legacy Window" windows 中的元素。

"Chrome Legacy Window" 是一个 WEB window,因此它需要使用 Selenium 进行自动化。

我如何使用 Selenium 挂钩 Chromium window,它不是像 Firefox、Chrome 和类似的浏览器?

"Remote Debugging Port" 是我的问题的解决方案。

  1. 我将 "remote-debugging-port=XXXX" 添加到我的 CEF(Chromium 嵌入式框架)命令中: https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html 这使我能够通过 localhost:XXXX.

  2. 查看和管理我的应用程序的 CEF windows
  3. 我同时使用了 Winium 和 Selenium 来测试我的应用程序。 Winium 用于我所有的 WPF windows 和 selenium 用于我所有的 CEF windows。 我的 GUI 测试从 Winium 驱动程序开始,以打开我的应用程序并导航 WPF windows。 每次我需要调试 CEF window 时,我都会使用 selenium 和 "remote-debugging-port" 参数打开 Chrome 驱动程序,这样我就可以单击 window 中的元素。当我完成这个 chromium window 时,我将关闭 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. 将两个 jar 添加到您的项目结构中:文件 → 项目结构 → 依赖关系 → +
  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;
    

使用 ChromeSelenium 驱动程序

按照以下步骤操作:

  1. 安装 Java JDK 并将其 bin 目录添加到您的系统 PATH 变量。
  2. 创建一个包含所有文件的目录。本教程使用 c:\temp.
  3. 下载Chrome驱动并解压(如chromedriver_win32.zip提供chomedriver.exe) https://sites.google.com/a/chromium.org/chromedriver/downloads
  4. 下载 selenium-server-standalone-X.X.X-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

在 Winium 中使用 WiniumDriver

按照以下步骤操作:

  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 windows。例如:

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);

使用铬内元素windows。例如:

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

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

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

关闭主 winium 驱动程序:

winiumDriver.quit();