Squish - 根据类型和属性在基于QT的GUI的所有菜单中搜索所有对象

时间:2017-08-22 15:27:24

标签: squish

我对Squish比较新,我正在测试用Qt5.9制作的GUI。

目前我想用固定值(尺寸12)检查应用程序中使用的所有字体的大小。

我已经记录了测试文件,并尝试从菜单中转到每个页面并选择匹配"类型的对象:QLabel"和"属性:pointSize"。但这是一个非常缓慢的过程,我需要在每个页面中选择200个对象。我从菜单中浏览了3页,我的代码已经超过了500行。以下是代码的一部分:

def main():
startApplication("ke3600")
test.compare(waitForObjectExists(":_QLabel").font.bold, False)
test.compare(waitForObjectExists(":_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":_MainWindow").font.bold, False)
test.compare(waitForObjectExists(":_MainWindow").font.pointSize, 12)
test.compare(waitForObjectExists(":Widget_QWidget").font.bold, False)
test.compare(waitForObjectExists(":Widget_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_voltage_LineVoltageWidget").font.bold, False)
test.compare(waitForObjectExists(":m_voltage_LineVoltageWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_dslSyncTime_QLabel").font.bold, False)
test.compare(waitForObjectExists(":m_dslSyncTime_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":m_clock_QLabel").font.bold, False)
test.compare(waitForObjectExists(":m_clock_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget_QStackedWidget").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget_QStackedWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.flashPage_QWidget").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.flashPage_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.moduleframe_QFrame").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.moduleframe_QFrame").font.pointSize, 12)
test.compare(waitForObjectExists(":moduleframe.label_QLabel").font.bold, False)
test.compare(waitForObjectExists(":moduleframe.label_QLabel").font.pointSize, 16)
test.compare(waitForObjectExists(":moduleframe.moduleLabel_QLabel").font.bold, False)
test.compare(waitForObjectExists(":moduleframe.moduleLabel_QLabel").font.pointSize, 16)
test.compare(waitForObjectExists(":moduleframe.passLabel_QLabel").font.bold, False)
test.compare(waitForObjectExists(":moduleframe.passLabel_QLabel").font.pointSize, 16)
test.compare(waitForObjectExists(":m_stackedWidget.errorLabel_QLabel").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.errorLabel_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.flashProgressBar_QProgressBar").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.flashProgressBar_QProgressBar").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.applPage_QWidget").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.applPage_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.menuPage_QWidget").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.menuPage_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.m_menuName_QLabel").font.bold, True)
test.compare(waitForObjectExists(":m_stackedWidget.m_menuName_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.subTitle_QLabel").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.subTitle_QLabel").font.pointSize, 12)
test.compare(waitForObjectExists(":m_stackedWidget.listView_MenuView").font.bold, False)
test.compare(waitForObjectExists(":m_stackedWidget.listView_MenuView").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.qt_scrollarea_hcontainer_QWidget").font.bold, False)
test.compare(waitForObjectExists(":listView.qt_scrollarea_hcontainer_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":listView_QScrollBar").font.bold, False)
test.compare(waitForObjectExists(":listView_QScrollBar").font.italic, False)
test.compare(waitForObjectExists(":listView.qt_scrollarea_vcontainer_QWidget").font.bold, False)
test.compare(waitForObjectExists(":listView.qt_scrollarea_vcontainer_QWidget").font.pointSize, 12)
test.compare(waitForObjectExists(":listView_QScrollBar_2").font.bold, False)
test.compare(waitForObjectExists(":listView_QScrollBar_2").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.Broadband_QModelIndex").font.bold, False)
test.compare(waitForObjectExists(":listView.Broadband_QModelIndex").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.Copper Test_QModelIndex").font.bold, False)
test.compare(waitForObjectExists(":listView.Copper Test_QModelIndex").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.Analysis_QModelIndex").font.bold, False)
test.compare(waitForObjectExists(":listView.Analysis_QModelIndex").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.Setup_QModelIndex").font.bold, False)
test.compare(waitForObjectExists(":listView.Setup_QModelIndex").font.pointSize, 12)
test.compare(waitForObjectExists(":listView.Address Book_QModelIndex").font.bold, False)
test.compare(waitForObjectExists(":listView.Address Book_QModelIndex").font.pointSize, 12)
test.compare(waitForObjectExists(":Battery_Battery").font.bold, False)
test.compare(waitForObjectExists(":Battery_Battery").font.pointSize, 12)

这个过程有效,但我的最终代码将超过20k行,如果我浏览菜单中的每个页面并选择属于我所需类别的所有对象,将花费我很多年。

更新:

好的,所以我能够通过他们的"类型"来搜索对象。只有我可以使用以下代码将其字体大小与默认值进行比较:

o = findObject("{name~='' type='QLabel'}")
test.compare(str(o.font.pointSize), "12") 

现在我的问题是,

如何检查应用程序的所有菜单的所有页面,而无需访问每个页面并手动运行此代码?像树遍历的东西?例如,这将自动转到菜单的每个树并搜索对象,如果它在那里,它将比较字体大小?

1 个答案:

答案 0 :(得分:0)

阅读“用户指南”后,我找到了解决此问题的方法。如果有人仍然感兴趣:

def main():
    startApplication("ke3600")


    objects = find_all_objects("{type='QLabel'}")

    # Iterate over all the found objects:
    for obj in objects:
        test.log(str(obj.font.pointSize))
        test.log(str(obj.objectName))
        pass

def find_all_objects(obj_real_name):
    if "occurrence=" in obj_real_name:
        test.warning("Real name must not contain the occurrence property on the actual object (but it is okay for containers): %s" % obj_real_name)

    objects = []
    try:
        one_object = findObject(obj_real_name)
        objects.append(one_object)

        # Occurrence of second instance for Java, MFC, UIA:
        occurrence = 1

        # Occurrence of second instance for Qt, Web:
        if hasattr(one_object, "metaObject") or hasattr(one_object, "nextSibling"):
            occurrence = 2

        while True:
            backup = testSettings.objectNotFoundDebugging
            testSettings.objectNotFoundDebugging = False
            try:
                obj_real_name_n = "{occurrence='%s' %s}" % (occurrence, obj_real_name[1:-1])
                one_object = findObject(obj_real_name_n)
                objects.append(one_object)
                occurrence += 1
            finally:
                testSettings.objectNotFoundDebugging = backup
    except LookupError:
        # No more occurrences found
        pass
    return objects

此代码将查找type ='QLabel'的所有对象,并打印其字体大小和对象名称。