如何在QT QTest设置中截取屏幕截图

时间:2018-04-10 18:56:21

标签: c++ ios qt testing

我试图在iOS模拟器上运行应用程序的某些测试中拍摄屏幕截图。

该应用程序看起来像这样:

的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);
}

我已经继承了这个代码库,而且我对QT不是很熟悉,但我尝试在上面的代码段中只包含相关内容。

我希望能够做的是在测试的某些时候对页面的内容进行一些截图。

当我定义了TEST时,我对应用程序的测试会自行完成,但是当窗口&gt; grabWindow()行被{{1中的断言w中的ASSERT错误命中时,它们会爆炸在qt库中。这是失败的断言(https://code.woboq.org/qt5/qtdeclarative/src/quick/scenegraph/qsgthreadedrenderloop.cpp.html#1281

0 个答案:

没有答案