From the course: Robot Framework Test Automation: Level 1 (Selenium)

Overview

So now that we have Robot Framework installed and configured, it's time to write our first script. The agenda for this section is we'll begin with an overview so you can understand the big picture concepts that pertain to writing Robot Framework scripts. I'll talk about organizing your project files. I'll explain some important sections of Robot Framework test and keyword files. Then we'll use the SeleniumLibrary and write a simple test script that opens a browser and interacts with the website. Finally, we'll run that script and examine the amazing Robot Framework results files. When you're creating Robot Framework scripts, you're going to need to use some kind of an IDE. For this course, I'm going to focus on the PyCharm IDE made by jetbrains.com Some other popular IDEs for Robot Framework scripting are Visual Studio Code, Eclipse, and Sublime Text. There is an IDE called RIDE that was explicitly created for Robot Framework scripting. I don't recommend using it though, because it doesn't have the advanced and mature capabilities that IDEs like PyCharm and Visual Studio have. And what I mean by that is things like search and replace, integration with source code repositories, and other enterprise features like that. There's nothing special about Robot Framework syntax. You can even use Notepad to create a Robot Framework test suite, but that would not be the smartest choice. As mentioned before, that even the Robot Framework creator still uses the PyCharm IDE, so it's a great choice for this course and for any test automation work you do. The format of Robot Framework scripts are simply space separated. And what that means is we'll use between 2 and 4 spaces or even tabs to separate our plain English keywords from our arguments. Robot Framework also supports a pipe delimited formats, which simply means using the pipe character as a separator instead of spaces. But I'm not a huge fan of that because when used properly, I think whitespace does a great job of separating keywords from data and it doesn't take up extra space and clutter your script like pipes do. Finally, while you can use Python scripting in Robot Framework, the majority of your Robot Framework scripting should be keyword-driven. And what that means is we can type plain English keywords to invoke pre-written logic that might be other keywords or even Python script that lives in pre-written libraries you can easily download and use. This gives us the ability to create our scripts in a natural language and what that means is our scripts can reflect our business domain, meaning that the test steps will reflect capabilities that our system under test can perform. So anybody who reads the tests can understand exactly what they're doing. Here's a diagram that makes it easier to visualize this. In our chosen IDE, we will create a test script file, which is simply a text file with a .robot extension. And at the top of that script file, we will declare one or more of these libraries that, like I said, can do some heavy lifting for us when we just type in plain English keywords. Each of these libraries has documentation that exposes various keywords that we can use to get work done. Now, this is an overly simple example. We typically won't include libraries directly into our test script files. What we will usually do looks more like this. We'll create a test script file, which again is a .robot file and that will contain our tests. Those tests will then invoke keywords that live inside an App Keyword File, which is also a .robot file. The keywords in the app keyword file simply represent common things that our application under tests can do, like log in, log out, add new customer, and so on. And the app keyword file could theoretically invoke these various libraries to perform the steps for any keyword we've written. So you can see that the libraries we have access to include some built-in libraries that come with Robot Framework when you install it, and we also have access to external libraries that we can install from a command line using pip install and the name of that library. Some of the many built-in libraries include; an operating system library, that allows us to interact with folders and files. There's a process library, that allows us to run applications. There's a dialogs library, that allows us to present different kinds of dialogs for maybe user input. There's a collections library for lists of data. There's a string library that has all kinds of string manipulations. Date Time library, XML library, and even a screen shot library, that allows us to capture what's happening on the screen anytime we want to. And bear in mind that Robot Framework automatically takes screenshots anytime a test fails, so you only need to invoke the screenshot activity in situations other than test case failures. There are so many external libraries. I'm only showing a few here. The SeleniumLibrary, of course, allows us to interact with web browsers. The database library, allows us to talk to various kinds of databases. Appium, of course, allows us to interact with mobile devices. The diff library allows us to compare the text contents of two different files. FTP allows us to upload and download files from FTP servers, and that's a common thing to do in some business scenarios. You've got an Excel library. SSH for interacting with Linux servers. Archive for dealing with zip files and so on. So going back to our simple example, while it's not a best practice, technically you can have a test script talking directly to libraries, and this is exactly what that looks like. I've got PyCharm open and you can see in my C:\development\robot-scripts directory, I have a project folder called CRM, which simply stands for Customer Relationship Manager. It's a kind of web application we might need to test. And inside there I've got a file called CRM.robot. There's literally nothing else in my project. I've double clicked on that file and it's open in this right pane. At the top of my script file, you can see I've pulled in the SeleniumLibrary, which is a pre-existing library full of keywords that allow me to interact with the web browser. And down here in this test cases section, I've created a test case that is comprised solely of keywords from this SeleniumLibrary. You can see these first two steps, I'm doing a little bit of setup. Then I open the browser and these two lines are only for my recordings because I want to position the browser in the same place where my recording Window is. I'm going to wait until the page contains some text. I'll click on a link, I'll input some text, click on a button again, wait until the next page contains something, then I'll sleep so you can see the results and finally close all the browsers. And again, I didn't have to write any of the logic behind these keywords. The keywords are invoking Python script that I got for free when I used pip install to install the Robot Framework SeleniumLibrary in the previous section. So this is what a Robot Framework script could look like in its most simplest form. Again, this is not a best practice. In future sections, I'm going to show you how we can refactor this so it's more readable, more modular, and more object oriented. When you want to explore what's available in these libraries, simply navigate over to robotframework.org and scroll down the page until you get to the resources section and then click on either the "BUILT-IN" button or the "LIBRARIES" button. Again, these built-in libraries are pre-installed when you install Robot Framework. You can scroll down the list and click on "SHOW MORE" and then simply click on any one of the links for a library and you're taken to that library's keyword documentation page. When you click on any given keyword, it gives you examples for how to use that keyword. Super cool. So in the next few videos, we're going to start off very simple by creating a test script that's going to use SeleniumLibrary keywords that will then talk to the Selenium Webdrivers that we installed and run a very simple test on Edge, Firefox, and Chrome browsers. I think you'll be surprised by how fast and easy it is. In future sections and videos, I'm going to show you how to take that basic script and turn it into something much more enterprise worthy. The outcome from this course is that you're going to know how to create a layered test suite like this, where you'll have a test script that has your test cases in it, and your test cases are going to talk to an app keyword file, and that app keyword file contains chunky, high level keywords that represent capabilities that your application can do. In order to achieve the capabilities, the keywords in the App Keyword File are then going to talk to page object files, and those page object files are also .robot files, and they contain more granular keywords that pertain to specific pages of your website under test. As you'll see, those page objects will also encapsulate the locators specific for those pages. That's how we keep things separate and organized. Your page object keywords are going to then talk to the various libraries. For example, for certain test steps, you might interact with the SeleniumLibrary and drive a web browser around. And in some other steps you might interact with a database library, for example, and make a database query to insert, or update, or delete some data. So bear in mind that even though we're going to start here, throughout the rest of the course, you're going to learn all the techniques that will allow you to create a much more elegant and enterprise ready test suite. I hope you're excited.

Contents