robotframeworkselenium2library

Why Robot Framework, Selenium2Library, Element Text Should Be keyword does not verify input element text properly?


I face an issue with testing a simple survey html page created for self learning purpose based on https://www.freecodecamp.org/learn/responsive-web-design/responsive-web-design-projects/build-a-survey-form. When I try to check if the input field is filled using the Element text should be keyword, it constantly throws an error The text of element 'id=name' should have been 'Test Name' but it was '', but selenium screenshot done during the test shows that the text was entered properly.

Input filled properly

To debug an issue I commented most of the code and try testing only for one input field.

survey.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>

Tests are written with BDD convention.

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}

I use: Python 3.7.5
robotframework-selenium2library 3.0.0
and I tested the code with Chrome in versions 79, 81, 83.


Solution

  • Get Element Text returns the text data inside a node, e.g. "<element>the text inside here</element>", while what you are targeting is an input element, and obviously the entered value.
    That (value) is not stored inside the node's content, but is in an attribute called "value"; and you retrieve it through Get Element Attribute:

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

    Then you can assret it is whatever you need it to be, with Should Be Equal As Strings, or other similar keywords.