RobotFramework Test Automation Framework - Write custom RobotFramework Lib using Python

  Build Lib project with Python

There are many IDE tools that can be used to develop Python Lib, such as Pycharm, Eclipse with PyDev plug-ins, etc., and the RobotFramework-Eclipse IDE plug-in has been provided on the RobotFramework official website, which can support Eclipse, and the access address of the plug-in is https:// github.com/NitorCreations/RobotFramework-EclipseIDE , you can download the plugin through this address.

Here we build a Lib in the form of Eclipse with PyDev plug-in. You can download the corresponding plug-in from http://www.pydev.org/ , or you can install it through eclipse online installation. Online installation and installation address: http: //www.pydev.org/updates

After starting eclipse, click the eclipse menu Help->Install New Software..., in the pop-up dialog box, click the Add button, fill in the Name: Pydev, fill in the Location http://pydev.org/updates

After clicking OK, you can see the plugin options for installation, here we choose to install all.

Then click Next and wait for the installation to complete.

After the installation is complete, you need to configure the Python interpreter in eclipse. In the Eclipse menu bar, click Windows -> Preferences. In the dialog box, click pyDev->Interpreter - Python. Interpreters click the New button, select the path of python.exe, After opening it shows a window with many checkboxes. Click OK

After the plugin configuration is complete, we can use eclipse to build a python project. Here we create a new ExcelLibrary project, enter ExcelLibrary in the Project name, and then click Finish to complete the project creation.

Write a custom Lib using Python

After the Lib project is created, we can write our own Lib, here we write a Lib example that reads data from Excel.

# -*- coding: utf-8 -*- 
'''
Import operation excel requires third-party xlrd Library
'''
import xlrd
'''
Introduce the log output logger of robotFramework
'''
from robot.api import logger
'''
define a python class
'''
class ExcelUtil():
    def __init__(self):
        return
    '''
              open an excel file
    '''
    def open_excel(self,excelfile):
        try:
            data = xlrd.open_workbook(excelfile)
            return data
        except Exception,e:
            logger.error(e)
    '''
    Get the data method in excel, specify the excel file name and sheetname to be read through parameters
    '''
    def get_excel_bysheetname(self,excelfile,lineindex=0,sheetname='Sheet1'):
        data = self.open_excel(excelfile)
        sheet = data.sheet_by_name(sheetname)
        rows = sheet.nrows
        linedata = sheet.row_values(lineindex)
        list = []
        for rownum in range(1,rows):
            row = sheet.row_values(rownum)
            if row:
                app = {}
                for j in range(len(linedata)):
                    app[linedata[j]] = row[j]
                list.append(app)
        return list

In the sample code, the function get_excel_bysheetname is defined to obtain the data in excel. You can specify which sheet of excel data needs to be obtained through parameters, and the obtained sheet data is finally returned in the form of a list. Each record in the List is stored in the form of a dictionary in python.

We can call whether the lib we wrote can be used normally. In RIDE, we import the lib we just wrote.

Then through the F5 shortcut key in RIDE, you can see our custom keywords.

After importing, we can call it through a test case and output the result in log form.

${list}     Get Excel Bysheetname  E:\\task.xls

log   ${list}    

Results of the:

[The original text belongs to the author, welcome to reprint, but the copyright is reserved, and the source needs to be indicated when reprinting ]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325110151&siteId=291194637