Detailed explanation and framework construction of appium automated testing

Table of contents

1. Introduction to Appium    

2. Appium actual combat (take the real machine as an example)


1. Introduction to Appium    

       Appium is an open source automated testing tool that supports native, mobile browser-based, hybrid applications on iOS and Android platforms.

1. The benefits of using appium for automated testing

       Appium uses standard automation APIs on different platforms, so there is no need to recompile or modify your own applications when cross-platform.

       Appium supports all languages ​​supported by Selenium WebDriver, such as java, Object-C, JavaScript, Php, Python, Ruby, C#, Clojure, or Perl language, and you can use Selenium WebDriver's Api. Appium supports any kind of testing framework. Appium realizes the real cross-platform automated testing. (This article mainly introduces the usage of Python)

2. Appium Architecture

      Appium is an HTTP server written in Node.js, which creates and manages multiple WebDriver sessions to interact with different platforms, such as iOS, Android, etc. 

      After Appium starts a test, it will start a server on the device under test (mobile phone) to listen to commands from the Appium server. Each platform like iOS and Android has different ways of running and interacting. Therefore, Appium will use a stub program to "hack" the platform and accept instructions to complete the running of test cases.

      Let's start the topic directly and conduct the actual combat of automated testing on the mobile terminal.

Appium automated testing video: In 2023, you must learn the actual combat of APP automated testing projects_哔哩哔哩_bilibili icon-default.png?t=N4HBhttps://www.bilibili.com/video/BV13g4y1G7QC/?spm_id_from=333.999.0.0

 

2. Appium actual combat (take the real machine as an example)

       aapt dump badging D:\XXX.apk Get all the information of the installation package

       adb devices (check if the phone is connected to the computer)

       adb shell pm list packages: List all package names and find the package name of the viewed package.

       adb shell dumpsys package com.android.XXX: View the specific information of a package

       other:

       adb devices: Check whether the Android device is connected to the computer.

       adb shell dumpsys activity: check which activity is currently running, some running processes, etc.

       adb shell dumpsys activity activities

       adb shell pm list packages: List all package names.

       adb shell dumpsys package: List all installed application information

       adb shell dumpsys package com.android.XXX: View the specific information of a package

       adb shell dumpsys activity | grep mFocusedActivity: check which activity is currently resumed

       adb logcat | grep ActivityManager: View the currently running Activity

       adb logcat | grep Displayed: View the currently running Activity

1. Connect to a real device or an emulator (take the real device as an example here)

       When connecting the mobile phone, pay attention to select the developer mode of the mobile phone, open the command line window with cmd, and enter adb devices to obtain the deviceName of the device.

       

2. Get Activity

       cmd to open the command line window, enter aapt dump badging D:\XXX.apk (xxx refers to the installation package name, D:\ refers to the path) to directly view the specific information of the app installation package to be tested.

       

       The marked is the package name, continue to drag down, you can find the activity information.

       

       The red circle here can see the activity information. These two values ​​need to be obtained in advance in the appium script, so this acquisition method needs to be introduced in advance. If you think it looks ugly in cmd, you can export the cmd information to a txt file, for example aapt dump badging shoujibaidu.apk > 123.txt After pressing Enter, a 123.txt file will be generated on the desktop, you can open it by Query to find this information. Through the above method, as long as we have the android SDK environment, then we can get the package name and Activity information of any package.

Appium automated testing:

In 2023, you must learn the actual combat of APP automation testing projects_哔哩哔哩_bilibili icon-default.png?t=N4HBhttps://www.bilibili.com/video/BV13g4y1G7QC/?spm_id_from=333.999.0.0                   

3. Start Appium 

     1) Double-click the appium icon on the desktop to open it, and click Android Settings to set it.

     

     2) Fill in the deviceName of the real device obtained above, and select platformName and platformVersion

     

      3) Configure the session of the service to be rewritable (non-essential configuration)

       

      After configuration, click the start button in the upper right corner to start.

       

        The startup is successful, the next step is to write the code.

4. Write the device and installation package information obtained above into the script

desired_caps = {
    'platformName': 'Android',
    'deviceName':'8TB6V4ZPZ54LPJ5P',
    'platformVersion': '5.1',
    'app': PATH(r'D:\shell_customer-debug.apk'),
    'appPackage': 'com.jyibb.shell_customer',
    'appActivity': 'com.jyibb.module_launch_customer.SplashActivity',    # 'unicodeKeyboard': 'True',#此两行是为了解决字符输入不正确的问题
    # 'resetKeyboard': 'True'   #运行完成后重置软键盘的状态

5. Find app positioning elements

      Start an artifact in the Android SDK to find the positioning elements of the app for scripting. There is a uiautomatorviewer.bat in the tools of the Android SDK, as shown in the following figure:

      

      Click uiautomatorviewer.bat to start. There are four buttons in the upper left corner, which are used to open local files, dump pages, pages in dump compressed format, and save; after startup, click the third icon in the upper left corner to display the real machine interface.

       

      The first connection will be slow, wait patiently, after the connection is completed, it will be fast. Here I take the Baidu interface as an example.

     

     This is the interface display after connecting to the real device. In this way, positioning can be performed based on id, class, text and other positioning elements.

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_id("com.baidu.searchbox:id/baidu_searchbox").click()
driver.find_element_by_id("com.baidu.searchbox:id/SearchTextInput").clear()
driver.find_element_by_id("com.baidu.searchbox:id/SearchTextInput").send_keys('appium测试')

driver.find_element_by_id("float_search_or_cancel").click()
driver.find_element_by_id("floating_action_button").click()

driver.quit()

3. The complete code is directly below, taking my own app as an example

#coding:utf-8

from appium import webdriver
import time,os

PATH = lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))

#设备及安装包信息
desired_caps = {
    'platformName': 'Android',
    'deviceName':'8TB6V4ZPZ54LPJ5P',
    'platformVersion': '5.1',
    'app': PATH(r'D:\shell_customer-debug.apk'),
    'appPackage': 'com.jyibb.shell_customer',
    'appActivity': 'com.jyibb.module_launch_customer.SplashActivity',
    # 'unicodeKeyboard': 'True',
    # 'resetKeyboard': 'True'
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(5)#等待时间可根据自己的情况进行添加

driver.find_element_by_id("com.jyibb.shell_customer:id/layout_main_header_mine").click()
time.sleep(5)

driver.find_element_by_id("com.jyibb.shell_customer:id/login_phone_number_p").send_keys('15000000000')
driver.find_element_by_id("com.jyibb.shell_customer:id/login_password_p").click()
driver.find_element_by_id("com.jyibb.shell_customer:id/login_password_p").send_keys("111111q")

time.sleep(5)
driver.find_element_by_id("com.jyibb.shell_customer:id/bt_login_p").click()

      At this point, run the script to perform automated testing.

  Appium automated testing:

In 2023, you must learn the actual combat of APP automation testing projects_哔哩哔哩_bilibili icon-default.png?t=N4HBhttps://www.bilibili.com/video/BV13g4y1G7QC/?spm_id_from=333.999.0.0

 

 

Guess you like

Origin blog.csdn.net/MXB1220/article/details/130773383