RobotFramework 截取中文中的数字比较时长
作者:互联网
先看下需求,这个报表中有个时长,需要对昨日和前日的时长进行比较,我们获取到的元素是例如“9分43秒”这样的格式
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
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}
注意此处需要用\\可以避免转义,一个\的时候,在使用该工具时被转义了,会出现如下错误
正确的写法
整体代码:
*** 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=//*[@id="loginKey"] wsjun
Input Text xpath=//*[@id="password"] xcep7879
Click Button xpath=//*[@id="submitLoginBtn"]
Sleep 2
Click Element xpath=//*[@id="app"]/div[1]/div[2]/ul/li[2]
Sleep 2
Click Element xpath=//*[@id="app"]/div[2]/div/div[1]/div/div/div[1]/div/div/div[1]
Sleep 2
Input Text xpath=//*[@id="app"]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/input 浙江新闻
Sleep 2
Click Element xpath=//*[@id="app"]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/div/span/div[1]/ul/li/div/p[1]
Sleep 2
#点击访问分析
Click Element xpath=//*[@id="app"]/div[2]/div/div[1]/div/div/div[2]/ul/div[6]/li/div
Sleep 2
#点击页面分析
Click Element xpath=//*[@id="app"]/div[2]/div/div[1]/div/div/div[2]/ul/div[6]/li/ul/li/ul/li[1]/a
sleep 2
#点击分析图表
Click Element xpath=//*[@id="app"]/div[2]/div/div[2]/div/div/div/div/div[4]/div/div[2]/div[2]/div[1]/div/div[1]/div/div[2]
Wait Until Element Is Visible xpath=//*[@id="app"]/div[2]/div/div[2]/div/div/div/div/div[4]/div/div[2]/div[1]/div/div/button
sleep 5
#鼠标点击昨日坐标位置
Click Element At Coordinates xpath=//*[@id="lineandbarcharts"]/div[1]/canvas 400 50
sleep 5
#获取昨日浏览次数、用户数、次均访问时长
${Yesterday_Browse_Count} get Text xpath=//*[@id="lineandbarcharts"]/div[2]/div[1]/div
sleep 2
${Yesterday_User_Count} get Text xpath=//*[@id="lineandbarcharts"]/div[2]/div[2]/div
sleep 2
${Yesterday_Averagevisit_Time} get Text xpath=//*[@id="lineandbarcharts"]/div[2]/div[3]/div
sleep 5
#鼠标点击前日坐标位置
Click Element At Coordinates xpath=//*[@id="lineandbarcharts"]/div[1]/canvas 250 50
sleep 5
#获取前日日浏览次数、用户数、次均访问时长
${Last_Browse_Count} get Text xpath=//*[@id="lineandbarcharts"]/div[2]/div[1]/div
sleep 2
${Last_User_Count} get Text xpath=//*[@id="lineandbarcharts"]/div[2]/div[2]/div
sleep 2
${Last_Averagevisit_Time} get Text xpath=//*[@id="lineandbarcharts"]/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 自定义关键字(库文件)
标签:xpath,中文,Evaluate,截取,Yesterday,RobotFramework,re,div,id 来源: https://www.cnblogs.com/wushujun/p/11775730.html