Robot Framework

How to handle text box in Robot Framework 

Textbox in robot framework

In Testing, it is essential to understand how to interact with the browser and locate the DOM elements. It is easy to perform actions in the input field with Robot Framework. In this blog, we will learn how to handle text boxes using the selenium library. 

To work with the input field – textbox, we need the locator, which is the main unique identifier for that textbox, and it can be id, name, class, etc. 

In this blog, we will discuss- 

  • Project setup for text box 
  • Enter data in the text box 
  • Clear data from the text box 
  • Click on the login button 

Project setup for textbox:

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

After this, we can check our libraries by pip list command, or we can check manually also 

File > settings > project: robot-automation > python interpreter 

Project setup

After verifying the prerequisite part create a file Textbox.robot in the TestCases directory. 

— go to the new file > give a name of with .robot >Enter 

Project file
New file

Keywords: 

  • Input Text − This keyword works on the input box and will look for the locator id. 
  • clear element text – clear the value of the text input element. 
  • click element – click the element identified by the locator. 

Now let us write a test case for better understanding. 

TestCase:

Step1: Go to https://opensource-demo.orangehrmlive.com/

Step2: Collect the specific locator for the text box from DOM 

Step3: input text on the user name 

Step4: clear the text 

Step5: click on login. 

*** Settings ***
Library  SeleniumLibrary
*** Variables ***
${browser}  chrome
${url}  https://opensource-demo.orangehrmlive.com/
*** Test Cases ***
LoginTest
 
    open browser        ${url}      ${browser}
    maximize browser window
    page should contain textfield   cd
    click element     xpath://input[@id='txtUsername']
    input text     id:txtUsername       Admin
    clear element text  id:txtUsername
    click element       xpath://input[@id='txtPassword']
    input text      id:txtPassword      admin123
    click element       xpath://input[@id='btnLogin']
    close browser
*** Keywords ***

***Settings*** – To work with robot framework keywords we need to import the selenium library because all these keywords are built in the selenium library- 

Library SeleniumLibrary 

Same as this we can import other libraries here according to the requirement of the project, and the resources file 

***Variables***– Here we declared variables of browser and URL so we can reuse these variables.

${url} ${browser}

In the same way, we can create more variables like this according to the requirement of the project. 

***Test Cases***- 

Textbox in robot framework code

Code explanation: 

The first step in this test is to open the URL https://opensource-demo.orangehrmlive.com/ in Chrome for this we already created variables. Then we gave a name to our test Testing Radio Buttons. Now Open Browser − This keyword opens the specific browser for the given URL and maximize browser windowmaximizes the current browser window. To work with the text box, we must find the locator. It is an identifier for textbox like id, name, and class here we have id as a specific locator. To provide text in the username field we have a keyword Input Text. This keyword works on the input box and will look for the locator id. 

After writing Admin text there we can clear this also with clear element text keyword it clears the value of text input element. Then click element keyword click the element identified by the locator.  

After executing all keywords for the input box now we can go for close browser to close browser and end the test. 

Execution: 

To execute the test case we need to use the command on the terminal: 

Execution command

After execution, we should get the result as a pass. 

Pass result

Report:

Textbox in robot framework report
Textbox in robot framework log

Conclusion:

In this Blog, we learned how to interact with the textbox using the Selenium library in the Robot Framework. With the keywords provided by the robot framework and the library imported, we can locate the textbox, enter data, and test it. 

Read our next blog on “How to handle radio buttons in Robot Framework”.