天天看點

robotframework-appiumlibrary 循環連接配接斷開wifi

*** Settings ***
Library           AppiumLibrary

*** Variables ***

*** Test Cases ***
連接配接wifi  #testcase名稱
    [Setup]
    :FOR    ${i}    in range    10   #循環次數為10次
    \    打開設定
    \    sleep    1    #每個操作後建議設定等待時間,避免代碼執行時找不到界面元素
    \    進入wifi搜尋界面
    \    sleep    1
    \    連接配接wifi-johnny50
    \    sleep    3
    \    斷開wifi-johnny50
    \    sleep    2
    \    關閉應用
    [Teardown]

*** Keywords ***
打開設定 #定義關鍵字‘打開設定’
    Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=6.0.1    deviceName=1560ab67    appPackage=com.android.settings    appActivity=MainSettings

進入wifi搜尋界面 #定義關鍵字‘進入wifi搜尋界面’
    Click Element    xpath=//*[contains(@text,'WLAN')]

連接配接wifi-johnny50 #定義關鍵字‘連接配接wifi-johnny50’
    : FOR    ${i}    IN RANGE    20
    \    swipe    849    934    70    482    1000   #注意滑動時x,y坐标是先結束坐标,後開始坐标
    \    sleep    3
    \    ${num}    Get Matching Xpath Count    xpath=//*[contains(@text,'johnny50')] #計算text為johnny50的控件個數,這裡用到關鍵字get matching xpath count
    \    Exit For Loop If    ${num}>0 #當控件個數大于0時停止滑動
    Click Element    xpath=//*[contains(@text,'johnny50')]
    sleep    1
    input text    xpath=//*[contains(@resource-id,'com.android.settings:id/password')]    chenlong0709
    sleep    3
    Click Element    xpath=//*[contains(@text,'連接配接')]

斷開wifi-johnny50
    ${num}    Get Matching Xpath Count    xpath=//*[contains(@text,'點選分享密碼')] #使用關鍵字get matching xpath count來判斷wifi是否已連上
    Run Keyword If    ${num}>0    log    wifi had connected
    Run Keyword If    ${num}>0    click element    xpath=//android.widget.TextView[@text='點選分享密碼']/../following-sibling::android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ImageView
    sleep    1
    :FOR    ${i}    in range    20
    \    swipe    0    666    0    352
    \    sleep    1
    \    ${num1}    Get Matching Xpath Count    xpath=//*[contains(@text,'删除網絡')]
    \    Exit For Loop If    ${num1}>0
    sleep    1
    Click Element    xpath=//*[contains(@text,'删除網絡')]
    Click Element    xpath=//*[contains(@text,'确定')]

關閉應用
    Close Application