0

I'm trying to take a screen shot during some tests where the application is being ran on an iOS simulator.

The app looks something like this:

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

#ifdef TEST
    thingTests = new ThingTests(&engine);
    tabletTests->startTestSuite(argv[1]);

#endif

  ...

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

ThingTests.cpp

TabletTests::TabletTests(QQmlApplicationEngine *engine) : QObject(nullptr), m_testSuiteName(NULL)
{
    m_engine = engine;
}

void TabletTests::startTestSuite(char *testSuiteName)
{
    m_testSuiteName = testSuiteName;
    connect(m_engine, SIGNAL(objectCreated(QObject*,QUrl)), this, SLOT(onObjectCreated(QObject*,QUrl)));
}


void TabletTests::onObjectCreated(QObject *, const QUrl &) {
 // run settings test for now, later on control with command line arguments
    SettingsTest *settingsTest = new SettingsTest(m_engine);
    QTest::qExec(settingsTest);
}

SettingsTest.cpp

void SettingsTest::openWarningModeSelectionTest()
{
    m_settingsTester->performTestOperation(SettingsTester::SettingsTestOperation::WarningModeSelection);
    QTest::qWait(1000);
    bool optionSelectorDisplayed = DialogController::getInstance()->optionSelectorShown();
    // This clip came from here: https://stackoverflow.com/questions/21697185/how-to-take-screenshot-of-qml-application-without-qquickview
    foreach(QObject* obj, this->m_engine->rootObjects()) {
      QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
      if (window) {
        QImage image = window->grabWindow(); //<-- This line throws the assertion error
      }
    }
    QVERIFY(optionSelectorDisplayed);
}

I've inherited this codebase and I'm not very familiar with QT, but I tried to only include relevant things in the above snippets.

What I want to be able to do is take some screenshots on what a page looks like at certain times in the tests.

When I have TEST defined, my tests for the app go and do their thing, but they blowup when the window->grabWindow() line is hit with an ASSERT error in assert w in scenegraph/qsgthreadedrenderloop.cpp in the qt libraries. This is the assert that is failing (https://code.woboq.org/qt5/qtdeclarative/src/quick/scenegraph/qsgthreadedrenderloop.cpp.html#1281)

Andy
  • 44,610
  • 13
  • 70
  • 69
  • If you have one window in your program at the root level just bail out of that `foreach` loop. Also `qDebug() << image` does not make sense. You want to save that image in the file? – Alexander V Apr 10 '18 at 22:08
  • I had just copied that code from some other area and was still flushing out what it was doing. I do want to save it to a file. If I bail out of that foreach, how would that stop the grabWindow() from throwing the error? – Andy Apr 11 '18 at 16:03
  • I removed the qDebug and I tried directly access `rootObjects()[0]` and do a `grabWindow()` on that (not in any for loops) and I still get the assertion error. – Andy Apr 11 '18 at 16:32
  • Now I wonder why the code compiles? Because there is no `QQuickWindow::grabWindow()` call available. http://doc.qt.io/qt-5/qwindow-members.html – Alexander V Apr 11 '18 at 17:56
  • It looks like you linked to qwindow. The function is in qquickwindow. http://doc.qt.io/qt-5/qquickwindow.html#grabWindow – Andy Apr 11 '18 at 18:59
  • It appears that the window was not properly initialized at the time of that call yet. Try to wait until http://doc.qt.io/qt-5/qquickwindow.html#showEvent called. – Alexander V Apr 11 '18 at 19:07
  • Closing the loop. I tried waiting on showEvent too and still no go. We've decided to to just write the tests in native iOS. Thanks for the help. – Andy Apr 18 '18 at 14:15

0 Answers0