gpt4 book ai didi

java - 在MacOS环境中使用selenium、java测试Electron应用程序

转载 作者:行者123 更新时间:2023-11-30 07:02:29 24 4
gpt4 key购买 nike

是否有使用 selenium-chromedriver 启动 Electron 应用程序的示例 java 代码?这就是我现在所处的位置。我的代码打开了 Electron 应用程序,我可以查看该页面上的 webElements,但不会启动我需要测试的应用程序。我可以将应用程序拖到 Electron 窗口并启动,但 WebDriver 没有指向它。

private void electronTest() throws Exception {

//select electron-chromedriver
System.setProperty("webdriver.chrome.driver", "/Users/username/work/node_modules/electron-chromedriver/bin/chromedriver");

ChromeOptions options = new ChromeOptions();
// path for Electron
options.setBinary("/Users/username/work/app/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron");
// I have tried both the folder and the app
options.addArguments("/Users/username/work/app/out/packages/mac/electronApplication.app");

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

driver = new ChromeDriver(capabilities);
// have also tried...
//driver = new RemoteWebDriver(new URL("http://localhost:9515"), capabilities);

// Electron page appears, but doesn't launch the Electron app

// driver is pointing to the electron page elements even if I drag the app to launch it
String screenText = " [" + driver.findElement(By.tagName("BODY")).getText().replace("\n", "][") + "]";
System.out.println("screenText " + screenText);
}

最佳答案

我可以给出一个在 macOS 上完美运行的示例,因为我最近一直在测试它们。

public void electronTest()
{
System.setProperty("webdriver.chrome.driver","path to the chromedriver");// You can skip this if chromedriver is already included in the PATH.

ChromeOptions options = new ChromeOptions();
options.setBinary("/Applications/YourApp.app/Contents/MacOS/YourApp");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);


// Now, your electron app would have been opened.
// Now if you open the dev tools using CMD+ALT+I you would notice two dev tools and first one being for the electron shell. We need to switch to the second window handle. Let's do that.

for (String handle : driver.getWindowHandles())
{
driver.switchTo().window(handle); // Since there are two window handles this would switch to last one(which is second one). You can also explicitly provide the window number.
}

// Let's navigate to a page
driver.navigate().to(URL);
// If you inspect using the Dev Tools, you would notice the second window Dev Tools corresponds to actual page you have opened.
// From here you can write the usual selenium script and it will work.
}

关于java - 在MacOS环境中使用selenium、java测试Electron应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40663615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com