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

标签 java macos selenium electron selenium-chromedriver

是否有使用 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/

相关文章:

java - 自动网络表单填写中 Selenium 工具的替代品

selenium - 如何使用Selenium Webdriver中的xpath查找包含的文本的确切值?

html - css list-style-type decimal, numbering 自动化测试

macos - 旧版Mac发生奇怪的GCC初始化崩溃

javac 找不到符号构造函数服务

java - (Kotlin)如何将 HashMap 输出保存到文件

java - JTable更改列字体

php - 在 OS X 上的 PHP 5 上安装 OAuth PECL 包

c - Libnet基本使用

Java在文件正则表达式中替换