1

我想用Qt WebDriver测试 Qml 应用程序。

我安装了 Qt 5.2.1 版本和 Qt 驱动程序。
在它与 QtWebDriver 集成一个应用程序之后,根据Use QtWebDriver to run your application

main.cc如下所示:

#define WD_ENABLE_WEB_VIEW 0
#define QT_NO_SAMPLES 1

#include "mainwindow.h"
#include <QApplication>
#include "Headers.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    wd_setup(argc, argv);
    w.show();

    return a.exec();
}

应用程序看起来像:

在此处输入图像描述

当应用程序运行时,我可以检查 Qt 驱动程序状态:

{ ~ }  » curl localhost:9517/status                                                                                                   ~
{"status":0,"value":{"build":{"revision":"WebDriver-cisco-cmt-1.3.0-87-g965f9466de-dirty","time":"Nov 15 2017 17:02:18 PST","version":"1.3.3"},"os":{"arch":"x86","name":"Windows NT","version":"6.2"}}}

为了访问我根据这篇文章所做的 Qml 元素:QML 细节

Rectangle {
      id: buttonDirect
      objectName: "vradio_direct_button"

      property string text: "Direct"
      ...
      Text {
          id: buttonDirectLabel; text: buttonDirect.text; anchors.centerIn: buttonDirect; color: activePalette.buttonText; font.pixelSize: 24
          objectName: "vradio_direct_button"
      }
  }

驱动配置如下:

    @Override
    public RemoteWebDriver getDriver() {
        RemoteWebDriver remoteWebDriver = null;

        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("maximize", true);
        //specify reuseUI to have WebDriver terminate any previous session and reuse its windows
        cap.setCapability("reuseUI", true);

        //specify to select the first found window
        cap.setCapability("browserStartWindow", "*");

        LoggingPreferences logs = new LoggingPreferences();
        Level level = Level.ALL;
        logs.enable(LogType.DRIVER, level);
        logs.enable(LogType.BROWSER, level);
        logs.enable(LogType.PERFORMANCE, level);
        // specify log level
        cap.setCapability(CapabilityType.LOGGING_PREFS, logs);

        try {
            URL url = new URL("http://localhost:9517");
            remoteWebDriver = new RemoteWebDriver(url, cap);

            Logger.debug("Native Page Sources:\n" + remoteWebDriver.getPageSource());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return remoteWebDriver;
    }
  }

但是,当我想执行时:

Object object = Driver.getDefault().executeScript("var v = ObjectNameUtils.findChild('vradio_direct_button'); return v.text;");
Logger.info("Here is JS output:" + object);

我有:

org.openqa.selenium.WebDriverException: Script execution failed. Script: var v = ObjectNameUtils.findChild('vradio_direct_button'); return v.text;;
 Current view doesnt support this command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.7.0', revision: '2321c73', time: '2017-11-02T22:22:35.584Z'
System info: host: 'LVL1305001', ip: '172.22.73.100', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptSslCerts: false, applicationCacheEnabled: true, browserConnectionEnabled: true, browserName: QtWebkit, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hybrid: {qml2: true, qtVersion: 5.2.1, widget: true}, javascriptEnabled: true, locationContextEnabled: false, nativeEvents: true, platform: XP, platformName: XP, remotePlayerEnabled: true, reuseUI: true, rotatable: false, takesElementScreenshot: true, takesScreenshot: true, webStorageEnabled: true}
Session ID: bfa073ee2e334f4977281996f3240b90

本机页面源看起来:

<?xml version="1.0" encoding="UTF-8"?>
<MainWindow elementId="2312dc5007f290f9bf4f3f92d3aa6884" className="MainWindow">
    <QRubberBand id="qt_rubberband" elementId="45050a91d64ad4cde3e3df8091813f54" className="QRubberBand"/>
    <QDeclarativeView elementId="3c4890eed00091c95bde9a653f59d22e" className="QDeclarativeView">
        <QWidget id="qt_scrollarea_hcontainer" elementId="ff075abbbeb5fe23501c7da3241def96" className="QWidget">
            <QScrollBar elementId="51188e66ef541bc0563bea5780cf8ed2" className="QScrollBar"/>
        </QWidget>
        <QWidget id="qt_scrollarea_vcontainer" elementId="3692bfb1340019907b7982f1ef2f821f" className="QWidget">
            <QScrollBar elementId="e76434466faf78d008ca5b096719e188" className="QScrollBar"/>
        </QWidget>
        <QWidget elementId="ac4c1e620945ad4784fbbcd3c60ae3f1" className="QWidget"/>
    </QDeclarativeView>
</MainWindow>

更新:

在发表这篇文章之前,我已经阅读了这篇文章。但是,我没有得到完整的想法。

我知道我必须直接连接到我QDeclarativeView的,因此,我必须使用 QtDriver 的功能来指定它:

cap.setCapability("browserClass", "MyWebView");

另外,我必须在服务器端设置它:

int main(int argc, char *argv[])
{
    //Configure web support
    webdriver::ViewCreator* webCreator = new webdriver::QWebViewCreator();
    //Register custom web view subclass
    webCreator->RegisterViewClass<QDeclarativeView>("MyWebView");

我对吗?或者让它工作缺少了什么?

如何解决此错误并单击按钮?

4

1 回答 1

1
cap.setCapability("browserStartWindow", "*");

将使 QtWebdriver 选择它找到的第一个窗口。

在您的情况下,从本机源代码中可以看出,它将是 MainWindow,它是一个 QWidget,它不支持 JS,因此会出现错误消息。

你需要明确选择 QDeclarativeview。

此链接:https ://github.com/cisco-open-source/qtwebdriver/wiki/Hybridity-And-View-Management 提供了有关如何执行此操作的更多详细信息以及代码示例

于 2017-11-21T02:00:52.853 回答