python3+RobotFramework验证码登录

发布时间 2023-08-24 13:59:19作者: 社会主义接班人

面试了下自动化测试讲师,也面过了,就是感觉自己还没到能教学生的成都,毕竟长时间不用RobotFramework,自己也还没搞明白,也不想误人子弟,所以就没去。昨天闲着无聊正好测试环境搭建好了,就跑个流程试试。

一、验证码识别

之前做图片验证码识别有用Sikuli库,百度了下,Sikuli好像不更新了,就没再用,想着怎么截图获取验证码图片,刚开始想着使用AutoItLibrary库,用鼠标键盘图片另存为保存验证码,虽然手动操作另存为的时候是图片,但用AutoItLibrary库时另存为变成了html类型,这个问题弄了半天也没解决,后来想着用Selenium2Library库截取屏幕,再通过坐标用python抠图获取验证码图片,思路也是可行的,在找对应关键字的时候找到了Capture Element Screenshot关键字,之前是没这个关键字的,看注释发现是新增的,这个关键字确实不错,可以直接通过这个关键字获取验证码图片,再通过python解析获取验证码。这里涉及RobotFramework自定义关键字,我创建了名为ImageOcr.py的文件,新增ImageOcr类,定义viewClickCode关键字,使用ddddocr识别图片获取验证码。

# -*- coding:utf-8 -*-
#自动化测试图形验证码处理
import ddddocr
from robot.api.deco import  keyword
class ImageOcr():
    ROBOT_LIBRARY_SCOPE="SUITE"
    ROBOT_AUTO_KEYWORDS=False

    def __init__(self):
        return
    @keyword('viewClickCode')
    def viewClickCode(self,imgpath):
        '''点选验证码识别,返回需要点选的坐标'''
        try:
            det = ddddocr.DdddOcr()
            with open(imgpath, 'rb') as f:
                image = f.read()
            poses = det.classification(image)
            return  poses
        except Exception as e:
            print("识别失败:{}".format(e))
            return ''
if __name__ == '__main__':
    a=ImageOcr().viewClickCode('D:\\imgcode\\2.png')
    print(a)
    print(len(a))
    # tagInimg('makecode.png',a)
View Code

将自定义的关键字作为Library引入到Robotframework中

 二、业务流程

这里主要模拟用户打开购物网站,选择商品下单的过程,在登录的时候第一次输入验证码时先输入一个错误的,刷新验证码二次登录的流程。实现的过程中熟悉了下RF中的关键字的使用,主要是xpath、选择器、AutoItLibrary键盘鼠标的使用,算是温故知新,毕竟好久没用了都忘了怎么使用了,废话不多说直接上脚本。

*** Settings ***
Library           Selenium2Library
Library           AutoItLibrary
Library           DateTime
Library           F:/Stock/ImageOcr.py

*** Variables ***
${imgpath}        D:\\imgcode

*** Test Cases ***
Login
    Log    Hello,你好
    Open Browser    https://www.baidu.com/    Google Chrome
    Maximize Browser Window
    Wait Until Element Is Visible    id=s-top-loginbtn
    Click Element    id=s-top-loginbtn
    Sleep    2
    Wait Until Element Is Visible    id=passport-login-pop-api
    Sleep    2
    Input Text    name=userName    18575533040
    Sleep    2
    send    {ENTER}
    send    123456
    Sleep    2
    send    {ENTER}
    Sleep    2

buy
    Open Browser    https://dsmall.csdeshang.com/    Google Chrome
    Maximize Browser Window
    Comment    sleep    2
    click element    class=close-fixed-suspension
    Comment    sleep    2
    Mouse Over    xpath=//li[@cat_id='1']
    Comment    sleep    2
    Wait Until Element Is Visible    xpath=//div[@cat_menu_id='1']
    click element    xpath=//a[contains(text(),'小米')]
    click element    xpath=//a[contains(text(),'小米 红米NOTE 5A智能拍照手机16G/32G/64G')]
    Comment    click element    xpath=//ul[@class='list_pic']/li[@class='item'][3]
    Comment    sleep    2
    Comment    ${win_curr}    Get Window Titles
    Comment    log    ${win_curr}
    ${win_curr}    Switch Window    NEW
    log    ${win_curr}
    Wait Until Element Is Visible    class=dss-buy
    click element    xpath=//a[@class='increase iconfont']
    Mouse Over    id=dss-freight-selector
    sleep    2
    click element    xpath=//li[@data-index='0']
    sleep    1
    click element    xpath=//ul[@class='area-list']/li/a[contains(text(),'河南省')]
    Comment    sleep    1
    Wait Until Element Is Visible    id=stock_city_item
    click element    xpath=//ul[@class='area-list']/li/a[contains(text(),'郑州市')]
    sleep    1
    Wait Until Element Is Visible    id=stock_area_item
    click element    xpath=//ul[@class='area-list']/li/a[contains(text(),'新郑市')]
    sleep    1
    click element    xpath=//a[@class='addcart ']
    sleep    1
    Input Text    id=member_name    buyer
    Comment    sleep    1
    Input Password    id=member_password    123456
    Set Screenshot Directory    ${imgpath}
    ${time}    Get Current Date    UTC
    ${usageUpdateTime}    convert date    ${time}    exclude_millis=yes    result_format=%Y%m%d%H%M%S
    ${filename}    Catenate    ${usageUpdateTime}.png
    Capture Element Screenshot    xpath=//img[@class='fl ml10']    ${filename}
    sleep    1
    ${imgurl}    Catenate    ${imgpath}\\${filename}
    ${code}    viewClickCode    ${imgurl}
    log    ${code}
    Input Text    id=captcha_normal_inajax    1234
    Comment    Input Text    id=captcha_normal_inajax    ${code}
    Comment    Send    {ENTER}
    sleep    1
    click element    name=Submit
    Comment    ${text}    Get Element Attribute    id=captcha_normal_inajax-error    text
    ${IsElementVisible}=    Run Keyword And Return Status    Element Should Be Visible    id=captcha_normal_inajax-error
    set variable    ${code}    ''
    log    ${IsElementVisible}
    sleep    1
    Run Keyword If    '${IsElementVisible}'=='False'    click element    xpath=//img[@class='fl ml10']
    sleep    1
    Run Keyword If    '${IsElementVisible}'=='False'    Capture Element Screenshot    xpath=//img[@class='fl ml10']    ${filename}
    sleep    2
    ${code}=    Run Keyword If    '${IsElementVisible}'=='False'    viewClickCode    ${imgurl}
    log    ${code}
    sleep    2
    Run Keyword If    '${code}'!=''    Input Text    id=captcha_normal_inajax    ${code}
    sleep    1
    Send    {ENTER}
    sleep    5
View Code

 三、效果展示

 https://files.cnblogs.com/files/5ishare/%E8%87%AA%E5%8A%A8%E5%8C%96.rar?t=1692856418&download=true