先看下需求,这个报表中有个时长,需要对昨日和前日的时长进行比较,我们获取到的元素是例如“9分43秒”这样的格式

RobotFramework 截取中文中的数字比较时长

1.首先要讲中文中的分和秒分别提取出来

提取python代码如下:

import re
str = '11分1秒'
list = re.findall(r'\d+', str)
print(list)

执行结果['11', '1']

可以看到,此段代码可以讲分、秒组装到一个list中

2.Python按照索引访问list就可以活得分和秒的具体值,进而计算出总共的秒数

import re
str = '11分1秒'
list = re.findall(r'\d+', str)
print(list[0])
print(list[1])

s=list[0]*60+list[1]

 

好了,代码知道怎么写了,那么我们把它用到工具里面去,这里有两种方法

第一种:直接在工具中使用python代码

1.首先引入re

Library re

RobotFramework 截取中文中的数字比较时长

 

 

 2.截取分和秒

#提取分、秒

${Yesterday_Minutes}   Evaluate re.findall(r'\\d+', '${Yesterday_Averagevisit_Time}')[0] re
${Yesterday_Seconds} Evaluate re.findall(r'\\d+', '${Yesterday_Averagevisit_Time}')[1] re

#转换成秒

${Seconds_1}    Evaluate    ${Yesterday_Minutes}*60+${Yesterday_Seconds}

注意此处需要用\\可以避免转义,一个\的时候,在使用该工具时被转义了,会出现如下错误

RobotFramework 截取中文中的数字比较时长

 

 

 正确的写法

RobotFramework 截取中文中的数字比较时长

 

整体代码:

*** Settings ***
Library Selenium2Library
Library collections
Library re

*** Test Cases ***
001-浙江新闻分析图表分析结果
Open Browser http://10.200.70.207/#/home chrome
Maximize Browser Window
Input Text xpath=//*[@]/div[2]/div[3]/div
sleep 2
#昨日指标和前日指标对比
#浏览次数对比
${Browse_Count_ratio} Evaluate ${Yesterday_Browse_Count}/${Last_Browse_Count}
${Browse_Count_ratio_float} Evaluate float(${Browse_Count_ratio} )
Should Be True 0.7<${Browse_Count_ratio_float}<1.3
sleep 2
#用户数对比
${User_Count_ratio} Evaluate ${Yesterday_User_Count}/${Last_User_Count}
${User_Count_ratio_float} Evaluate float(${User_Count_ratio} )
Should Be True 0.7<${User_Count_ratio_float}<1.3
sleep 2
#次均访问拆分成分、秒
${Yesterday_Minutes} Evaluate re.findall(r'\\d+', '${Yesterday_Averagevisit_Time}')[0] re
${Yesterday_Seconds} Evaluate re.findall(r'\\d+', '${Yesterday_Averagevisit_Time}')[1] re
${Last_Minutes} Evaluate re.findall(r'\\d+', '${Last_Averagevisit_Time}')[0] re
${Last_Seconds} Evaluate re.findall(r'\\d+', '${Last_Averagevisit_Time}')[1] re
#时长统一转换成秒数
${Seconds_1} Evaluate ${Yesterday_Minutes}*60+${Yesterday_Seconds}
${Seconds_2} Evaluate ${Last_Minutes}*60+${Last_Seconds}
#次均访问时长对比
${Seconds_ratio} Evaluate ${Seconds_1}/${Seconds_2}
${Seconds_ratio_float} Evaluate float(${Seconds_ratio} )
Should Be True 0.7<${Seconds_ratio_float}<1.3

 

 

 

第二种是导入RobotFramework 自定义关键字(库文件)

 

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2021-05-16
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-10-27
  • 2021-09-30
  • 2021-09-27
相关资源
相似解决方案