【问题标题】:How to write Selenium tests for CEF applications如何为 CEF 应用程序编写 Selenium 测试
【发布时间】:2019-09-06 08:22:05
【问题描述】:

我有一个C++ 应用程序application.exe,它使用CEF browser(chrome 嵌入式框架)作为 UI。

对于这个 UI,我想用 Selenium 开发自动化测试。通常我通过Python控制Selenium

我曾多次使用chromedriver 并开发过浏览器测试。 我不太明白如何使用它来控制CEF browser

我已经在这里找到了一些包含相同主题的页面。但是,我还是不明白SeleniumCEF browser之间的交互。

我的目标是 Selenium 启动 application.exe 以便我可以控制 CEF browser 内的 Web 元素。

【问题讨论】:

    标签: python selenium automated-tests chromium-embedded


    【解决方案1】:

    您需要在chromeoptions中设置setBinary,并提供CEF浏览器.exe文件的路径。

    Java 示例代码:

    public class Example  {
      public static void main(String[] args) {
        // Path to the ChromeDriver executable.
        System.setProperty("webdriver.chrome.driver", "c:/temp/chromedriver.exe");
    
        ChromeOptions options = new ChromeOptions();
        // Path to the CEF executable.
        options.setBinary("c:/temp/cef_binary_3.2171.1979_windows32_client/Release/cefclient.exe");
        // Port to communicate on. Required starting with ChromeDriver v2.41.
        options.addArguments("remote-debugging-port=12345");
    
        WebDriver driver = new ChromeDriver(options);
        driver.get("http://www.google.com/xhtml");
        sleep(3000);  // Let the user actually see something!
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("ChromeDriver");
        searchBox.submit();
        sleep(3000);  // Let the user actually see something!
        driver.quit();
      }
    
      static void sleep(int time) {
        try { Thread.sleep(time); } catch (InterruptedException e) {}
      }
    }
    

    来源:

    https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md

    https://www.ultimateqa.com/chromium-embedded-framework/

    注意:您可能需要在项目中使用不同版本的 chrome 驱动程序二进制文件和 chrome 版本依赖项。确定与您当前版本的CEF browser兼容的版本

    Chrome 驱动二进制文件下载链接:

    https://chromedriver.storage.googleapis.com/index.html

    【讨论】:

    • 到目前为止一切顺利。不幸的是,我在启动 CEF application.exe 时收到以下错误消息:该文件应实现函数 void Start() 以初始化应用程序并订阅所有必要的事件,例如帧更新。
    • 另外,我还有以下问题:application.exe本身不是CEF进程,只是数据初始化后才启动。我不确定如何自动测试这个CEF 进程。
    • 如果它是桌面应用程序,那么您可以使用 winium 或 winappdriver(windows) 它将触发应用程序然后您需要设法存储 cookie 并传递给 webdriver 实例......这是我从来没有的方法自己试过了
    • 你得到的错误是因为浏览器的版本,你使用的依赖项也和chromedriver一起使用......你需要找到匹配的版本
    • 检查这里提供的工作和测试版本:bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver.md