Skip to content

Use QtWebDriver to run your application

hekra01 edited this page Jan 14, 2017 · 26 revisions

Testing a plain html or qml application

Then using the prebuilt Webdriver out of the box is enough.

  • Start WebDriver
$ ./WebDriver --verbose
  • Use Selenium to open the application
    WebDriver driver = new RemoteWebDriver(new URL("http://192.168.24.1:9517"), capabilities);
    driver.get("http://example.com/example.html");
    // or
    driver.get("http://example.com/example.qml");
    System.out.println("Plain HTML or QML page source:\n" + driver.getPageSource());

Testing a native or hybrid application

There are 2 ways:

Option 1: WebDriver attaches to the running application

    WebDriver driver = new RemoteWebDriver(new URL("http://192.168.24.1:9517"), capabilities);
    System.out.println("Native page source:\n" + driver.getPageSource());
    WebElement elt = driver.findElement(By.xpath("//QTextEdit"));
  • Example 1: A basic example of an application using QtWebDriver main.cc
  • Example 2: The changes needed to Qt-5.5/widgets/mainwindows/mainwindow to add QtWebDriver support
  • mainwindow.pro
--- mainwindowold.pro	2017-01-13 15:02:41.981524867 -0800
+++ mainwindow.pro	2017-01-13 15:06:53.048081230 -0800
@@ -1,6 +1,17 @@
 TEMPLATE = app
 
-QT += widgets
+QT += widgets quick
+INCLUDEPATH += /home/hekra01/qtwebdriver/out/dist/desktop/release/Test
+INCLUDEPATH += /home/hekra01/qtwebdriver/out/dist/desktop/release/h
+LIBS += -L/home/hekra01/qtwebdriver/out/dist/desktop/release/libs
+LIBS += -lchromium_base -lWebDriver_core -lWebDriver_extension_qt_base -lWebDriver_extension_qt_quick -lWebDriver_extension_qt_quick_web -lWebDriver_extension_qt_web
+DEFINES += QT_NO_SAMPLES="1" 
  • main.cpp
--- mainold.cpp	2017-01-13 15:46:00.327079422 -0800
+++ main.cpp	2017-01-13 15:45:33.663764896 -0800
@@ -32,6 +32,7 @@
 ****************************************************************************/
 
 #include "mainwindow.h"
+#include "Headers.h"
 
 #include <QApplication>
 #include <QPainterPath>
@@ -149,6 +150,7 @@ int main(int argc, char **argv)
 {
     QApplication app(argc, argv);
     QMap<QString, QSize> customSizeHints = parseCustomSizeHints(argc, argv);
+    wd_setup(argc, argv);
     MainWindow mainWin(customSizeHints);

Pros:

  • Easy to setup
  • The application lifecylce is not impacted. It starts as it should, not on request of WebDriver

Cons:

  • Need to modify the application code in order to run webdriver. But a such additional code can be flagged for test build only.

Option 2: WebDriver creates the application

  • Modify main.cc to register your application:
int wd_samples_setup(webdriver::ViewCreator* widgetCreator,
    webdriver::ViewCreator* webCreator,
    webdriver::ViewCreator* qmlCreator,
    CommandLine &cmd_line)
{
    if(widgetCreator != NULL)
    {
        widgetCreator->RegisterViewClass<MyWidgetClass>("MyWidgetClass");
...

(See Samples.h )

  • Rebuild Webdriver and rerun WebDriver
$ ./build.sh
$ out/dist/desktop/release/bin/WebDriver --verbose
  • Use Selenium to make WebDriver create you application:
    WebDriver driver = new RemoteWebDriver(new URL("http://192.168.24.1:9517"), capabilities);
    // This will trigger creation of a MyWidgetClass
    wd.get("qtwidget://MyWidgetClass");
    System.out.println("Native page source:\n" + driver.getPageSource());
    WebElement elt = driver.findElement(By.xpath("//QTextEdit"));

Pros:

  • No need to modify the application code

Cons:

  • Need to modify the WebDriver build to add the application classes. It must be possible to build the application using the WebDriver build system
  • The application lifecylce can be impacted. WebDriver will call the default constructor of the application class, not knowing about any parameters or preconditions.

Also See

Clone this wiki locally