欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Robot Framework Appium案例

程序员文章站 2024-03-22 16:05:40
...

1、验证Android的版本信息

示例代码

*** Settings ***
Suite Setup       Open Settings App
Library           AppiumLibrary



*** Variables ***
${EXPECTED_ANDROID_VERSION}    8.1.0
${EXPECTED_MODEL_NUMBER}    Nexus 6P

*** Test Cases ***
Test Case 1: Model Number Test
    [Tags]    System Information
    Verify Model Number    ${EXPECTED_MODEL_NUMBER}

Test Case 2: Android Version Test
    [Tags]    System Information
    Verify Android Version    ${EXPECTED_ANDROID_VERSION}


   

*** Keywords ***
Open Settings App
    Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=8.1.0    deviceName=dev_name    appPackage=com.android.settings    appActivity=.Settings
    Sleep    2s
    Open About phone page

Open About phone page

    Log    Step 1: Scroll down until find "System"
    : FOR    ${i}    IN RANGE    20
    \    Swipe    400    1000    400    500    200
    \    ${count}    Get Matching Xpath Count    xpath=//*[contains(@text, 'System')]
    \    Exit For Loop If    ${count}>0
	
    Log    Step 2: Click the "System" Label
    Click Element    xpath=//*[contains(@text, 'System')]

    Log    Step 3: Scroll down until find "About phone"
    : FOR    ${i}    IN RANGE    20
    \    Swipe    400    1000    400    500    200
    \    ${count}    Get Matching Xpath Count    xpath=//*[contains(@text, 'About phone')]
    \    Exit For Loop If    ${count}>0
	
    Log    Step 4: Click the "About Phone" Label
    Click Element    xpath=//*[contains(@text, 'About phone')]
	
    Log    Step 3: Scroll down until find "Build number"
    : FOR    ${i}    IN RANGE    20
    \    Swipe    400    1000    400    500    200
    \    ${count}    Get Matching Xpath Count    xpath=//*[contains(@text, 'Build number')]
    \    Exit For Loop If    ${count}>0


Verify Android Version
    [Arguments]    ${expected_android_version}
    Log    Verify the Page if contains the right Android Version
    Page Should Contain Text    ${expected_android_version}    INFO
	Press Keycode    4    None
 

Verify Model Number
    [Arguments]    ${expected_Model_Number}
    Log    Verify the Page if contains the right model number
    Page Should Contain Text    ${expected_Model_Number}    INFO

示例效果

Robot Framework Appium案例

2、Camera测试

示例代码

*** Settings ***
Library           AppiumLibrary

*** Test Cases ***
open camera
    Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=8.1.0    deviceName=dev_name    appPackage=com.google.android.GoogleCamera    appActivity=com.android.camera.CameraActivity
    Sleep    2s
    Click Element    xpath=//*[contains(@text, 'ALLOW')]
    Sleep    2s

take phone
    Click Element    xpath=//*[contains(@resource-id, 'id/shutter_button')]

示例效果
Robot Framework Appium案例

3、WiFi测试

示例代码

*** Settings ***
Suite Setup       Open Settings App
Library           AppiumLibrary

*** Variables ***
${SSID_NAME}      vivo
${PASSWORD}       88888888

*** Test Cases ***
Test Case 1: Turn On WiFi
    [Tags]    WiFi Test
    Turn On WiFi

Test Case 2: Connect WiFi
    [Tags]    WiFi Test
    Connect WiFi    ${SSID_NAME}    ${PASSWORD}

*** Keywords ***
Open Settings App
    Open Application    http://localhost:4723/wd/hub    platformName=Android    platformVersion=8.1.0    deviceName=dev_name    appPackage=com.android.settings    appActivity=.Settings
    Sleep    2s
    Open WiFi Setting Page

Open WiFi Setting Page
    Log    Click the "Wi-Fi" Label
    Wait Until Page Contains Element    xpath=//*[contains(@text, 'Network')]    10    Can NOT find "Network" label
    Click Element    xpath=//*[contains(@text, 'Network')]

Turn On WiFi
    Wait Until Page Contains Element    xpath=//*[contains(@resource-id, 'id/switch_widget')]
    ${wifi_status} =    Get Element Attribute    xpath=//*[contains(@resource-id, 'id/switch_widget')]    text
    Run Keyword If    '${wifi_status}' != 'ON'    Click Element    xpath=//*[contains(@resource-id, 'id/switch_widget')]
	Sleep    2s
	Click Element    xpath=//*[contains(@text, 'Wi')]

Connect WiFi
    [Arguments]    ${ssid_name}    ${password}
    Log    Step 1: Click the SSID-Name ${ssid_name}
    Wait Until Page Contains Element    xpath=//*[contains(@text, '${ssid_name}')]    10    Can NOT find ${ssid_name}
    Click Element    xpath=//*[contains(@text, '${ssid_name}')]
    Sleep    2s
    Log    Step 2: Check if the AP ${ssid_name} already connected, if connected then disconnect
    ${count}    Get Matching Xpath Count    xpath=//*[contains(@text, 'Forget')]
    Run Keyword If    ${count} > 0    Log    WiFi already connected, forget it then re-connect
    Run Keyword If    ${count} > 0    Click Element    xpath=//*[contains(@text, 'Forget')]
    Run Keyword If    ${count} > 0    Sleep    2s
    Run Keyword If    ${count} > 0    Click Element    xpath=//*[contains(@text, '${ssid_name}')]
    Run Keyword If    ${count} > 0    Sleep    2s
    Log    Step 3: Input the password then connect.
    Wait Until Page Contains Element    xpath=//*[contains(@resource-id, 'id/password')]    10    Can NOT find "password" text
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
	Press Keycode    15    None
    Sleep    1s
    Click Element    xpath=//*[contains(@text, 'CONNECT')]
    Log    Step 4: Check if the WiFi connected sucesfully or not.
    Wait Until Page Contains    Connected    10    The device does NOT connect to the Access Point ${ssid_name} yet

示例效果

Robot Framework Appium案例

4、遇到问题

问题1. Press Keycode 15 None
No keyword with name ‘Press Keycode 15’ found. Did you mean:
AppiumLibrary.Press Keycode

Press Keycode 15 None
直接改代码时,关键词空格格式不对

问题2.
Input Value xpath=//[contains(@resource-id, ‘id/password’)] ${password}
Input Text xpath=//
[contains(@resource-id, ‘id/password’)] ${password}
Cannot set the element to ‘88888888’. Did you interact with the correct element?

input 失败,改为按键输入

相关标签: Android