Skip to content

Use QtWebDriver to run your application

hekra01 edited this page Aug 15, 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

Pros:

  • Easy to setup
  • The application lifecycle 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.

Example 1: A basic example of an application using QtWebDriver

See main.cc.

Example 2: Add QtWebDriver support to qtbase/examples/widgets/mainwindows/mainwindow.

See example for:

Instructions:

  • Add the QtWebDriver headers and libraries in the application build environment
  • In the application code, include the headers: Headers.h
  • Call the wd_setup(argc, argv) function with the parameters you want (See full argument list: Command Line Switches)
  • In your application's build system set appropriate #defines for features required from QtWebDriver (e.g. to add support for QWebView define WD_ENABLE_WEB_VIEW=1, to disable sampling define QT_NO_SAMPLES with any value). This is necessary because these values are used in Headers.h which is compiled into your application.
  • Rebuild an run your application, this will start QtWebDriver
  • Connect your Selenium session to the QtWebDriver server running inside your application, e.g. in Java:
    DesiredCapabilities capability = new DesiredCapabilities();
    //Start WebDriver by reusing existing widget UI
    capability.setCapability("browserStartWindow", "*");
    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"));
    
    Note that we connect to it as a generic "remote" web driver rather than any particular browser.

Option 2: WebDriver creates the application

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 lifecycle can be impacted. WebDriver will call the default constructor of the application class, not knowing about any parameters or preconditions.

Example: Adding Qt's Calculator application to QtWebDriver See here.

Instructions:

  • 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 your 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"));

See Also