【问题标题】:Testing Electron app using selenium, java in MacOS environment在 MacOS 环境中使用 selenium、java 测试 Electron 应用程序
【发布时间】:2017-04-01 12:14:04
【问题描述】:

是否有使用 selenium-chromedriver 启动电子应用程序的示例 java 代码?这就是我现在的位置。我的代码启动了电子应用程序,我可以在该页面上查看 webElements,但没有启动我需要测试的应用程序。我可以将应用程序拖到电子窗口并启动它,但 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);
}

【问题讨论】:

  • 取得进展... 我没有启动 Electron 并尝试将路径传递给构建,而是构建了应用程序,并将二进制文件设置为 .app 中的 MacOS 构建:选项.setBinary("/Users/username/work/ape/out/packages/mac/appName.app/Contents/MacOS/appName");现在这会启动应用程序本身,但有必要抓住第二个窗口句柄并在窗口出现时执行 driver.switchTo。

标签: java macos selenium electron selenium-chromedriver


【解决方案1】:

我可以举一个在 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.
}

【讨论】:

    【解决方案2】:

    您可以阅读Using Selenium and WebDriver。我通常不粘贴链接,但您可能应该阅读此内容。其他章节也可能对您有用。

    【讨论】:

      【解决方案3】:

      这是我在 MacOS 上使用 seleniumWebDriver + Java 启动电子应用程序的示例

          System.setProperty("webdriver.chrome.driver", "ChromeDriverPath");
          ChromeOptions options = new ChromeOptions();
          options.setBinary(binaryPath);
          options.addArguments("--app=" + argPath);
          options.setCapability("chromeOptions", options);
          options.setCapability("browserName", "chrome");
      
          driver = new ChromeDriver(options);
      

      【讨论】:

        【解决方案4】:

        您是否尝试在 selenium 网格的帮助下在远程驱动程序上使用相同的方法? 我的应用程序是一个基于电子的消息传递应用程序,需要在两个应用程序之间发送接收消息。我可以在我的本地机器上做任何事情,发送即时消息,使用所有按钮,登录注销等,但是在使用为节点机器设置的远程驱动程序时,即使调用了应用程序的二进制文件并启动了应用程序,远程驱动不能做任何事情,

        public class remotetest {
        WebDriver Rdriver;
        @Test
        public void launch() throws InterruptedException, IOException {
        
            System.setProperty("webdriver.chrome.driver","D:\\work\\grid\\chromedriver.exe"); // chromedriver path
        
            ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Users\\myol\\AppData\\Local\\Programs\\ConnectMe_S4B2015\\Connect Me S4B2015.exe");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            capabilities.setBrowserName("chrome");
            capabilities.setPlatform(Platform.WINDOWS);
        
            WebDriver Rdriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
        
                for (String hand : Rdriver.getWindowHandles()) {
        
                Rdriver.switchTo().window(hand);
                }
        
            Rdriver.findElement(By.id("username-kandy")).sendKeys("2311");
        
        
        
        
        }
        

        【讨论】:

        • 发现需要运行2.40以上的chrome版本情人。 “在 Chrome 内部运行的 DevTools 可以分配一个远程调试端口,然后将端口号放入用户数据目录下名为 DevToolsActivePort 的文件中。在 Electron 应用程序的情况下,ChromeDriver 无法找到这个 DevToolsActivePort 文件,因此显然 Electron没有创建此文件的代码。可能需要更新 Electron 中的 DevTools 服务器才能创建此文件。"
        猜你喜欢
        • 1970-01-01
        • 2021-06-16
        • 2023-01-29
        • 1970-01-01
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多