問題描述
我正在嘗試使用 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 和類似瀏覽器的瀏覽器?
遠程調試端口"是我的問題的解決方案.
我將remote-debugging-port=XXXX"添加到我的 CEF(Chromium 嵌入式框架)命令中:https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html這讓我可以通過 localhost:XXXX 查看和管理我的應用程序的 CEF 窗口.
我同時使用了 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 項目中使用這些模塊,請按照以下步驟操作:
- 下載 selenium-server-standalone jar 文件(例如 selenium-server-standalone-3.141.59.jar)https://www.seleniumhq.org/download/
- 下載 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/
- 將兩個 jars 添加到您的項目結構中:文件 → 項目結構 → 依賴項 → +
現在所有的 Selenium 和 Winium 導入都應該可以工作了.例如:
import org.openqa.selenium.By;導入 org.openqa.selenium.WebDriver;導入 org.openqa.selenium.chrome.ChromeDriver;導入 org.openqa.selenium.chrome.ChromeOptions;導入 org.openqa.selenium.support.ui.ExpectedConditions;導入 org.openqa.selenium.support.ui.WebDriverWait;導入 org.openqa.selenium.winium.DesktopOptions;導入 org.openqa.selenium.winium.WiniumDriver;導入 org.openqa.selenium.winium.WiniumDriverService;
將 ChromeDriver 與 Selenium 一起使用
按照以下步驟操作:
- 安裝 Java JDK 并將其 bin 目錄添加到系統 PATH變量.
- 創建一個包含所有文件的目錄.本教程使用 c: emp.
- 下載 ChromeDriver 并解壓(如 chromedriver_win32.zip 提供 chomedriver.exe)https://sites.google.com/a/chromium.org/chromedriver/下載
- 下載 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
- 從 Cefbuilds 下載 CEF 二進制分發客戶端并提取(例如 cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64.tar.bz2)http://opensource.spotify.com/cefbuilds/index.html
- 您的目錄結構現在應該如下所示:
<塊引用>
c: empcef_binary_3.2171.1979_windows32_client發布cefclient.exe(和其他文件)chromedriver.exeExample.javaselenium-server-standalone-2.44.0.jar
有關更多信息,您可以閱讀此 wiki:https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md
在 Winium 中使用 WiniumDriver
按照以下步驟操作:
- 下載 Winium.Desktop.Driver.zip 并解壓到 c: emphttps://github.com/2gis/Winium.Desktop/releases
Java 代碼示例
啟動一個winium驅動并打開你的應用:
DesktopOptions desktopOption = new DesktopOptions();desktopOption.setApplicationPath("Path_to_your_app.exe");文件 drivePath = new File("C:\temp\Winium.Desktop.Driver.exe");WiniumDriverService 服務 = 新 WiniumDriverService.Builder().usingDriverExecutable(drivePath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();服務.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("遠程調試端口=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();
I'm trying to automate a Windows application GUI using Winium. The app is using both WPF windows and "Chrome Legacy Window" (Chromium) windows.
I'm using the tool "Automation Spy" to inspect the GUI elements' ids inside the WPF windows to use with Winium. Automation Spy can't inspect elements in the "Chrome Legacy Window" windows in the same manner that Winium can't access these elements.
"Chrome Legacy Window" is a WEB window, so it requires automation with Selenium.
How do I use Selenium to hook on a Chromium window, which is not a browser like Firefox, Chrome and similar?
"Remote Debugging Port" was the solution for my problem.
I added the "remote-debugging-port=XXXX" to my CEF (Chromium Embedded Framework) command: https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html This allowed me to see and manage my app's CEF windows through localhost:XXXX.
I used both Winium and Selenium to test my app. Winium for all my WPF windows and selenium for all my CEF windows. My GUI test starts with Winium Driver to open my app and navigate WPF windows. Each time I need to debug a CEF window, I'm opening a Chrome Driver using selenium with the "remote-debugging-port" argument, which allows me to click elements inside that window. When I'm finish with this chromium window I'm closing the selenium driver and continues with Winium.
Use Selenium and Winium with IntelliJ IDEA
Selenium is a portable framework for testing and automating web applications. Winium is a Selenium-based tool for testing and automating desktop applications on Windows. In order to use these modules inside IntelliJ IDEA project, follow these steps:
- Download selenium-server-standalone jar file (e.g. selenium-server-standalone-3.141.59.jar) https://www.seleniumhq.org/download/
- Download winium-webdriver jar file (e.g. winium-webdriver-0.1.0-1.jar) http://central.maven.org/maven2/com/github/2gis/winium/winium-webdriver/0.1.0-1/
- Add both jars to your project structure: File → Project Structure → Dependencies → +
Now all Selenium and Winium imports should work. For example:
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;
Use ChromeDriver with Selenium
Follow these steps:
- Install the Java JDK and add its bin directory to your system PATH variable.
- Create a directory that will contain all files. This tutorial uses c: emp.
- Download ChromeDriver and extract it (e.g. chromedriver_win32.zip provides chomedriver.exe) https://sites.google.com/a/chromium.org/chromedriver/downloads
- Download selenium-server-standalone-X.X.X-alpha-1.zip (e.g. selenium-server-standalone-4.0.0-alpha-1.zip) http://selenium-release.storage.googleapis.com/index.html
- Download a CEF binary distribution client from Cefbuilds and extract (e.g. cef_binary_76.1.13+gf19c584+chromium-76.0.3809.132_windows64.tar.bz2) http://opensource.spotify.com/cefbuilds/index.html
- Your directory structure should now look similar to this:
c: emp cef_binary_3.2171.1979_windows32_client Release cefclient.exe (and other files) chromedriver.exe Example.java selenium-server-standalone-2.44.0.jar
For more information you can read this wiki: https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md
Use WiniumDriver with Winium
Follow these steps:
- Download the Winium.Desktop.Driver.zip and extract it to c: emp https://github.com/2gis/Winium.Desktop/releases
Java Code Example
Start a winium driver and open your app:
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);
Navigate and test WPF windows using winium. For example:
winiumDriver.findElement(By.id("someElementID")).click();
Create a ChromeOptions object which holds all needed selenium information such as chromium client and remote debugging port. Make sure to change the "XXXX" port to your remote debugging port.
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");
Open a chrome driver (selenium) using the chrome options object.
WebDriver seleniumDriver = ChromeDriver(chromeOptions);
Use element inside the chromium windows. For example:
seleniumWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("someButtonClassName")));
seleniumDriver.findElement(By.className("someButtonClassName")).click();
When you're done with the CEF window, close selenium driver and continue with the winium driver:
seleniumDriver.quit();
winiumDriver.findElement(By.id("someElementID")).click();
Close main winium driver:
winiumDriver.quit();
這篇關于“Chrome Legacy Window"的自動化(鉻)使用 Winium的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!