I want to test Qml application with Qt WebDriver.
I installed Qt 5.2.1 version and Qt Driver.
After it integrated an application with QtWebDriver, according to Use QtWebDriver to run your application.
main.cc
looks like following:
#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();
}
Application looks like:
When the application is running I can check Qt Driver status:
{ ~ } » 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"}}}
For accessing Qml elements I did according to this post: QML Specifics:
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"
}
}
Driver configuration is the following:
@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;
}
}
However, when I want to execute:
Object object = Driver.getDefault().executeScript("var v = ObjectNameUtils.findChild('vradio_direct_button'); return v.text;");
Logger.info("Here is JS output:" + object);
I got:
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
Native page sources looks:
<?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>
UPDATE:
I had read this post before made this post. However, I didn't get the full idea.
I understand that I have to connect directly to my QDeclarativeView
and for this reason, I have to specify it with capabilities for QtDriver:
cap.setCapability("browserClass", "MyWebView");
Also, I have to set it on the server side:
int main(int argc, char *argv[])
{
//Configure web support
webdriver::ViewCreator* webCreator = new webdriver::QWebViewCreator();
//Register custom web view subclass
webCreator->RegisterViewClass<QDeclarativeView>("MyWebView");
Am I right? Or something is missing from making it work?
How to solve this error and make click on a button?
cap.setCapability("browserStartWindow", "*");
will make QtWebdriver select the first window it finds.
In your case, as seen from the native source, it will be the MainWindow, which is a QWidget, which does not support JS, hence the error message.
Yo need to select explicitly the QDeclarativeview.
This link:https://github.com/cisco-open-source/qtwebdriver/wiki/Hybridity-And-View-Management provides more details on how to do it as well as code samples