使用的Library:
Library Selenium2Library
Library Screenshot
Library DateTime
Library OperatingSystem
1.时间,要导入DateTime库
获取当前时间戳
${time} Get Current Date result_format=%Y%m%d%H%M%S
${date} Get Current Date result_format=%Y%m%d
time
#获取当前时间或者指定时区时间并处理
${date1}= get current date
log ${date1}
${date2}= get current date UTC
log ${date2}
${date3}= get current date increment=02:30:00
log ${date3}
${date4}= get current date UTC -5 hours
log ${date4}
${date5}= get current date result_format=datetime
log ${date5}
time2
#对时间格式转化,获取时间戳。
${date}= Convert Date 20210501 12:05:03.111
should be equal ${date} 2021-05-01 12:05:03.111
${date}= Convert Date ${date} epoch
should be equal ${date} ${1619841903.111}
${date}= Convert Date 05.28.2021 12:05 exclude_millis=yes date_format=%m.%d.%Y %H:%M
should be equal ${date} 2021-05-28 12:05:00
time4
${date} get current date utc
${time} convert date ${date} result_format=%m%d
time5
${time} get current date utc
log ${time}
${time} get current date result_format=%Y-%m-%dT%H:%M:%S
log ${time}
${d} Add Time To Date ${time} -7 days
log ${d}
运行结果:
Starting test: Shimian.Test.time
20210630 14:23:37.898 : INFO : ${date1} = 2021-06-30 14:23:37.898
20210630 14:23:37.899 : INFO : 2021-06-30 14:23:37.898
20210630 14:23:37.899 : INFO : ${date2} = 2021-06-30 06:23:37.899
20210630 14:23:37.899 : INFO : 2021-06-30 06:23:37.899
20210630 14:23:37.900 : INFO : ${date3} = 2021-06-30 16:53:37.900
20210630 14:23:37.901 : INFO : 2021-06-30 16:53:37.900
20210630 14:23:37.901 : INFO : ${date4} = 2021-06-30 01:23:37.901
20210630 14:23:37.902 : INFO : 2021-06-30 01:23:37.901
20210630 14:23:37.902 : INFO : ${date5} = 2021-06-30 14:23:37.901626
20210630 14:23:37.903 : INFO : 2021-06-30 14:23:37.901626
Ending test: Shimian.Test.time
Starting test: Shimian.Test.time2
20210630 14:23:37.908 : INFO : ${date} = 2021-05-01 12:05:03.111
20210630 14:23:37.909 : INFO : ${date} = 1619841903.111
20210630 14:23:37.910 : INFO : ${date} = 2021-05-28 12:05:00
Ending test: Shimian.Test.time2
Starting test: Shimian.Test.time4
20210630 14:23:37.912 : INFO : ${date} = 2021-06-30 06:23:37.912
20210630 14:23:37.913 : INFO : ${time} = 0630
Ending test: Shimian.Test.time4
Starting test: Shimian.Test.time5
20210630 14:23:37.914 : INFO : ${time} = 2021-06-30 06:23:37.914
20210630 14:23:37.915 : INFO : 2021-06-30 06:23:37.914
20210630 14:23:37.916 : INFO : ${time} = 2021-06-30T14:23:37
20210630 14:23:37.916 : INFO : 2021-06-30T14:23:37
20210630 14:23:37.917 : INFO : ${d} = 2021-06-23 14:23:37.000
20210630 14:23:37.917 : INFO : 2021-06-23 14:23:37.000
Ending test: Shimian.Test.time5
2.Repeat Keyword重复执行关键字,Press Keys模拟键盘操作
Repeat Keyword 25 Press Keys ${输入框xpath} BACK_SPACE #模拟键盘操作重复点击25次BACK_SPACE键
3.随机字符串
${num} Evaluate random.random() random #random()生成0<=n<1之间的随机实数--它会生成一个随机的浮点数,范围是在0.0~1.0之间
log ${num}
${a1} evaluate random.choice(['130','133','187','159','180','177','150','156','aaa']) random #choice(seq)从序列中返回随机的元素
log ${a1}
${a2} evaluate random.uniform(1.5,5.6) random #random.uniform(a,b)正好弥补了1中函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限
log ${a2}
${a3} evaluate random.randint(1000,9999) random #random.randint(a,b):随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值
log ${a3}
${a6} evaluate random.sample("0123456789",5) random,string #random.sample()---sample(seq, n) 从序列seq中选择n个随机且独立的元素;
${a7} evaluate "".join(random.sample("0123456789",5)) random,string
log ${a6}
log ${a7}
${letters} evaluate "".join(random.sample(string.ascii_letters+string.digits,32)) random,string
${random} evaluate "".join(random.sample(string.ascii_letters + string.digits,int(${num}))) random,string #获取数字字母组合的随机字符串,${num}为想获取的随机字符串长度
${num} evaluate random.randint(20,1000) random #获取20-1000之前的随机数字
4.Choose File选择本地文件上传
Choose File ${XPath} C:\\${文件名称}
5.Set Focus To Element 聚焦,适用于滚动条
Set Focus To Element ${element locator} #聚焦到某个元素
6.判断文件下载是否成功,并删除该文件
Assert file download
[Arguments] ${文件名称}
${file_path} Set Variable C:\\Users\\86184\\Downloads\\${文件名称}
sleep 5
Wait Until Keyword Succeeds 3x 5s Should Exist ${file_path}
sleep 2
Remove File ${file_path}