“This is the 20th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Following the Python RobotFramework parsing, we understand and get familiar with the RobotFramework framework, which is mainly divided into four parts:

  • Test Data: Test cases written in tabular form
  • RobotFramework: starts tests, processes data, executes test cases, and generates logs and reports
  • Test Libraries: RF systems are not aware of Test content. All actions are performed by RF interactive Test Libraries and results are returned to RF for display
  • System Under Test: Application interface or Test driver used in the Test library

Among them, Test Library except RF Biltin does not need to import in the Test case, other libraries need to use the form of Library XXX Library imported in advance.

If you want to use a third party Library, you need to download the Library using PIP first and then import it using Library. Otherwise, RF will report a Not Found error and cannot execute the test case.

The RobotFramework system is powerful enough to support multiple types of tests and access custom library imports.

In this episode, you will learn how to create a custom library using the RobotFramework framework

1. Overview of RobotFramework test libraries

As we all know, the power of the RobotFramework framework is that you can import personalized libraries and therefore support testing in different scenarios.

  • All of this learning will be done in a Python 3.7 environment
  • Python third-party libraries are installed in the python37/Lib/site-packages directory

First, we download the framework of the Robotframework via PIP. The RF package can be found in the site-packages path

In the Robot library file Libraries, you can see the system keywords built into the RobotFramework

Let’s open a random library like Builtin to see the internal implementation as follows

2. Create and import Python libraries

According to the above, check the official RobotFramework built-in library implementation, in accordance with the hoist way used to create custom libraries.

Creating a custom RobotFramework library requires three steps:

  1. Create a new directory under the Python installation directory Lib\site-packages. (The file directory name is the library name) for example: PYCLibrary

  2. Under the new PYCLibrary file directory, create a Python file with the extension py. Such as: PYClibrary. Py

    • In python we need to import the from robot.api.deco import keyword modifier
    #! /usr/bin/env python
    #-*- coding: utf-8 -*-
    
    # from robot.libraries.BuiltIn import BuiltIn
    from robot.api.deco import keyword
    # from robot.api import logger
    
    
    class MyClass(object) :
        def __init__(self) :
            pass
    
        @keyword
        def printMsg(self, msg) :
            print(msg)
    
        @keyword
        def printMoreMsg(self, **msg) :
            for i in msg:
                print(i)
    
        @keyword
        def testlib_login(self, username, password) :
            print('username: %s' % username)
            print('password: %s' % password)
    
            if username == 'user' and password == 'password':
                print('Login OK.')
                return True
    
            print('Login Failed.')
            return False
    Copy the code
  3. At the same time, in the PYCLibrary directory, we also need to create an __init__.py file to display the keyword class directly, so that RF can quickly find it.

    #! /usr/bin/env python
    #-*- coding: utf-8 -*-
    
    from .PYClibrary import MyClass
    
    class PYCLibrary(MyClass) :
        ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    Copy the code

3. Use the custom library approach

  • After opening the RIDE desktop on the computer desktop, we first imported the custom library we just created, PYCLibrary, into Testcase

Note: library names will be highlighted in red if the library has not been imported successfully

  • In the RIDE toolbar, click the K mark button to enter, select the Source bar and select the PYCLibrary library, you can see that we created three keywords

  • Going into Testcase, we create a use case using the testlib_login method

    testpyc
        testlib login    Juejin    1234567
    Copy the code
  • Run TextPyC alone, and the resulting log output is shown

4. Error reporting

When we import the library, we will encounter the phenomenon that the keyword library is marked red. We need to perform the following steps:

  1. Python packages for automatic keywords need to be placed in the Python installation directory Lib\site-packages

  2. The structure of the Python package file must meet the following requirements

    /--custLibrary
      /--- __init__.py
      /---Keyword.py
    Copy the code
  3. Add PYTHOPATH to the system environment variable with a value of the custom Python package path

  4. On the CLI, run the following command to view the path and name of the customized Python package

>python
>>> import sys
>>> sys.path
Copy the code
  1. Add the Library Library path to RIDE

conclusion

In this issue, the main Robotframework framework to create a custom keyword database steps for practical operation and learning.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time