[Python] AppUI automation—appium automated development environment deployment, APP test case (17)

1. Introduction to appium

1.What is appium

  • Appium is a cross-platform mobile terminal automation testing tool that can create automated test cases for IOS and Android platforms. It can simulate various operations inside the App, such as clicking, sliding, text input, etc. Appium can complete all the actions we perform manually.

  • appium supports multiple platforms and multiple languages

    • Appium uses standard automation APIs across different platforms, so there is no need to recompile or modify your own applications when crossing platforms.

    • appium supports all languages ​​supported by Selenium WebDriver, 如java、Object-C、JavaScript、Php、Python、Ruby、C#、Clojure,或者Perl语言, and you can also use the API of Selenium WebDriver.

    • appium supports multiple platforms, including MAC 和 Windows. It was developed for these two major platforms appium-Server.

    • It also supports Android 和 iOS two operating systems at the same time. This makes appium very flexible.

2. How appium works

Insert image description here

  1. The automation script sends registration parameters (URL, platformName, platformVersion, deviceName, appPackage, appActivity to AppiumServer )

  2. AppiumServerGeneralbootstrap.jar包release手机端medium

  3. Mobile terminal usebootstrapStart手机端口(4724) and return the port toAppiumServer(equivalent to established通信连接)

  4. Automation scriptSends a request (operation or command or action) toAppiumServer

  5. AppiumServerwill请求解析and send it tobootstrap

  6. bootstrapSend request analysis toUIAutomator

  7. UIAutomator performs the corresponding operation and returns the result tobootstrap

  8. bootstrap returns the response results toAppiumServer

  9. AppiumServerReturn response results to script

To sum up, AppiumServer is like 请求者; bootstrap plays the role of 桥梁 and is responsible for PC与电脑之间的通信; UIAutomator is the real executor .

3.APP type

Insert image description here

Insert image description here

4.APP page layout

Similar to HTML, pages are composed of individual tags
Insert image description here
Insert image description here

2. Appium development environment deployment (python environment)

1. Download and install the environment

1.1. Download and install the required environment

Since appium runs based on the JDK+SDK environment, if you want to use python+appium for script development normally, you need to deploy the following environment

Through the above settings, we have the requirements for using appium for python automated testing.

1.2. Appium-desktop ( Appium-Server-GUI ) configuration

  • It can be installed by default, no special settings are required, and the appium icon on the desktop will be launched after installation.
  • Appium-desktop configures the Android SDK installation directory and Java JD installation directory
    Insert image description here
    startServer starts Appium-desktop: listens to the local 4723 port to initiate a request
    Insert image description here

1.3.Appium-Inspector configuration

  1. Remote Path:/wd/hub
  2. Advanced Settings: checked Allow Unauthorized Certificates, unchecked Use Proxy
    Insert image description here

startSession starts Appium-Inspector

Insert image description here

Insert image description here

1.4. Element detection tools

  • is used to locate APP elements. For example, if you need to click a button, you need to get the element of the button to operate it (click, double-click, etc.) , recommended here UseUIAutomatorViewer.
    Insert image description here

1.5.Appium function introduction

Simple: simple mode

  • Edit Configurations: You can configure the environment addresses of android sdk and Java sdk. If the environment has been configured, the default address will be used.
    Insert image description here

Advanced: Advanced mode
Insert image description here

Insert image description here
Insert image description here

  • A certain person has id,元素id值是唯一的, therefore可用id去进行定位发送秘钥对文本框进行输入
    Insert image description here

2.The relationship between appnium-python-client and selenium

  • There is a certain version dependency between the Appium Python Client module and Selenium.

    • appium is like a service, appium-python-client is 让python连接appium服务的一个驱动, which is python语言封装和appium api通讯的一个库.

    • appium has inherited selenium的webdriver,因此appium-python-client安装会更新selenium。

      查看selenium和appium-python-client的版本
      	pip show selenium
      	pip show appium-python-client
      
      卸载当前的selenium和appium-python-client
      	pip uninstall selenium
      	pip uninstall appium-python-client
      
      安装指定版本的appium-python-clien
      #安装appium-python-client的同时,系统会安装与之相匹配的selenium版本
      	pip install appium-python-client==2.11.0
      

3.Appium automated code writing steps

  • Python3
  • Java8
  • Android SDK v2.4.1
  • Appium v2.2.1

1. Mobile phone operation (taking Redmi mobile phone as an example)

  1. Enter [Developer Mode]

    • [Settings] - [My Device] - [All Parameters] - [MIUI Version] Click 5 times to turn on the developer mode
      Insert image description here
      Insert image description here
  2. In the developer options, turn on [usb debugging] and [usb installation]
    Insert image description here

2. Computer operation

android-sdk-tools environment construction

  • Installationinstaller_r24.4.1-windows.exe, be sure to see [java路径] before proceeding to the next step
    Insert image description here
    Insert image description here

  • Extractbuild-tools.zip 、tools.zip 、platform-tools.zip to the installation directory【不要文件夹套着文件夹
    Insert image description here

  • Open the installation directorySDK Manager.exe and viewplatform-tools和build-tools是否已安装
    Insert image description here

  • Check the android version of your phone, and install the corresponding android version inSDK Manager
    Insert image description here

4. Use appium for APP testing:

1. Mobile phone operation (taking Redmi mobile phone as an example)

  • Enter [Developer Mode]
    Please see step 1 and will not repeat them again

  • Turn on USB debugging and installation
    Insert image description here

  • Mobile phone connected to computer via data cable

2. Computer operation (take the calculator that comes with the mobile phone as an example)

  1. Open appium, click Edit Configuration, and change the Android SDK/JDK paths as shown in the picture to your own paths.
    Insert image description here

    • Save changes and click to start the server
  2. Open Appium Inspector to configure the parameters required to connect the app
    Insert image description here

    • The items shown in the figure are:
      {
              
              
        "platformName": "Android",  # 机操作系统,如 iOS, Android, 或者 FirefoxOS (必填)
        "platformVersion": "9",  #手机版本 (必填)
        "deviceName": "FJH7N19xxx00457",   #设备编码,不会进行校验,但是没有会报错 (必填)
        "appPackage": "com.android.calculator2",    #运行的 Android 应用的包名 (必填)
        "appActivity": "com.huawei.calculator.Calculator",   #程序入口类名 (必填)
        "noReset": true   #在当前 session 下不会重置应用的状态。默认值为 false(非必填)
      }
      
    • Get [device name] command line input [adb devices],
      Insert image description here
    • Take [程序包名、程序入口类名]
      1. 关闭手机所有后台程序
      2. . Open the calculator-application on your phone
      3. Enter【adb shell "dumpsys window | grep mCurrent】 in the command window
        Insert image description here

      如果上面命令正确启动报错,说明当前的appActivity设置的不是app首次进入的appActivity,可以解析apk文件

      • aapt dump badging apk package path |findstr package
      • aapt dump badging F:\my_tools\DigiFinex_V2023.10.12_debugT.apk |findstr package
      • aapt dump badging apk package path |findstr activity
      • aapt dump badging F:\my_tools\DigiFinex_V2023.10.12_debugT.apk |findstr activity
        Insert image description here
  3. After configuring the required functions, save the configuration and click to start the session
    Insert image description here

  4. You will find that the mobile phone has automatically jumped to the calculator interface and is displayed on the left side of the appium window on the computer.
    Insert image description here

  • Note:如果手机是第一次连接appium,会提示下载一个软件【Appium Settings】,正常下载安装即可, the installed APP is shown in the red box in the picture

    Insert image description here
    There is no need to download it when you use it again. [Appium Settings] will be displayed in the notification bar of the phone when running, as shown in the red box in the picture
    Insert image description here

  • Use the mouse to click on the mobile calculator interface on the computer appium to record the operations that need to be tested (take testing the multiplication table as an example)

    • First enter [adb shell pm clear 包名] in the DOS window and press enter, which means clearing the cache to prevent the cache from affecting the upcoming test. The return result is [< a i=2>】The cache is cleared successfullySuccess
      Insert image description here

    • Click the eye icon in the red frame to start recording
      Insert image description here

  • Start recording specific operations, taking operation 7*8 as an example
    Insert image description here

    Insert image description here

  • Copy the recorded code to pycharm

    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    from appium.webdriver.common.touch_action import TouchAction
    
    # Appium服务器地址
    appium_server = 'http://127.0.0.1:4723/wd/hub'
    # Appium测试设备配置
    desired_caps = {
          
          
        "platformName": "Android",
        "appium:platformVersion": "9",
        "appium:deviceName": "FJH7N19131000457",
        "appium:appPackage": "com.android.calculator2",
        "appium:appActivity": "com.huawei.calculator.Calculator",
        'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
        'resetKeyboard': True,  # 执行完程序恢复原来输入法
        'noReset': True,  # 不要重置App,如果为False的话,执行完脚本后,app的数据会清空,比如你原本登录了,执行完脚本后就退出登录了
        'newCommandTimeout': 6000
    }
    # 连接Appium服务器
    driver = webdriver.Remote(appium_server, desired_caps)
    driver.implicitly_wait(10)
    # TODO 干掉弹框
    TouchAction(driver).tap(x=777, y=2192).perform()
    
    # 清空计算器
    driver.find_element(AppiumBy.ID, "com.android.calculator2:id/op_clr").click()
    
    for i in range(1, 10):
        for j in range(i, 10):
            # TODO 按下 9
            el1 = driver.find_element(AppiumBy.ID, f'com.android.calculator2:id/digit_{
            
            i}')
            el1.click()
    
            # TODO 按下 乘X
            el2 = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "乘")
            el2.click()
    
            # TODO 按下 8
            el3 = driver.find_element(AppiumBy.ID, f'com.android.calculator2:id/digit_{
            
            j}')
            el3.click()
    
            # TODO 获取 result 结果
            el4 = driver.find_element(AppiumBy.ID, "com.android.calculator2:id/result")
            print(f"{
            
            i} X {
            
            j} = {
            
            el4.text}", end=", ")
    
            # 清除
            el6 = driver.find_element(AppiumBy.ID, "com.android.calculator2:id/op_clr")
            el6.click()
        print(f"\n-----{
            
            i}乘法已完毕-----------")
    
    driver.quit()
    
    • operation result:
      Insert image description here

5. Element positioning and element event operations

[Python] AppUI Automation—appium automated element positioning and element event operations (17) Part 2

Guess you like

Origin blog.csdn.net/qq877728715/article/details/133682495