Robotframework Automated test framework-write custom robotframework Lib using Python

Source: Internet
Author: User

Building a Lib project with Python

There are many IDE tools that can be used to develop Python lib, common Pycharm,eclipse with Pydev plug-ins and so on, and Robotframework-eclipseide plugin has been provided in Robotframework official website. , you can support eclipse, the plug-in's access address is Https://github.com/NitorCreations/RobotFramework-EclipseIDE, and you can download the plugin from that address.

Here we build a lib in the form of the eclipse with Pydev plug-in, which can be downloaded from the http://www.pydev.org/, or installed through the installation of the Eclipse Online installation address:/HTTP Www.pydev.org/updates

After launching Eclipse, click the Eclipse menu Help->install New software ..., in the popup dialog box, click the Add button, name fill in: Pydev, location fill 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, and 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 the Python.exe, and open it to display a window with a lot of check boxes. Click OK

Once the plug-in configuration is complete, we can use eclipse to build a Python project where we create a new Excellibrary project, enter excellibrary in Project name, and click Finish to complete the project creation.

Writing a custom lib using Python

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

#-*-coding:utf-8-*-" "import operation Excel requires a third-party xlrd Library" "Importxlrd" "introduction of robotframework log output logger" " fromRobot.apiImportLogger" "define a Python class" "classexcelutil ():def __init__(self):return    " "Open an Excel file" "    defOpen_excel (self,excelfile):Try: Data=Xlrd.open_workbook (excelfile)returnDataexceptException,e:logger.error (e)" "gets the data method in Excel, specifying the Excel file name and sheetname to be read by parameters" "    defGet_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= []         forRowNuminchRange (1, rows): Row=sheet.row_values (rownum)ifRow:app= {}                 forJinchRange (len (linedata)): App[linedata[j]]=Row[j] List.append (APP)returnList

In the sample code, the function get_excel_bysheetname is defined to get the data in Excel, and parameters are used to specify which data in the sheet of Excel is needed, and the sheet data obtained is eventually returned as a list. Each record in the list is stored in a dictionary form in Python.

We can call the lib we write to whether it can be used normally, in ride, we import just write lib.

You can then see our custom keywords by ride the F5 shortcut key.

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

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

Log ${list}

Execution Result:

"The original is owned by the author, welcome reprint, but retain the copyright, and reproduced, need to indicate the source "

Robotframework Automated test framework-write custom robotframework Lib using Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.