gpt4 book ai didi

robotframework - 为什么机器人框架、Selenium2Library、元素文本应该是关键字不能正确验证输入元素文本?

转载 作者:行者123 更新时间:2023-12-04 09:49:34 25 4
gpt4 key购买 nike

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

Input filled properly

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

调查.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 约定编写的。

Survey_tests.robot
** 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
机器人框架-selenium2library 3.0.0
我在 Chrome 79、81、83 版本中测试了代码。

最佳答案

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

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

然后,您可以使用 Should Be Equal As Strings 来确定您需要的任何内容。 ,或其他类似的关键字。

关于robotframework - 为什么机器人框架、Selenium2Library、元素文本应该是关键字不能正确验证输入元素文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62029237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com