Qt框架 在为跨平台应用程序开发用户界面的人中众所周知。 Qt快速, a module for writing fluid and dynamic user interfaces using the QML language, is a part of this framework. One important functional consideration for QA engineers is to check elements containing a text for truncation. When can truncation happen? For example, if the content of a text element exceeds its width and the text element has the elide property set. This 文章 will introduce two approaches to testing text truncation in Qt快速 applications with 压扁:一种用于纯文本的方法,一种用于富文本格式(RTF)的方法。 

测试纯文本截断

Let’s create a QML text element that has a width property set to a number which is less than the width of the actual text. It is plain text, so it may contain the elide property. Let’s use Text.ElideRight. In our case, this truncates the text and adds an ellipsis at the end:

显示当前日期的文本元素的QML代码。
显示日期的文本元素的QML代码。

我们在左上角观察到截断的text元素:

Qt快速窗口显示被截断的文本。
Qt快速应用程序窗口显示被截断的文本。

使用以下Squish脚本很容易检查文本截断:

obj = waitForObjectExists(names.date)
    text = str(obj.text)
    name = objectMap.symbolicName(obj)
    msg = "Verify 'truncated' property: '%s' of %s" % (text, name)
    if obj.truncated:
        test.fail(msg)
    else:
        test.passes(msg)

应用程序窗口左上方的日期被截断,因此我们的测试失败:

测试结果,显示截断检查失败。

测试RTF截断

上述方法适用于非RTF格式的文本。 RTF没有截断的属性,因此要测试RTF的截断,我们应该使用另一种方法。看下面的代码:

def checkTruncation(obj, stripHTML=True):
    text = obj.text
    if stripHTML:
        text.remove(QRegExp("<[^>]*>"))
    text = str(text)
    name = objectMap.symbolicName(obj)
    msg = "Verify truncation: '%s' of %s" % (text, name)
    if len(text) != 0:
        width = builtins.int(obj.width)
        # If it has decimal places, round up:
        if builtins.float(width) != builtins.float(obj.width):
            width += 1
        width_required = requiredWidth(obj, text)
        if width < width_required:
            detail = "Obj. width: %s, but requires: %s" % (width, width_required)
            return test.fail(msg, detail)
    return test.passes(msg)

def requiredWidth(obj, text):
    fm = QFontMetrics(obj.font)
    return fm.boundingRect(QString(text)).width

In requiredWidth() we are obtaining the width that a text element should have in order to show the whole text without truncating it. 的n, in checkTruncation(), we are comparing the width of text shown on the screen to the required width. 的 purpose of stripHTML is to remove HTML tags from the text before comparing the width. 的se tags are not visible on the screen.

在这种情况下,Squish提供以下输出:

包起来

为了提供最佳的UX / UI,重要的是检查Qt Quick中的文本项是否被截断。 压扁可以使用两种方法来完成此操作。第一个是访问适用于纯文本的截断属性,第二个是将所需的文本元素宽度与实际显示的文本宽度进行比较,后者也适用于RTF。

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *

复制链接