python3+RobotFramework驗證碼登錄

面試了下自動化測試講師,也面過了,就是感覺自己還沒到能教學生的成度,畢竟長時間不用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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章