Basic use of Python Appium Android automated testing-Phone Spider

Basic use of Python Appium Android automated testing

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
For example: Chapter 1 Introduction to Python Machine Learning: Use of pandas



Preface

For example: With the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This article introduces the basic content of machine learning.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Environment installation

1.1 Python Pip installation module

pip install Appium-Python-Client -i https://pypi.tuna.tsinghua.edu.cn/simple

1.2 Sdk, Jdk, Appium-desktop network disk download

Link: https://pan.baidu.com/s/19C9fGmoXne8DgfXhrTB2TQ

Extraction code: kgwb

1.3 Reference documentation

https://www.byhy.net/tut/auto/appium/01/

2. Android Sdk uiautomateviewer analysis & positioning App interface elements

2.1 Startup steps

Find tools/bin/uiautomatorviewer.bat in the SDK directory and double-click to run;
Insert image description here
Insert image description here

2.2 Frequently Asked Questions

Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

2.2.1 Solution 1:

Usually it conflicts with Appium Desktop Appium. Just close Appium Desktop Appium and get the app interface again;

2.2.2 Solution 2:

Run the command panel:
After running adb reconnect, click Connect again.

2.3 resource-id attribute analysis is often used for element positioning

resource-id=tv.danmaku.bili:id/search_src_text, you can use search_src_text to locate the element ID, or you can write full ( tv.danmaku.bili:id/search_src_text) positioning;

from selenium.webdriver.common.by import By
driver.find_element_by_id('search_src_text')
driver.find_element(By.ID, 'search_src_text')

2.4 bounds attribute analysis can be used for element positioning

bounds = [175,88][887,171], indicating the width and height of the upper left corner of an element and the width and height boundary pixel positions of the lower right corner of an element;

2.5 text attribute analysis can be used for text judgment

text=appium, the content inside the component;

2.6 content-desc attribute analysis text describing the function of the component

Elements can be positioned based on text

from appium.webdriver.common.appiumby import AppiumBy
driver.find_element_by_accessibility_id('搜索查询')
driver.find_element(AppiumBy.ACCESSIBILITY_ID, '搜索查询')

2.7 xpath element positioning

For specific operation introduction, please view in Part 3, Appium Desktop Appium;

2.8 UiSelector element positioning, multi-condition positioning

Refer to the official Google Android documentation here: https://developer.android.google.cn/training/testing/ui-automator

UiSelector commonly used element selection methods

  • text("search query"), you can find element positioning based on the element's text attribute
  • textContains("python"), positioning based on what strings the text contains
  • textmartch(), you can use regular expressions to locate
  • className("android.widget.TextView"), positioned based on component name
  • resourceId("tv.danmaku.bili:id/search_src_text"), positioned based on component ID
  • index(1), positioning based on the position subscript in the parent element, the subscript starts from 1, similar to *[n] in xpath;
  • instance(0), the value is determined based on the subscript in the positioned element result, and the subscript is calculated from 0;
  • childSelector(new UiSelector().className("android.widget.TextView")), continue to locate child elements in the positioned element;
from appium.webdriver.common.appiumby import AppiumBy
code = 'new UiSelector().text("搜索查询").className("android.widget.TextView").resourceId("tv.danmaku.bili:id/search_src_text")'
result = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, code)
childSelectorCode = 'new UiSelector().resourceId("tv.danmaku.bili:id/search_src_text").childSelector(new UiSelector().className("android.widget.TextView"))'
result = driver.find_elements_by_uiautomator(childSelectorCode);

注意上边的code变量,里面写的是Java代码,Java代码只能使用双引号,所以在编写的时候多多注意;

3. Appium Desktop Appium analyzes & locates App interface elements

3.1 Startup steps

1. Open Appium, click Start Server, start the program, click the magnifying glass in the upper right corner
Insert image description here
2. Add configuration

# python代码需要使用到的配置
desired_caps = {
    
    
  'platformName': 'Android',  # 被测手机是安卓
  'platformVersion': '10',  # 手机安卓版本
  'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
  'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
  'appActivity': '.MainActivityV2',  # 启动Activity名称
  'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
  'resetKeyboard': True,  # 执行完程序恢复原来输入法
  'noReset': True,  # 不要重置App
  'newCommandTimeout': 6000,  # 最大请求超时的时间
  'automationName' : 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}
// Appium Desktop  Appium 捕获app界面元素配置
{
    
    
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "xxx",
  "appPackage": "tv.danmaku.bili",
  "appActivity": ".MainActivityV2",
  "unicodeKeyboard": true,
  "resetKeyboard": true,
  "noReset": true,
  "newCommandTimeout": 6000,
  "automationName": "UiAutomator2"
}

Insert image description here

3.2 Get interface elements

Insert image description here

3.3 Search and locate elements based on xpath syntax in Appium Desktop Appium

1. Positioning based on class needs to be written in full: //android.widget.TextView
2. Positioning a component based on resource-id: //*[@resource-id="tv.danmaku.bili:id/action_search"]
3. Relative positioning based on element nodes: positioning 热门elements;

Positioning syntax: //*[@resource-id='tv.danmaku.bili:id/tabs']//android.view.ViewGroup[3]

from appium.webdriver.common.appiumby import AppiumBy
driver.find_element(AppiumBy.XPATH, "//*[@resource-id='tv.danmaku.bili:id/tabs']//android.view.ViewGroup[3]")
driver.find_element_by_xpath("//*[@resource-id='tv.danmaku.bili:id/tabs']//android.view.ViewGroup[3]")

4. Positioning precautions

4.1 element & elements

下面的解释不只是针对xpath定位方法,也包含所有带element字眼的方法
find_element_by_xpath, without s, means locating the first matched one, which is generally used for positioning;
find_elements_by_xpath, with s, means locating all the matched elements, and is generally used to obtain the data value of the element;

5. Python regular code

from appium import webdriver
from selenium.webdriver.common.by import By
from appium.webdriver.extensions.android.nativekey import AndroidKey


"""
    获取app activity信息
    打开目标app至手机的当前活动屏幕中, 打开cmd 输入下面的命令 获取手机正在运行的app信息
        adb shell dumpsys activity recents | find "intent={"
    第一行就是手机当前app的信息、提取cmp的信息,.MainActivityV2
        cmp=tv.danmaku.bili/.MainActivityV2
    
"""

desired_caps = {
    
    
  'platformName': 'Android',  # 被测手机是安卓
  'platformVersion': '10',  # 手机安卓版本
  'deviceName': 'xxx',  # 设备名,安卓手机可以随意填写
  'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
  'appActivity': '.MainActivityV2',  # 启动Activity名称
  'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
  'resetKeyboard': True,  # 执行完程序恢复原来输入法
  'noReset': True,  # 不要重置App
  'newCommandTimeout': 6000,
  'automationName': 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}

# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

# 设置缺省等待时间
driver.implicitly_wait(5)

# 如果有`青少年保护`界面,点击`我知道了`
iknow = driver.find_elements(By.ID, "text3")
if iknow:
    iknow.click()

# 根据id定位搜索位置框,点击
driver.find_element(By.ID, 'expand_search').click()

# 根据id定位搜索输入框,点击
sbox = driver.find_element(By.ID, 'search_src_text')
sbox.send_keys('白月黑羽')
# 输入回车键,确定搜索
driver.press_keycode(AndroidKey.ENTER)

# 选择(定位)所有视频标题
eles = driver.find_elements(By.ID, 'title')

for ele in eles:
    # 打印标题
    print(ele.text)

input('**** Press to quit..')
driver.quit()

Guess you like

Origin blog.csdn.net/EXIxiaozhou/article/details/132344880