This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dongxiaoyan-tsg-autotest/02-Keyword/common/readme.robot
2020-12-15 19:58:08 +08:00

133 lines
5.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

*** Settings ***
Library Collections
*** Test Cases ***
#https://www.cnblogs.com/fnng/p/3901391.html
#引用变量文件https://www.cnblogs.com/chengchengla1990/p/9895244.html
#关于标签接口的一律贴bifang标签全流程的贴adc和bifang或uiUI的贴ui
Common-test
[Tags] Common-test
Comment :1、log
Comment log 关键字就是编程语言里的“print”一样可以打印任何你想打印的内容。
log 123
Comment 2、定义变量
Comment 通过“Set variable”关键字来定义变量
${a} Set Variable Hello Word!
Comment 3、连接对象
Comment “Catenate”关键字可以连接多个信息
${hi} Catenate Hellow Word! What Do You Do!
Comment 加上“SEPARATOR=”可以对多个连接的信息进行分割。
${hi} Catenate SEPARATOR=-- Hellow Word! What Do You Do!
Comment 4、定义列表
Comment 通过“Create List”关键字可以定义列表。每个字符串前面加 u是为了统一编码问题将字符串转为 Unicode 编码。
${abc} Create List a b c
log ${abc}
Comment 如果通过“@{}”去定义列表的话可以通过“log many”关键字进行打印
@{abc} Create List a b c
log many ${abc}
Comment 5、时间的操作
Comment Robot Framework 中提供了“get time”关键字用来获取当前时间。
${time} Get Time
log ${time}
Comment 6、设置休眠时间
Comment “sleep”关键字用来设置休眠一定时间sleep 关键字默认以“秒”为单位。
${time} Get Time
log ${time}
sleep 5
${time} Get Time
log ${time}
Comment 7、if语句
Comment 通过“run keyword if”关键字可以编写 if 分支语句。
${a} Set Variable 100
Run Keyword If ${a}>=90 log 优秀
... ELSE IF ${a}>=70 log 优秀
... ELSE IF ${a}>=60 log 及格
... ELSE log 不及格
Comment 首先定义一个变量 a 等于 59 。
Comment If 判断 a 大于等于 90 ,满足条件 log 输出 “优秀 ”;
Comment 不满足上面的条件,接着 else if 判断 a 大于等于 70 ,满足条件 log 输出 “良好”;
Comment 不满足上面的条件,接着 else if 判断 a 大于等于 60 ,满足条件 log 输出 “及格”;
Comment 上面的条件都不满足else log 输出“不及格”。
Comment 注:注意 ELSE IF 和 ELSE 前面的三个点点点(...。注意ELSE IF和ELSE要是大写。
Comment 8、for 循环
Comment 在 Robot Framework 中编写循环通过“:for”。通过“for”定义 for 循环in range 用于指定循环的范围。
Comment 例子1这个例子为执行 10 次循环
:FOR ${i} IN RANGE 10
\ log ${i}
Comment 注意in range 定义为 10它的范围是 0~9
Comment 例 2遍历列表
Comment “create list” 关键字用来定义列表a,b,c“@{}”用来存放列表。通过过“:for”循环来来遍历@{abc}列表中的字符。
@{abc} Create List a b c
:FOR ${i} IN @{abc}
\ log ${i}
Comment 例 3循环中的判断
Comment 通过“Exit For Loop If”关键字时行 for 循环内的判断,当满足 Exit For Loop If 条件后,循环结束。
@{abc} Create List a b c
:FOR ${i} IN @{abc}
\ Exit For Loop If '${i}'=='b'
\ log ${i}
log ${i}
Comment 从执行结果看到当循环到字符 b 时Exit For Loop If 条件成立,结束循环;通过 log 打印当前的字符 b。
Comment 9、强大的 Evaluate
Comment 因为通过它可以使用 Python 语言中所提供的方法
Comment 例 1生成随即数
#import random
#random.randint(1,5)
Comment 使用Evaluate关键字之后可使用Python中的类和方法
${d} Evaluate random.randint(1,5) random
log ${d}
Comment 例 2执行本地程序
Comment #Evaluate os.system('python c:/helloword.py') os
Comment 通过调用 Python 所提供的 os 模块中的 system()方法可以执行本地 Python 文件。至于在.py 文件中
Comment Python 可以做任何想做的事。
Comment 对于 system()方法来说,它也不单单可执行 Python 文件,任何在 cmd 命令提示符下可运行文件和命
Comment 令,它都可以执行。
Comment 不过,一般情况下不建议通过 system()方法去执行外部程序。这样做其实就脱离了 Robot Framework
Comment 也就是说不管 Robot Framework 什么事了。我们尽量把要做的事情通过 Python 封装成关键字给 Robot
Comment Framework 使用。
Comment 10、导入库
Comment (1)“Import Library”等同于 Python 语言中的 import。Robot Framework 会遍历 Python 安装目录下的相关目录查找“Selenium2Library”模块
Comment Import Library Selenium2Libary
Comment (2)调用 Python 文件
Comment 首先创建 test.py 文件
Comment coding=utf-8
Comment def add(a,b):
Comment return a+b
Comment if __name__ == "__main__":
Comment a = add(4,5)
Comment print a
Comment 通过“Evaluate”转化成为 int 类型后,再调用 add 就得到了想要的结果。
Import Library E:/test.py
${a} Evaluate int(4)
${a} Evaluate int(5)
${add} add ${a} ${b}
log ${add}
Comment 11、注释
Comment Robot Framework 中添加注释也非常简单。
Comment 1“Comment”关键字用于设置脚本中的注释。
Comment 2也可以像 Python 一样使用“#”号进行注释
Comment 这是注释
#这是注释