首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MacOS环境下使用selenium、java测试电子应用

在MacOS环境下使用selenium、java测试电子应用
EN

Stack Overflow用户
提问于 2016-11-18 03:38:08
回答 4查看 1.9K关注 0票数 1

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

代码语言:javascript
复制
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);
}
EN

回答 4

Stack Overflow用户

发布于 2016-12-21 23:29:58

我可以举一个在macOS上完美工作的例子,因为我最近一直在测试它们。

代码语言:javascript
复制
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.
}
票数 1
EN

Stack Overflow用户

发布于 2016-11-18 04:33:46

你可以阅读Using Selenium and WebDriver是如何。我通常不会粘贴链接,但你应该看看这篇文章。其他章节也可能对你有用。

票数 0
EN

Stack Overflow用户

发布于 2018-07-18 20:06:44

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

代码语言:javascript
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40663615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档