Transcripted Summary



Chapter 4, keywords. Where do keywords come from?

In this chapter, we're going to learn a few things.

  • The 2 places keywords can come from.
  • We're going to learn what a keyword is.
  • We're going to learn how to identify the correct syntax for a keyword.
  • We're going to use a keyword in a test script.
  • We’ll find keywords in keyword documents.
  • You'll also create your own keyword.

There are 2 main ways to use keywords in your script — one is by a library; the other is by creating it yourself.

We're going to talk about using those created in libraries first.

# Keywords Created in Libraries

Go with me into chapter 004-Keywords. In this directory you'll find a file called example-settings.robot.

In each of the exercises I'm going to have a challenge for you, and that challenge is going to be set up with a file in the exercise.

The robot file in this case is example-settings.robot. The answer, in case you want to cheat, is in the example-settings.answer.robot file. But I know you guys don't want to cheat.

So, what is a keyword?

A keyword is a piece of functionality in a test case. We talked about test cases briefly earlier.

# Example Code - example-settings.robot


*** Settings ***
#Library  #?

*** Test Cases ***
Create an Invoice
    Comment    This is my first RobotFramework test case!
    Open Browser    http://inv.beaufortfairmont.com:8082/    chrome

We know that we've got a Settings section.

It's denoted by having 3 asterisks, a space, the word "Settings", space, and then 3 asterisks again. It's left justified on the 0th column.

We notice we have a Test Cases section in this file as well.

A Robot Framework test file is really a test suite. It contains 1 or more tests.

The Test Cases section holds our test cases.

The name of a test case is defined at the top of a test case, and it's left justified.

Spacing in Robot Framework is crucial. Make sure you understand spacing within Robot Framework.

NOTE

If you need a cheat sheet for this, and other pieces of Robot Framework make sure to look it up on the beaufortfairmont.com website. We've got a reference sheet for you there.

Test cases, once again, are named and defined by being left justified.

This test case is called "Create An Invoice".

There are only 2 steps to this test case:

  • We make a comment, and the comment is, "This is my first RobotFramework test case!"
  • And then, we open a browser, we point it to a URL, and we tell it to use the Chrome browser.

The keywords here are Comment and Open Browser.

How did I know that? Well, in general keywords will start a line.

Keywords are always defined by having 2 or more spaces between the keyword (in this case Comment) and an argument (in this case the String, "This is my first RobotFramework test case!").

An argument is a piece of information we give a keyword to tell it to do something special.

In this case, we're going to make a comment when the test case runs saying, "This is my first RobotFramework test case!"

The second line here uses a different keyword called Open Browser.

Open Browser is going to open the browser. The arguments are the URL, and the type of browser to use.

Where did these come from? Well, that's the challenge I want you to figure out in this test case. So, I'm not going to give you the answer right here.

Instead, what I'd like to do is look at another example of keywords and help you understand where keywords come from.

Take a look at the 007-BasicsOfKeywords directory and open up the file called extra-large-test.robot.

Example Code - extra-large-test.robot


*** Settings ***
Library  SeleniumLibrary
Library  RequestsLibrary
Library  String
Library  Collections

Suite Setup  Run Keywords   
Suite Teardown  Run Keywords    Close Browser  

*** Test Cases ***
Create an Invoice
    Open Browser    http://inv.beaufortfairmont.com		chrome
    ${invoiceId}=    Generate Random String    10    [LETTERS]
    Set Suite Variable   ${invoiceId}
    Set Selenium Speed    0.5 Seconds
    Click Link    Add Invoice
    Page Should Contain Element     invoiceNo_add
    Input Text  invoice   ${invoiceId}

    Fill in invoice information

    Click Button    createButton
    Create Session	invoice-manager     http://inv.beaufortfairmont.com:8082
    ${resp}=  Get On Session    invoice-manager    /invoices/${invoiceId}
    Should Be Equal As Strings  ${resp.status_code}	 200
    Dictionary Should Contain Value	 ${resp.json()}	 ${invoiceId}
    ${resp}=  Delete On Session    invoice-manager    /invoices/${invoiceId}
    Should Be Equal As Strings	${resp.status_code}	 200
    ${resp}=  Get On Session    invoice-manager    /invoices/${invoiceId}    expected_status=any
    Should Be Equal As Strings  ${resp.status_code}  404

*** Keywords ***
Fill in invoice information
    Input Text  company   my example company
    Input Text  type   plumbing
    Input Text  price   34.00
    Input Text  dueDate   2018-10-31
    Input Text  comment   Unclogged Drain
    Select From List By Value   selectStatus    Past Due

This is a really long test case; it does a lot of steps.

The steps, in this case, almost all come from libraries. In fact, most of them come from the SeleniumLibrary.



If you're familiar with Selenium, these keywords probably look familiar.

  • Open Browser, we saw earlier.
  • Click Link sounds very familiar to Selenium users.
  • Page Should Contain Element we're looking for a specific HTML element on the page.
  • Click Link and Input Text are also SeleniumLibrary keywords to help us navigate an HTML webpage.

But where are we getting the functionality for these keywords?

SeleniumLibrary Keywords

I mentioned the Settings section. In it, we can use a library to pull in keywords to make them available to us to use. A number of libraries have already been created for you for Robot Framework. You can use them with very little work.

We've already installed the SeleniumLibrary on this machine when you did the setup steps. But I want to show you what the SeleniumLibrary documentation looks like, and where you can go to find it.

robotframework.org/seleniumlibrary is where you can find the documentation for the SeleniumLibrary.

What you want to do here is look at the keyword documentation.



Luckily, there's a search space here at the top and we can look for specific keywords, like for instance, Open Browser.

The document is in the format that nearly all Robot Framework documents are in. It looks a little bit plain, but there's a lot of utility here. You can find out what you need to know about this library from this document.

You'll notice several things. We have a table of contents, and we have some information that is general about this library and how to use it.

As you scroll down, you'll notice even more helpful material.

Remember, we were looking for Open Browser.

We can search for Open Browser in the search box, pull it up in the keyword section on the left, and when we click on it, we get the Open Browser keyword documentation on the right.

Notice we have the arguments to use for Open Browser and we also have documentation just below there about how to use this keyword and all the specifics of it.

Here we are in Open Browser.



Notice we pass in the URL, which happens to be the address inv.beaufortfairmont.com.

And we also pass in the Chrome browser as an argument.

This is the definition of the keyword. It's the documentation from it, and we know that the keyword comes from the SeleniumLibrary. We now know how to use it.

# Robot Standard Library Keywords

You'll notice a lot of other ones here in our code example.

We see the Generate Random String keyword comes from a different library. It comes from the String library, which we import here on line 4.

You'll notice Set Suite Variable keyword, which comes from a built-in library for Robot Framework. You don't see that in the settings section because BuiltIn comes along with every test. You don't have to use the Library keyword.



Here's the documentation on the BuiltIn library and a few of the keywords.

You'll also notice [OperatingSystem](https://robotframework.org/robotframework/latest/libraries/OperatingSystem.html) which gives you the opportunity to work with the operating system, do file manipulations, work with directories, log files.

You'll notice things like the [Process](https://robotframework.org/robotframework/latest/libraries/Process.html) library.

The [String](https://robotframework.org/robotframework/latest/libraries/String.html) library, which I mentioned earlier. We use "Generate Random String to generate the invoice number for the invoice in this example.

[Collections](https://robotframework.org/robotframework/latest/libraries/Collections.html) is a library that we'll use from to time to manage our data. We have lists, we have dictionaries and things like that to manage data within Robot Framework.

So, keywords are a piece of text that'll allow you to do multiple things in Robot Framework.

Let's go back to 004-Keywords directory and look at the example-settings.robot file.

You can find it by going to the command line and going into the proper directory, but let's also go in and run this test case.

If you remember, you run a test case by typing robot and then the name of the file — robot example-settings.robot.

You should have the same experience I have here, which is a failing test case.



This test case fails because we haven't completed it yet.

It basically says, "No keyword with name open browser found".

That's because we haven't told the script where to find it yet.

If you were listening carefully, you already know how to make Robot Framework find the Open Browser keyword. I taught you one way so far to use keywords. Use that way to make sure that this test case works.

SPOILER ALERT

If you don't want to see the answer, pause here, make the script work, and come back when you're done.

For those of you who have already answered this on your own and are checking on how it works, you'll notice the answer is, we put the "SeleniumLibrary" in the Settings section.


*** Settings ***
Library  SeleniumLibrary

*** Test Cases ***
Create an Invoice
    Comment    This is my first RobotFramework test case!
    Open Browser    http://inv.beaufortfairmont.com/    chrome

That's really all you need.

Let's now run the answer, and you can see it pass. And it goes to the page we expect, and we see a nice green pass in the output. If you got that right, good work.

We want to do one more exercise now or one more challenge while we're here.

Let's go into a new directory. We’re going to go into 007-BasicsOfKeywords and we’re going to look again at extra-large-test.robot.

This is a good way to look at some of the keywords that can be used to see how they're used within a long test case.

What I'd like to do now is teach you another way to create keywords.

So, the first way we used was to use the keyword Library in the settings section to pull in already created libraries with their keywords. Now we're going to do things a bit differently.

# Creating Custom Keywords

We're going to create our own keyword.

We'll create a keyword section to start. Notice I've already got one in this file — *** Keywords ***.

Again, it's asterisk, asterisk, asterisk, space, "Keywords", space, asterisk, asterisk, asterisk.

One thing you could do here is to create a keyword. I'm going to create one called, "My Keyword"

I'm going to tab over 4 spaces underneath and give it something to do. In this case I'm just going to log something to the console. I'm going to use the keyword Log to Console. It's one that I know, one that I've used before. You can look it up in the BuiltIn library if you'd like.

And then I'm going to log the text, "This is a keyword I created".


*** Keywords ***
My Keyword
    Log to Console    This is a keyword I created

You'll notice there are at least 2 spaces between the keyword and the argument, and I can use this keyword that I've created wherever I want.

So, I'm going to use it right here in the beginning.

All I have to do to use it is to tab over or use at least 2 spaces, I've got 4 here on line 12, and then type, "My Keyword."



I'm going to comment out the rest of the test case just for us to be able to run this 1 keyword with no other things going on here to see very quickly what it does.

It should output to the screen, "This is a keyword I created". So, I should see this in my output when I run this test.

Let's see what happens. I run the keyword by running robot extra-large-test.robot.

It ran very quickly. That's a good sign.

I can see "This is a keyword I created" in the output there. That's exactly what Log to Console should do, and our keyword worked.



It says it passed, but as a good test automation engineer, I'm always skeptical.

So, I'm going to go and look into our logs and see if I can see the output there. It should be there.

We know the keyword ran, and we can see right here where the keyword ran.



Down at the very bottom it says, "My Keyword" and it shows us that it wrote the words, "This is a keyword I created".

# Challenge

Now what I'd like you to do is take a look at the README file for the 007-BasicsOfKeywords directory.



In the robot file I'd like you to create a keyword called, "Navigate To Home Page".

And what it is is you're going to look in the keyword-basics.robot file and you're going to create a keyword in here called, "Navigate To Home Page".

# Example Code - keyword-basics.robot


*** Settings ***
Library  SeleniumLibrary

*** Test Cases ***
Check invoice manager page
    Comment    We're learning how to create a custom keyword.
    Open Browser  http://inv.beaufortfairmont.com/   chrome
    Page Should Contain     Invoice Manager

I'll give you a hint; the steps in this are already in the test case.

You're going to replace the steps in the test, probably the ones that I've highlighted, by calling a keyword just like we just did. You'll need to create your own keyword section and define the "Navigate To Home Page" keyword in it.

Now, pause the video and give this exercise a try.

SPOILER ALERT

Here's my answer for how I accomplished what we were looking for.

I created a keyword called "Navigate To Home Page". I put it in a newly created Keywords section. And inside the keyword I called the Open Browser keyword to open the browser. And that's it.

# Example Code - keyword-basics.answer.robot


*** Settings ***
Library  SeleniumLibrary

*** Test Cases ***
Check invoice manager page
    Comment    We've learned how to create a custom keyword!
    Navigate To Home Page
    Page Should Contain     Invoice Manager

*** Keywords ***
Navigate To Home Page
    Open Browser    http://inv.beaufortfairmont.com		chrome

If we look back really quickly at how we started, you'll notice we were already calling Open Browser.

All I did was copy from line 7 into my keyword section, and then I called the keyword or executed it on line 7, where I've typed, "Navigate To Home Page".

Now we're calling our own keyword instead of Open Browser on line 7; and we still call Open Browser inside the keyword we created. I'm just showing you how to create a keyword.

Let me show you that this works.



And there we go.

# Another Learning Example…

Let's take a look at another example down in directory 032-ParametersForKeywords. Let's look at example-arguments.robot.




*** Settings ***

*** Test Cases ***
My Addition Test
  ${sum}=  Add  2  3
  Should be equal as integers  5  ${sum}

My Test Case
  ${nickName}=  Set Variable  Paul
  Print Profile  ${nickName}  Merrill  Trainer  dpaulmerrill
  ${nickName}=  Change Nickname  ${nickName}  Davie
  Print Profile  ${nickName}  Merrill  Trainer  dpaulmerrill

*** Keywords ***
Add
  [Arguments]  ${value1}  ${value2}
  ${value}=  Evaluate  ${value1} + ${value2}
  [Return]  ${value}

Change Nickname 
  [Arguments]  ${original}  ${new}
  [Return]    ${new}

Print Profile
   [Arguments]  ${firstName}  ${lastName}  ${title}  ${twitterHandle}
   Log to Console   \n__Profile__
   Log to Console   First Name: "${firstName}"
   Log to Console   Last Name: "${lastName}"
   Log to Console   Title: "${title}"
   Log to Console   Twitter Handle: "${twitterHandle}" 

You'll notice we have 2 test cases.

One is called, "My Addition Test," the other is called, "My Test Case."

In the "My Test Case" you'll see that we use a keyword called, Print Profile which is defined down in the Keywords section.

All it does is print out a profile of a particular person. We pass in arguments, which are the first name, the last name, and a title, and a Twitter handle.

And it should log to the console, which is our command prompt, with my log keyword (Log to Console).

The second test case just does addition.

We pass in 2 and 3 and we check to make sure that the answer is 5, or the sum. Add is created here inside Keywords as well.

We use the BuiltIn keyword called, Evaluate to add the first value to the second value.

Now let's run these test cases and see how they work.

Remember, I have to change into the proper directory, 032, and I'll run the test using our robot command robot example-keywords.robot.

And there we go.



One of my keywords printed out profiles. It was used within the "My Test Case" test case.

The first time we ran "My Test Case", the name was "Paul Merrill". My title was "Trainer". And my Twitter handle was "@DPaulMerrill".

The second time we called within the first test case, or rather the second time the Print Profile keyword was called within "My Test Case", we printed this out, "Davie", "Merrill", "Trainer", "@DPaulMerrill," and "My Test Case".

The other test case here was addition.

There was no output from it, but we can see that both "My Test Case", as well as "My Addition Test" passed.

Looking back at those, remember "My Test Case" is defined here. The entire test case is the section from line 8 down to line 12.

The other test case was from line 4 down to line 6.

# In Review…

There are 2 main ways to create and use keywords.

  • Keywords can come from a library, which may be supplied to you, in this case, we used SeleniumLibrary and OperatingSystem library.
  • Or you can create them on your own inside the test case using the Keywords section.

A keyword is defined by having 2 or more spaces around it.

A keyword is a piece of functionality that we can create or use to do something special.

Many keyword libraries are already developed for Robot Framework, so make sure to use them as much as possible in lieu of creating your own.

Thanks for joining me for a walkthrough on keywords. I'll see you in the next chapter.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR