首页 > 解决方案 > 为什么 Robot Framework、Selenium2Library、Element Text Should Be 关键字无法正确验证输入元素文本?

问题描述

我在测试基于https://www.freecodecamp.org/learn/responsive-web-design/responsive-web-design-projects/build-a-survey-为自学目的创建的简单调查 html 页面时遇到问题形式。当我尝试检查输入字段是否使用Element text should be关键字填充时,它不断抛出错误The text of element 'id=name' should have been 'Test Name' but it was '',但在测试期间完成的 selenium 屏幕截图显示文本输入正确。

输入正确填写

为了调试一个问题,我注释了大部分代码并尝试只测试一个输入字段。

调查.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Survey Form</title>
</head>
<body>
<h1 id="title">Survey Form</h1>

<p id="description">Thank you for taking the time</p>

<div class="container">
    <form id="survey-form">
        <div>
            <label for="name" id="name-label">Name:</label>
            <input type="text" name="name" id="name" placeholder="Enter your name"/>
        </div>

    </form>
</div>
</body>
</html>

测试是使用 BDD 约定编写的。

调查测试机器人

** Settings **
Library           Selenium2Library
Test Teardown     Close Browser

** Variables **
${SURVEY PAGE}    an/absolute/path/to/survey.html
${BROWSER}        Chrome

** Test Cases **
Check name input
  When Survey Page is opened
  Given User fill the name field with 'Test Name'
  Then Element name contains 'Test Name'


** Keywords **
Survey Page is opened
    Open Browser                       ${SURVEY PAGE}    ${BROWSER}

User fill the name field with '${name}'
    Input Text                         id=name           ${name}

Element name contains '${name}'
    Element Text Should Be             id=name           ${name}

我使用:Python 3.7.5
robotframework-selenium2library 3.0.0
,并在 79、81、83 版本中使用 Chrome 测试了代码。

标签: robotframeworkselenium2library

解决方案


Get Element Text返回节点内的文本数据,例如"<element>the text inside here</element>",而您的目标是一个input元素,显然是输入的值。
该(值)不存储在节点的内容中,而是存储在一个名为“值”的属性中;然后您通过以下方式检索它Get Element Attribute

${value}=    Get Element Attribute    id=name    value

然后你可以断言它是你需要的任何东西,withShould Be Equal As Strings或其他类似的关键字。


推荐阅读