Python 3.6 + robotFramework automated framework Environment setup, learning notes,

Source: Internet
Author: User
Tags for in range

Python 3.6 + robotFramework automated framework Environment setup, learning notes,

######################################## #########################
# Author: Chen yuebai
# _ Blogs: http://www.cnblogs.com/chenyuebai/
######################################## #########################

 I. Environment Construction
Overview: win 7 + python 3.6 + pycharm +Robotframework+ IntelliBot

1. Install related libraries

Install in sequence:
Pip install robotframework
Https://pypi.python.org/pypi/selenium/ # dependency packages
Https://pypi.python.org/pypi/robotframework-seleniumlibrary # dependency packages
Https://pypi.python.org/pypi/robotframework-selenium2library

2. pycharm

Development IDE, omitted


3.IntelliBotPycharm development plug-in [syntax highlighting]
Method 1: pycharm> File> setting> Plugins> Browse reponsitories
Method 2: https://plugins.jetbrains.com/plugin/7386-intellibot download to local
Pycharm> File> setting> Plugins> install plugin from disk


4. Configure the actuator for pycharm
File> setting> Tools> External tool. After the configuration is complete, you can select to use this executor in the. robot File for execution.

Parameters: current file path

Working directory: job path, log, result output, etc., as follows:

Note: log.html execution log

Xml of output. xml execution information, which can be used for subsequent secondary Parsing

Report. xml execution result report


2. Run the command in cmd.
Run a use case:
Pybot -- test test_case test_suit.robot

Run the specified file:
Pybot test_suit.robot

Run the test file suffixed with. robot in the current directory.
Pybot *. robot

Run all the use cases under the current testpath directory
Pybot testpath


III. Basic Syntax of case script (. robot file)

1. Basic syntax

* ** Settings ** Library MyLib # import the custom Library Selenium2Library *** Test Cases *** # The first behavior is in a fixed format, and the ID # is recommended to be consistent with python, use tab indent alignment (Set tab = 4 spaces in pycharm); otherwise, execution may fail, report "Test case contains no keywords" case1 helloworld # case Name log chenyuebai first rfw case # log is equivalent to the print of python and can generate a log. view case 2 log # create exception log in xml, supports multiple levels of log test line 2 ERRORcase 3 varible $ {myname} Set variable chen # defines the variable log my name is $ {myname} # Use the variable # case 4 Use varible # the case where the Variable scope is defined (case3); otherwise, "Variable '$ {myname}' not found is reported. "# log $ {myname} case 5 Catenate $ {hi1} Catenate hello world # define variable Mode 2 to connect two objects, support custom connector log ${hi1 }$ {hi2} Catenate SEPARATOR = ---- hello world log $ {hi2} case 6 list @ {mylist} create list a B c # definition list log outputs @ {mylist} # print the element in the List case 7 get time $ {currentTime} get time # get the current time 18:05:47 log $ {currentTime }$ {currentYear} get t Ime format = year # multiple input parameters are supported. For details, see the function definition log current year is $ {currentYear} case 8 sleep log get time sleep 1 # sleep, which is equivalent to the time of python. sleep (5) log get timecase 9 if $ {score} set variable 55 run keyword if $ {score}> = 90 log excellent... else if $ {score }>= 70 log is good # ELSE/else if should be capitalized... The syntax is very painful .. Why not directly apply the python syntax ..... ELSE log is poor #... I don't know what to consider .. It looks like the identifier belongs to the judgment logic of "run keyword if". Is it similar to the python tab? Case 10 for # for Loop, note that you need to use \ to identify the for loop range, feel and above... FOR $ {I} in range 5 # for in range (5): \ log $ {I} # print (I) case 11 allround the list @ {myList} create list 1 2 3 4 # myList = [1, 2, 4]: FOR $ {I} IN @ {myList} # for I in myList: \ log $ {I} # print (I) case 12 Evauate # Call the method in python, very powerful $ {randomNum} EVALUATE random. random () random # variable keyword EVALUATE the database name where the method of calling python is located log $ {randomNum} case 13 call my python # import the custom database, the following is a detailed description of $ {nowTime} get current time log $ {nowTime} case 14 Comment log start # line 1 comment line 2 # annotation: use the keyword or # log endcase 15 Selenium2Library # Selenium2Library library, operate the browser, and automate the web interface, to be refined open_browser http://www.baidu.com firefox Input text id = kw Chen yuebai click button id = su sleep 3 close Browser

 

2. Call the custom library in the robot File

Selenium2Library provides many methods, but some scenarios still need to be compiled by yourself. robotFramework supports importing User-Defined libraries.

Take my directory structure as an example:

(1) file of the custom method: MyRobotFrameworkLib. py

 
Import timeclass MyRobotFrameworkLib (): def _ init _ (self): pass # Get current time def get_current_time (self): current_time = time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () return current_time

(2) create MyLib. py

 
"Inherits the classes in the Custom library. rfw will parse the functions in the class as keywords. On the internet, It is also said that this file should be named _ init __. py, but I tried it. when the robot file is executed, the import fails. It seems that the file name must be the same as the class name (currently this is the case) "from MyRobotFrameworkLib import * class MyLib (MyRobotFrameworkLib): ROBOT_LIBRARY_SCOPE =" GLOBAL"
 

(3) import the robot file into the custom class and use the methods in the class

* ** Settings ** Library MyLib # import the custom Library case 13 call my python $ {nowTime} get current time log $ {nowTime}

 

3. Use Selenium2Library for automated web interface Testing

# Todo

20180126





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.