Robot Framework - Working With Keywords



In Robot Framework, test cases are constructed in test case tables using keywords. In this chapter, we will cover the details on keywords used in Robot Framework. There are 2 types of keywords used in Robot −

  • Library Keywords
  • User Defined Keywords

Library Keywords

Library Keywords are keywords that come from the library we import in Robot Framework. We will now take a look at the Selenium library, which helps us interact with the browser. We will discuss some of the important keywords associated with selenium library.

Follow the steps shown below to import Selenium library −

The details relating to the installation of Selenium library is discussed in chapter “Working with Browsers using Selenium Library ”. Open ride using ride.py from the command line.

Library Keywords

Click on New Project and give name to your project. The name given to the project is LibraryKeywords.

Right-click on the name of the project created and click on New Test Case

Library New Test Case

Library New Test Case Ex

Give a name to the test case and click OK.

We are done with the project setup. Now, we will write test cases to show the working of library keywords. Since we need Selenium library, we need to import the same in our project.

Click on your project on the left side and click Library.

Library project

Upon clicking Library, a screen will appear where you need to enter the library name −

Library name

Click OK and the library will get displayed in the settings.

Library displayed

The name given has to match with the name of the folder installed in site-packages.

Now will create test case in the project created and use a few important keywords.

Click on your test case created TC1 and in the tabular form enter the keywords to open the browser and enter data inside the form opened.

Here is a simple test case using Library Keywords −

Library simple

To get more details of this keyword, while typing the keyword press ctrl + spacebar. It will show the details of the library keyword entered.

Here is an example for Open Browser, and if any help required for that keyword you can make use of ctrl + spacebar while typing the keyword.

Open Browser Keyword Details

Open Browser Keyword Details

Similarly, we have Library keywords to work with Input, Radio, Text, etc

Library keywords List

Library keywords List Input

We will execute the test case we entered to open the browser with URL − https://www.tutorialspoint.com/ and enter details in the input text.

Library keywords List Input Text

We have executed the test case. You can see the textbox has all the details we gave in the test case.

User-defined Keywords

User-defined keywords can be created to perform a particular action in the test case or it can also be created using the library keywords and built-in keywords in robot framework. We will work on an example and see how we can create keywords for our test case.

We will use the same project that we created above and create user-defined keywords in that and use in the test case.

To create keyword in Ride, right-click on your project and click on New User Keyword as shown below −

User-defined Keywords

Upon clicking New User Keyword, a screen appears as shown below −

New User Keyword screen

Enter the Name of the keyword and click OK. The screen also shows Arguments. We will discuss what arguments have to do with Keywords in a subsequent section.

New User Keyword subsequent

We have given the name BrowserDetails to the keyword. Click OK to save it. The keyword BrowserDetails is created.

New User Keyword Details

To test the URL in the browser, we repeatedly have to enter open browser, maximize browser keywords.

Now, we will create a user-defined keyword that will have open browser and maximize browser details. The keyword created will be used in our test case.

New User Keyword test case.

Our BrowserDetails keyword is a combination of other keywords used repeatedly.

Now, we will use the keyword created in the test case as shown below.

Test case

New User test case.

Considering the above test case, we are going to use the user-defined keyword BrowserDetails.

We will now replace 1 and 2 keywords with the user-defined keyword −

New User test case Ex.

Let us now run the test case to see the output −

Library keywords List Input Text

The execution of the test case works perfectly fine.

Now, we will see the use-case of arguments in keywords.

Here is the keyword that we created −

Library keywords created

The name of the keyword is BrowserDetails. We can use this keyword in other test cases created under the project. The keyword contains the URL of the browser hardcoded. If we want to use the keyword in another test case with a different URL, it will not be possible.

We can use arguments to help us with the hardcoded parameters. We will go back to the keyword created and make use of arguments.

Library keywords created arguments

Click on Edit against the Arguments.

Library keywords against

Enter the argument to be used with the keyword.

Library keywords argument

If there is more than 1 argument, you can separate them using pipe (|). We will now use the argument in the Keyword specified as follows −

Library keywords specified

Go back to your test case. Now, you need to pass the value which is the URL to be used for the test case.

In the test case, when you type the user-defined keyword and press Ctrl + Spacebar, it gives the details of the keyword along with the arguments.

Following are the details for keyword BrowserDetails −

Library keywords Browser

The test case now will have the URL to be passed as argument.

Library keywords  passed

Let us now run the test case to see the output −

Library keywords List Input Text

The Keyword and the arguments passed to the user-defined keyword are working fine.

Let us now change the URL; we will use https://www.google.com/

Library keywords change url

The URL for keyword BrowserDetails is changed to https://www.google.com/

We have changed the argument to Input Text to the id available from google site. To get the id or name or class of the input field, you can inspect and check in the browser.

Let us run the above test case and see the output.

Upon successful execution, the above test case generates the following output −

Library keywords google

Conclusion

In this chapter, we have seen how to get help for built-in keywords. We have also seen how to create user-defined keywords, which can be a combination of library keywords and built-in keywords.

Advertisements