robotframework - 为什么 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 测试了代码。
解决方案
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
或其他类似的关键字。
推荐阅读
- php - 找不到 PHP Doctrine ORM 类
- python - Matplotlib图反转排序系列的Y轴?
- java - 如何使用 Jsoup 监听 WebView 中的元素变化?
- python - 内存友好的应用生成器而不是熊猫我可以读写同一个文件吗?
- algorithm - 删除异或链接列表中的节点
- flutter - 如何直接执行 login() 方法而不是覆盖 build 方法并在 AccountKit 上返回一个小部件
- tensorflow - 在 Google Colab 中使用 pickle.dump 时会话因未知原因而崩溃
- flutter - Flutter Bloc:更新后未调用 BlocBuilder,ListView 仍显示旧数据
- ruby-on-rails - 命令“rails generate model xxx”的问题
- android - 基于现有布局以编程方式添加按钮