Robot Framework Usage Summary

Source: Internet
Author: User
Tags builtin jenkins ci

The recent project on the use of RF rapid implementation of a number of acceptance testing automation case, feel good, very useful, the following record the use of RF to automate the process.

What is RF?

RF is a test framework that enables testers to quickly automate acceptance testing within their framework. There are many extension libraries available for you to use, and some automated test cases can be implemented without any language programming foundation.

To put it bluntly, the role of any kind of framework is to help you do some basic work, so that you focus more on the business logic to be tested, rather than on technical details, including how use cases work, how they are organized, how logs are recorded, how they are presented, how they are integrated with CI, and so on.

The use of the RF framework combined with the Jenkins CI tool makes it easy to implement remote deployment, operation, and results presentation of your tests. This is much faster than rewriting a wheel and writing a system of your own, which is best suited for just starting a project.

What can RF do?

What RF can do depends on what extension framework you use, the default built-in libraries and external extension libraries provided by RF, and of course you can write your own extension libraries to customize functionality. The basic library has been able to meet the general testing requirements, including the mobile phone, web-side automated testing, as well as API interface testing.

Writing RF Files

RF files are usually suffixed with robot, and many editing tools are provided to facilitate the editing of robot files. I am using Pycharm RF plug-in for editing, because I need to use Python to write a large number of extension libraries, so in Pycharm Unified robot and PY file editing, it is very convenient. Or the use of the official ride is also a good choice, a purely graphical interface to facilitate the team without development experience of people involved.

Structure of the RF file

Let's look at an example of an RF file:

As shown above, an RF file typically consists of three nodes:

    • Settings node:
      1. Set setup and Tearndown actions for this test suite
      2. Setup and Tearndown actions for each test case in this test suite
      3. Specify the test template
      4. Specify the location of the resource files referenced by this test suite
      5. Use the Library keyword to reference the RF standard library, or customize the libraries:

    • Test Cases Node:
      1. You can define a common test case
      2. You can also invoke the template and pass the template the required parameters to it
      3. The keywords that are called in the test case may come from the following three places:
      * keyword defined in the keywords node of the current test suite file
      * The keyword defined in the resource file specified by the Setttings node
      * keyword defined in the built-in Builtin library

    • Keywords node:
      1. Keyword can be understood as a common method for use in test case
      2. Keyword can pass in parameters and return results
      3. RF also provides a lot of logic to determine if, loop for and other keywords

Structure of the resource resource file

In fact, the resource file is not much different from ordinary robot files, except that it is a library file that is imported and is commonly used to define common variables and keywords:

Write your own library file

RF runs use many languages to write your own library files, choose Python to write, first look at the existing library file extension

Extensions to the Selenium2library library file:
1 #Import selenium2library Module2  fromSelenium2libraryImportselenium2library Module3  fromSelenium.common.exceptionsImportstaleelementreferenceexception4 Import Time5 6 7 def_get_table_field_value (element, field):8     returnElement.find_element_by_xpath ("./td[@field = '"+ Field +"']"). Text.strip ()9 Ten #Inherit Selenium2library One classcustomseleniumlibrary (selenium2library): A     defGet_table_row_count (Self, table_locator): -Attempts =0 -          whileTrue: the             Try: -Table =Self._table_element_finder.find (Self._current_browser (), Table_locator) -                 returnLen (Table.find_elements_by_xpath ("./tbody/tr")) -             exceptstaleelementreferenceexception: +Time.sleep (1) -                 ifAttempts >= 1: +                     RaiseAssertionerror ("Cell in table%s could not being found."%table_locator) A                 Else: at                     Pass -Attempts + = 1 -  -     defget_user_search_results (self, Table_locator, Row_index): -Table =Self._table_element_finder.find (Self._current_browser (), Table_locator) -RET = [] in         ifTable is  notNone: -rows = Table.find_elements_by_xpath ("./tbody/tr") to             ifLen (rows) <=0: +                 returnNone -Row_index =Int (row_index) the             ifLen (rows)-1 <Row_index: *                 RaiseAssertionerror ("The row index '%s ' is large than row length '%s '."%(Row_index, Len (rows) )) $              forRowinchrows:Panax NotoginsengDIC = { -                     'userId': _get_table_field_value (Row,'userId'), the                     'Nickname': _get_table_field_value (Row,'Nickname'), +                     'Realname': _get_table_field_value (Row,'Realname'), A                     'Mobile': _get_table_field_value (Row,'Mobile'), the                     'Idno': _get_table_field_value (Row,'Idno'), +                     'usertype': _get_table_field_value (Row,'usertype'), -                     'Verifyuserstatus': _get_table_field_value (Row,'Verifyuserstatus'), $                     'operator': _get_table_field_value (Row,'Operater'), $                     'Operatetime': _get_table_field_value (Row,'Operatetime'), -                 } - ret.append (DIC) the             returnRet[row_index] -         Else:Wuyi             returnNone the  -...

To create a new library file:
1 #-*-coding:utf-8-*-2 3  fromLibs. Db_utils.utilsImport*4  fromLibs.request_utilsImportUtils5  fromLibs.request_utilsImportFlow_task_manage6  fromLibs.global_enumImport*7  fromLibs.modelImportUser_search_result8  fromRobot.libraries.BuiltInImportBuiltIn9 Import TimeTen  One  A classVerifylibrary (object): -  -     def __init__(Self, base_url, username, dubbo_web_base_url=None): theSelf.base_url =Base_url -Self.username =username -Self.request_utils =Utils. Requestutil (Base_url, username) -Self.flow_task_request_utils =Flow_task_manage. Flowtaskmanage (Base_url, username) +Self.built_in =BuiltIn () -         ifDubbo_web_base_url is  notNone: +Self.dubbo_web_request_utils =Utils. Requestutil (Dubbo_web_base_url) A  at     defUpdate_verify_user_role (self, email, dept_id, role_id, amount_limit=5000): -Real_name =get_verify_user_name_by_email (email) -verify_user_id =get_verify_user_id_by_email (email) - Self.request_utils.login () -Response =Self.request_utils.update_verify_user (Real_name, verify_user_id, Amount_limit, dept_id, role_id) -         returnResponse.json ()

Note that the Verifylibrary constructor requires a minimum of two parameters to be passed in when the robot file references this library file:

RF Framework integrated with Jenkins CI

Using Jenkins to run the RF write test case is simple, first you need to install the RF extension on Jenkins:

Then, use the Pybot command line to run the well-written RF test case:

At the end of the test, you can parse the results of the test on Jenkins and test the results of each build in good detail:

Robot Framework Usage Summary

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.