在 Java 环境中使用 org.openqa.selenium 测试 Electron 应用程序 (Intellij)

Testing Electron application with org.openqa.selenium in a Java environment (Intellij)

有没有办法在 Electron 应用程序的 Java 环境中使用 cucumberselenium-webdriver 创建自动化场景?

我在 electron.atom.io 上找到了一些 Node.js 解决方案,但我更喜欢 Java。

谢谢。

您可以通过 ChromeDriver 使用 Electron 浏览器。尝试使用类似的设置创建 WebDriver:

// If chromediver executable is not in your project directory, 
//  point to it with this system variable
System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe"); 

Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("binary", "path/to/electron/binary");
chromeOptions.put("args", Arrays.asList(" path-to-electron-app"));
//eg.: chromeOptions.put("binary", "D:\electron-quick-start\node_modules\electron-prebuilt\dist\electron.exe");
//     chromeOptions.put("args", Arrays.asList(" D:\electron-quick-start"));
//  for some reason the app arg needs to follow a space on my Windows machine
    
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", chromeOptions);
capabilities.setBrowserName("chrome");

WebDriver driver = new ChromeDriver(capabilities);

这里path-to-electron-app是应用源码(main.js)存放的目录,electron binary取自构建过程中下载的依赖

或者,如果您想使用预编译的应用程序 - 它本身变成电子二进制文件,您可以使用以下内容:

System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe"); 
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("binary", "D:\my-electron-app.exe");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", chromeOptions);
capabilities.setBrowserName("chrome");

WebDriver driver = new ChromeDriver(capabilities);