summaryrefslogtreecommitdiffstats
path: root/tests/auto/core/webenginedriver/tst_webenginedriver.cpp
blob: cd3098b25cb95022fc3d3a8f092ec91f5c5ec918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QtTest>

#include <QtCore/qjsondocument.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qobject.h>
#include <QtCore/qprocess.h>
#include <QtGui/qimage.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtNetwork/qnetworkrequest.h>
#include <QtWebEngineCore/qtwebenginecoreglobal.h>
#include <QtWebEngineCore/qwebenginepage.h>
#include <QtWebEngineCore/qwebengineprofile.h>
#include <QtWebEngineWidgets/qwebengineview.h>

#include <widgetutil.h>

#define REMOTE_DEBUGGING_PORT 12345
#define WEBENGINEDRIVER_PORT 9515

class DriverServer : public QObject
{
    Q_OBJECT

public:
    DriverServer(QProcessEnvironment processEnvironment = {}, QStringList processArguments = {})
        : m_serverURL(QUrl(
                QLatin1String("http://localhost:%1").arg(QString::number(WEBENGINEDRIVER_PORT))))
    {
        QString driverPath = QLibraryInfo::path(QLibraryInfo::LibraryExecutablesPath)
                + QLatin1String("/webenginedriver");
#if defined(Q_OS_WIN)
        driverPath += QLatin1String(".exe");
#endif
        m_process.setProgram(driverPath);

        if (processArguments.isEmpty())
            processArguments
                    << QLatin1String("--port=%1").arg(QString::number(WEBENGINEDRIVER_PORT));
        m_process.setArguments(processArguments);

        if (!processEnvironment.isEmpty())
            m_process.setProcessEnvironment(processEnvironment);

        connect(&m_process, &QProcess::errorOccurred, [this](QProcess::ProcessError error) {
            qWarning() << "WebEngineDriver error occurred:" << error;
            dumpConsoleMessages();
        });

        connect(&m_process, &QProcess::readyReadStandardError, [this]() {
            QProcess::ProcessChannel tmp = m_process.readChannel();
            m_process.setReadChannel(QProcess::StandardError);
            while (m_process.canReadLine()) {
                QString line = QString::fromUtf8(m_process.readLine());
                if (line.endsWith(QLatin1Char('\n')))
                    line.truncate(line.size() - 1);
                m_stderr << line;
            }
            m_process.setReadChannel(tmp);
        });

        connect(&m_process, &QProcess::readyReadStandardOutput, [this]() {
            QProcess::ProcessChannel tmp = m_process.readChannel();
            m_process.setReadChannel(QProcess::StandardOutput);
            while (m_process.canReadLine()) {
                QString line = QString::fromUtf8(m_process.readLine());
                if (line.endsWith(QLatin1Char('\n')))
                    line.truncate(line.size() - 1);
                m_stdout << line;
            }
            m_process.setReadChannel(tmp);
        });
    }

    ~DriverServer()
    {
        if (m_process.state() != QProcess::Running)
            return;

        if (!m_sessionId.isEmpty())
            deleteSession();

        shutdown();
    }

    bool start()
    {
        if (!QFileInfo::exists(m_process.program())) {
            qWarning() << "WebEngineDriver executable not found:" << m_process.program();
            return false;
        }

        connect(&m_process, &QProcess::finished,
                [this](int exitCode, QProcess::ExitStatus exitStatus) {
                    qWarning().nospace()
                            << "WebEngineDriver exited unexpectedly (exitCode: " << exitCode
                            << " exitStatus: " << exitStatus << "):";
                    dumpConsoleMessages();
                });

        m_process.start();

        bool started = m_process.waitForStarted();
        if (!started) {
            qWarning() << "Failed to start WebEngineDriver:" << m_process.errorString();
            return false;
        }

        bool ready = QTest::qWaitFor(
                [this]() {
                    if (m_process.state() != QProcess::Running)
                        return true;

                    for (QString line : m_stdout) {
                        if (line.contains(
                                    QLatin1String("WebEngineDriver was started successfully.")))
                            return true;
                    }

                    return false;
                },
                10000);

        if (ready && m_process.state() != QProcess::Running) {
            // Warning is already reported by handler of QProcess::finished() signal.
            return false;
        }

        if (!ready) {
            if (m_stdout.empty())
                qWarning("Starting WebEngineDriver timed out.");
            else
                qWarning("Something went wrong while starting WebEngineDriver:");

            dumpConsoleMessages();
            return false;
        }

        return true;
    }

    bool shutdown()
    {
        // Do not warn about unexpected exit.
        disconnect(&m_process, &QProcess::finished, nullptr, nullptr);

        bool sent = sendCommand(QLatin1String("/shutdown"));

        bool finished = (m_process.state() == QProcess::NotRunning) || m_process.waitForFinished();
        if (!finished || !sent)
            qWarning() << "Failed to properly shutdown WebEngineDriver:" << m_process.errorString();

        return finished;
    }

    bool sendCommand(QString command, const QJsonDocument &params = {},
                     QJsonDocument *result = nullptr)
    {
        if (command.contains(QLatin1String(":sessionId"))) {
            if (m_sessionId.isEmpty()) {
                qWarning("Unable to execute session command without session.");
                return false;
            }

            QStringList commandList = command.split(QLatin1Char('/'));
            for (int i = 0; i < commandList.size(); ++i) {
                if (commandList[i] == QLatin1String(":sessionId")) {
                    commandList[i] = m_sessionId;
                    break;
                }
            }

            command = commandList.join(QLatin1Char('/'));
        }

        QNetworkReply::NetworkError replyError = QNetworkReply::NoError;
        QString replyString;

        connect(
                &m_qnam, &QNetworkAccessManager::finished, this,
                [&replyError, &replyString](QNetworkReply *reply) {
                    replyError = reply->error();
                    replyString = QString::fromUtf8(reply->readAll());
                },
                static_cast<Qt::ConnectionType>(Qt::SingleShotConnection));

        QNetworkRequest request;
        QUrl requestURL = m_serverURL;

        requestURL.setPath(command);
        request.setUrl(requestURL);

        if (params.isEmpty()) {
            m_qnam.get(request);
        } else {
            request.setHeader(QNetworkRequest::ContentTypeHeader,
                              QVariant::fromValue(QStringLiteral("application/json")));
            m_qnam.post(request, params.toJson(QJsonDocument::Compact));
        }

        bool ready = QTest::qWaitFor(
                [&replyError, &replyString]() {
                    return replyError != QNetworkReply::NoError || !replyString.isEmpty();
                },
                10000);

        if (!ready) {
            qWarning() << "Command" << command << "timed out.";
            dumpConsoleMessages();
            return false;
        }

        if (replyError != QNetworkReply::NoError) {
            qWarning() << "Network error:" << replyError;
            if (!replyString.isEmpty()) {
                QJsonDocument errorReply = QJsonDocument::fromJson(replyString.toLatin1());
                if (!errorReply.isNull()) {
                    QString error = errorReply["value"]["error"].toString();
                    QString message = errorReply["value"]["message"].toString();
                    if (!error.isEmpty() || message.isEmpty()) {
                        qWarning() << "error:" << error;
                        qWarning() << "message:" << message;
                        return false;
                    }
                }

                qWarning() << replyString;
                return false;
            }

            dumpConsoleMessages();
            return false;
        }

        if (result) {
            if (replyString.isEmpty()) {
                qWarning("Network reply is empty.");
                return false;
            }

            QJsonParseError jsonError;
            *result = QJsonDocument::fromJson(replyString.toLatin1(), &jsonError);

            if (jsonError.error != QJsonParseError::NoError) {
                qWarning() << "Unable to parse reply:" << jsonError.errorString();
                return false;
            }
        }

        return true;
    }

    bool createSession(QJsonObject chromeOptions = {}, QString *sessionId = nullptr)
    {
        if (!m_sessionId.isEmpty()) {
            qWarning("A session already exists.");
            return false;
        }

        QJsonObject root;

        if (chromeOptions.isEmpty()) {
            // Connect to the test by default.
            chromeOptions.insert(
                    QLatin1String("debuggerAddress"),
                    QLatin1String("localhost:%1").arg(QString::number(REMOTE_DEBUGGING_PORT)));
            chromeOptions.insert(QLatin1String("w3c"), true);
        }

        QJsonObject alwaysMatch;
        alwaysMatch.insert(QLatin1String("goog:chromeOptions"), chromeOptions);

        QJsonObject seOptions;
        seOptions.insert(QLatin1String("loggingPrefs"), QJsonObject());
        alwaysMatch.insert(QLatin1String("se:options"), seOptions);

        QJsonObject capabilities;
        capabilities.insert(QLatin1String("alwaysMatch"), alwaysMatch);
        root.insert(QLatin1String("capabilities"), capabilities);

        QJsonDocument params;
        params.setObject(root);

        QJsonDocument sessionReply;
        bool sent = sendCommand(QLatin1String("/session"), params, &sessionReply);
        if (sent) {
            m_sessionId = sessionReply["value"]["sessionId"].toString();
            if (sessionId)
                *sessionId = m_sessionId;
        }

        return sent;
    }

    bool deleteSession()
    {
        if (m_sessionId.isEmpty()) {
            qWarning("There is no active session.");
            return false;
        }

        QNetworkReply::NetworkError replyError = QNetworkReply::NoError;
        QString replyString;

        connect(
                &m_qnam, &QNetworkAccessManager::finished, this,
                [&replyError, &replyString](QNetworkReply *reply) {
                    replyError = reply->error();
                    replyString = QString::fromUtf8(reply->readAll());
                },
                static_cast<Qt::ConnectionType>(Qt::SingleShotConnection));

        QNetworkRequest request;
        QUrl requestURL = m_serverURL;
        requestURL.setPath(QLatin1String("/session/%1").arg(m_sessionId));
        request.setUrl(requestURL);
        m_qnam.deleteResource(request);

        bool ready = QTest::qWaitFor(
                [&replyError, &replyString]() {
                    return replyError != QNetworkReply::NoError || !replyString.isEmpty();
                },
                10000);

        if (!ready) {
            qWarning("Deleting session timed out.");
            return false;
        }

        if (replyError != QNetworkReply::NoError) {
            qWarning() << "Network error:" << replyError;
            return false;
        }

        return true;
    }

    QStringList stderrLines() const { return m_stderr; }

    bool waitForMessageOnStderr(const QRegularExpression &re, QString *message = nullptr) const
    {
        return QTest::qWaitFor(
                [this, &re, message]() {
                    for (QString line : m_stderr) {
                        if (line.contains(re)) {
                            if (message)
                                *message = line;
                            return true;
                        }
                    }

                    return false;
                },
                10000);
    }

private:
    void dumpConsoleMessages()
    {
        auto dumpLines = [](QStringList *lines) {
            if (lines->empty())
                return;

            for (QString line : *lines)
                qWarning() << qPrintable(line);

            lines->clear();
        };

        dumpLines(&m_stdout);
        dumpLines(&m_stderr);
    }

    QProcess m_process;
    QStringList m_stdout;
    QStringList m_stderr;

    QUrl m_serverURL;
    QString m_sessionId;

    QNetworkAccessManager m_qnam;
};

class tst_WebEngineDriver : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void status();
    void startBrowser();
    void navigate();
    void typeElement();
    void executeScript();
    void screenshot();
};

void tst_WebEngineDriver::status()
{
    DriverServer driverServer;
    QVERIFY(driverServer.start());

    QJsonDocument statusReply;
    QVERIFY(driverServer.sendCommand(QLatin1String("/status"), {}, &statusReply));

    QString versionString = statusReply["value"]["build"]["version"].toString();
    QCOMPARE(qWebEngineChromiumVersion(), versionString.split(QLatin1Char(' '))[0]);

    bool ready = statusReply["value"]["ready"].toBool();
    QVERIFY(ready);
}

void tst_WebEngineDriver::startBrowser()
{
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert(QLatin1String("QTWEBENGINE_REMOTE_DEBUGGING"),
               QString::number(REMOTE_DEBUGGING_PORT));

    QStringList args;
    args << QLatin1String("--port=%1").arg(QString::number(WEBENGINEDRIVER_PORT));
    args << QLatin1String("--log-level=ALL");

    DriverServer driverServer(env, args);
    QVERIFY(driverServer.start());

    QString testBrowserPath =
            QCoreApplication::applicationDirPath() + QLatin1String("/testbrowser");
#if defined(Q_OS_WIN)
    testBrowserPath += QLatin1String(".exe");
#endif

    QJsonArray chromeArgs;
    chromeArgs.append(QLatin1String("enable-logging=stderr"));
    chromeArgs.append(QLatin1String("v=1"));
    // To force graceful shutdown.
    chromeArgs.append(QLatin1String("log-net-log"));

    QJsonObject chromeOptions;
    chromeOptions.insert(QLatin1String("binary"), testBrowserPath);
    chromeOptions.insert(QLatin1String("args"), chromeArgs);
    chromeOptions.insert(QLatin1String("w3c"), true);
    QString sessionId;
    QVERIFY(driverServer.createSession(chromeOptions, &sessionId));

    // Test if the browser is started.
    QVERIFY(driverServer.waitForMessageOnStderr(QRegularExpression(
            QLatin1String("^DevTools listening on ws://127.0.0.1:%1/devtools/browser/")
                    .arg(QString::number(REMOTE_DEBUGGING_PORT)))));
    QVERIFY(driverServer.waitForMessageOnStderr(QRegularExpression(
            QLatin1String("^Remote debugging server started successfully. "
                          "Try pointing a Chromium-based browser to http://127.0.0.1:%1")
                    .arg(QString::number(REMOTE_DEBUGGING_PORT)))));

    // Check custom chromeArgs via logging.
    QVERIFY(driverServer.waitForMessageOnStderr(QRegularExpression(QLatin1String("VERBOSE1"))));

    QJsonDocument statusReply;
    QVERIFY(driverServer.sendCommand(QLatin1String("/status"), {}, &statusReply));
    bool ready = statusReply["value"]["ready"].toBool();
    QVERIFY(ready);

    QVERIFY(driverServer.deleteSession());

    // Test if the browser is stopped.
    QVERIFY(driverServer.waitForMessageOnStderr(
            QRegularExpression(QLatin1String("Test browser is about to quit."))));
    QVERIFY(driverServer.waitForMessageOnStderr(
            QRegularExpression(QLatin1String("\\[%1\\] RESPONSE Quit").arg(sessionId))));
}

void tst_WebEngineDriver::navigate()
{
    DriverServer driverServer;
    QVERIFY(driverServer.start());

    QWebEnginePage page;
    QSignalSpy loadSpy(&page, &QWebEnginePage::loadFinished);

    page.load(QUrl(QLatin1String("about:blank")));
    QTRY_COMPARE(loadSpy.size(), 1);

    QVERIFY(driverServer.createSession());

    QUrl url = QUrl(QLatin1String("qrc:///resources/input.html"));
    QString paramsString = QLatin1String("{\"url\": \"%1\"}").arg(url.toString());
    QJsonDocument params = QJsonDocument::fromJson(paramsString.toUtf8());
    QVERIFY(driverServer.sendCommand(QLatin1String("/session/:sessionId/url"), params));
    QTRY_COMPARE(loadSpy.size(), 2);
    QVERIFY(loadSpy.at(1).at(0).toBool());
    QCOMPARE(url, page.url());

    QVERIFY(driverServer.deleteSession());
}

void tst_WebEngineDriver::typeElement()
{
    DriverServer driverServer;
    QVERIFY(driverServer.start());

    QWebEngineView view;
    view.resize(300, 100);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    QUrl url = QUrl(QLatin1String("qrc:///resources/input.html"));
    QSignalSpy loadSpy(view.page(), &QWebEnginePage::loadFinished);
    view.load(url);
    QTRY_COMPARE(loadSpy.size(), 1);

    QVERIFY(driverServer.createSession());

    // Find <input id="text_input"> element and extract its id from the reply.
    QString textInputId;
    {
        QString paramsString = QLatin1String(
                "{\"using\": \"css selector\", \"value\": \"[id=\\\"text_input\\\"]\"}");
        QJsonDocument params = QJsonDocument::fromJson(paramsString.toUtf8());
        QJsonDocument elementReply;
        QVERIFY(driverServer.sendCommand(QLatin1String("/session/:sessionId/element"), params,
                                         &elementReply));

        QVERIFY(!elementReply.isEmpty());
        QJsonObject value = elementReply["value"].toObject();
        QVERIFY(!value.isEmpty());
        QStringList keys = value.keys();
        QCOMPARE(keys.size(), 1);
        textInputId = value[keys[0]].toString();
        QVERIFY(!textInputId.isEmpty());
    }

    // Type text into the input field.
    QString inputText = QLatin1String("WebEngineDriver");
    {
        QString command = QLatin1String("/session/:sessionId/element/%1/value").arg(textInputId);

        QJsonObject root;
        root.insert(QLatin1String("text"), inputText);
        QJsonArray value;
        for (QChar ch : inputText) {
            value.append(QJsonValue(ch));
        }
        root.insert(QLatin1String("value"), value);
        root.insert(QLatin1String("id"), textInputId);
        QJsonDocument params;
        params.setObject(root);

        QVERIFY(driverServer.sendCommand(command, params));

        QTRY_COMPARE(
                evaluateJavaScriptSync(view.page(), "document.getElementById('text_input').value")
                        .toString(),
                inputText);
    }

    QVERIFY(driverServer.deleteSession());
}

void tst_WebEngineDriver::executeScript()
{
    DriverServer driverServer;
    QVERIFY(driverServer.start());

    QUrl url = QUrl(QLatin1String("qrc:///resources/input.html"));
    QWebEnginePage page;
    QSignalSpy loadSpy(&page, &QWebEnginePage::loadFinished);

    page.load(url);
    QTRY_COMPARE(loadSpy.size(), 1);
    QCOMPARE(page.title(), url.toString());
    QSignalSpy titleSpy(&page, &QWebEnginePage::titleChanged);

    QString newTitle = QLatin1String("WebEngineDriver Test Page");
    QString script = QLatin1String("document.title = '%1';"
                                   "return document.title;")
                             .arg(newTitle);

    QVERIFY(driverServer.createSession());
    QString paramsString = QLatin1String("{\"script\": \"%1\", \"args\": []}").arg(script);
    QJsonDocument params = QJsonDocument::fromJson(paramsString.toUtf8());
    QJsonDocument executeReply;
    QVERIFY(driverServer.sendCommand(QLatin1String("/session/:sessionId/execute/sync"), params,
                                     &executeReply));

    QTRY_COMPARE(titleSpy.size(), 1);
    QCOMPARE(executeReply["value"].toString(), newTitle);
    QCOMPARE(page.title(), newTitle);

    QVERIFY(driverServer.deleteSession());
}

void tst_WebEngineDriver::screenshot()
{
    DriverServer driverServer;
    QVERIFY(driverServer.start());

    QWebEngineView view;
    view.resize(300, 100);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    QSignalSpy loadSpy(view.page(), &QWebEnginePage::loadFinished);

    view.setHtml(QLatin1String("<html><head><style>"
                               "html {background-color:red;}"
                               "</style></head><body></body></html>"));
    QTRY_COMPARE(loadSpy.size(), 1);

    QVERIFY(driverServer.createSession());
    QJsonDocument screenshotReply;
    QVERIFY(driverServer.sendCommand(QLatin1String("/session/:sessionId/screenshot"), {},
                                     &screenshotReply));

    QByteArray base64 = screenshotReply["value"].toString().toLocal8Bit();
    QVERIFY(!base64.isEmpty());
    QImage screenshot;
    screenshot.loadFromData(QByteArray::fromBase64(base64));
    QVERIFY(!screenshot.isNull());
    QCOMPARE(screenshot.pixel(screenshot.width() / 2, screenshot.height() / 2), 0xFFFF0000);

    QVERIFY(driverServer.deleteSession());
}

#define STRINGIFY_LITERAL(x) #x
#define STRINGIFY_EXPANDED(x) STRINGIFY_LITERAL(x)
static QByteArrayList params = QByteArrayList()
        << "--remote-debugging-port=" STRINGIFY_EXPANDED(REMOTE_DEBUGGING_PORT);
W_QTEST_MAIN(tst_WebEngineDriver, params)

#include "tst_webenginedriver.moc"