手机自动化测试:4.通过appium inspector 获取相关app的信息,以某团为例,点击,搜索,获取数据等。

0.使用inspector时,一定要把不相关的如weditor啥的退出去,否则,净是事。

1.从0开始的数据获取

第一个位置,有时0.0.0.0,不可以的话,你就用这个。

第二个位置,抄上。

直接点击第三个启动。不要问为什么,问就是。我也不知道。

第一个错误:它说得有个平台名,好吧。

错误

Failed to create session. An unknown server-side error occurred while processing the command. Original error: You must include a platformName capability

添上

注意一定要添上如下

复制代码
"appium:automationName": "UiAutomator2"

再次启动。OK了进来了。

再次提醒,版本不一样,出错不一样,你根据出错的地方完善就可以。

2.获取某团相关

自动点击手机上的某团,打开它。

一个一个的点复制。

复制代码
'getCurrentActivity' "com.meituan.android.pt.homepage.activity.MainActivity"
'getCurrentPackage' "com.sankuai.meituan"

返回修改

复制代码
{
  "appium:platformName": "Android",
  "appium:automationName": "UiAutomator2",
  "appium:appPackage": "com.sankuai.meituan",
  "appium:appActivity": "com.meituan.android.pt.homepage.activity.MainActivity"
}

其他信息模仿吧。

通过adb获取信息

复制代码
C:\Users\Administrator>adb shell dumpsys activity | findstr "mResume"
    mResumedActivity: ActivityRecord{1fef505 u0 com.sankuai.meituan/com.meituan.android.pt.homepage.activity.MainActivity t1097}

形成测试代码:

复制代码
from appium import webdriver
from appium.options.android import UiAutomator2Options
desired_caps = {
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "Q5X7N19605002672",
  "appium:appPackage": "com.sankuai.meituan",
  "appium:appActivity": "com.meituan.android.pt.homepage.activity.MainActivity",
  "unicodeKeyboard": True,
  "resetKeyboard":True,
   "noReset":True,
  "appium:newCommandTimeout": 6000,
  "appium:automationName": "UiAutomator2"
}

if desired_caps is not None:
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=UiAutomator2Options().load_capabilities(desired_caps))
else:
    raise ValueError("desired_caps must not be None")

运行成功。

用inspector进行附加。

利用代码启动某团,一定要利用pycharm中的代码进行,否则无效,原理很简单。就是通过appium互动留下的信息进行启动的。

再次刷新附加

界面示例:

3.点击某个元素、输入文本、搜索等。

点击超市便利为例:

有一个结果,点击一下。看手机是否正常反映

有反映,记录之

复制代码
(//android.widget.ImageView[@resource-id="com.sankuai.meituan:id/channel_icon"])[5]

输入商店名称进行搜索:以下记得刷新,否则你明白 的。信息不准。

方法同上。

输入框定位后,点击一下。

复制代码
//android.widget.LinearLayout[@resource-id="com.sankuai.meituan:id/minutes_animte_action_search"]/android.widget.LinearLayout[1]

进入新窗口

通过应用源一级级定位到最低级的编辑框位置。输入想要搜索的关键词。

复制代码
//android.widget.EditText[@resource-id="com.sankuai.meituan:id/txt_search_keyword"]

如下图:

定位搜索按钮。

复制代码
//android.widget.LinearLayout[@resource-id="com.sankuai.meituan:id/search_tv"]

来到以下页面。

点击排名第一个商店,进入商店

通过应用源一级级的定位,不再多说。

复制代码
(//android.widget.FrameLayout[@resource-id="com.sankuai.meituan:id/mach_container_wrapper"])[2]

来到如图:

4.根据以上查证结果,生成py文件,进行自动操作。

提醒,以上其实是XPATH定位,不是最推荐的,但目前,用这个就行。

以下代码,完整无伤通过。

复制代码
from appium import webdriver
from appium.options.android import UiAutomator2Options
desired_caps = {
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "Q5X7N19605002672",
  "appium:appPackage": "com.sankuai.meituan",
  "appium:appActivity": "com.meituan.android.pt.homepage.activity.MainActivity",
  "unicodeKeyboard": True,
  "resetKeyboard":True,
   "noReset":True,
  "appium:newCommandTimeout": 6000,
  "appium:automationName": "UiAutomator2"
}


driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=UiAutomator2Options().load_capabilities(desired_caps))

# 设置超时时间和重试间隔
timeout = 10
poll_frequency = 1
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

####################### 点击超市便利########################################################

element = WebDriverWait(driver, timeout, poll_frequency).until(
    EC.element_to_be_clickable((By.XPATH, '(//android.widget.ImageView[@resource-id="com.sankuai.meituan:id/channel_icon"])[5]'))
)
element.click()
######################### 点击搜索框########################################################
element = WebDriverWait(driver, timeout, poll_frequency).until(
    EC.element_to_be_clickable((By.XPATH, '//android.widget.LinearLayout[@resource-id="com.sankuai.meituan:id/minutes_animte_action_search"]/android.widget.LinearLayout[1]'))
)
element.click()
##################### 使用显式等待定位并输入文本############################################################
try:
    search_box = WebDriverWait(driver, timeout, poll_frequency).until(
        EC.presence_of_element_located((By.XPATH, "//android.widget.EditText[@resource-id='com.sankuai.meituan:id/txt_search_keyword']"))
    )
    search_box.send_keys("优小优")
except Exception as e:
    print(f"无法找到或操作元素: {e}")

########################## 点击搜索按钮#########################################################

element = WebDriverWait(driver, timeout, poll_frequency).until(
    EC.element_to_be_clickable((By.XPATH, '//android.widget.LinearLayout[@resource-id="com.sankuai.meituan:id/search_tv"]'))
)
element.click()

########################## 点击排名第一的商店#########################################################

element = WebDriverWait(driver, timeout, poll_frequency).until(
    EC.element_to_be_clickable((By.XPATH, '(//android.widget.FrameLayout[@resource-id="com.sankuai.meituan:id/mach_container_wrapper"])[2]'))
)
element.click()

希望你也能一次无伤通过。

下一节,打算获取其商品数据。并保存之。

相关推荐
returnthem1 天前
安装Appium
appium
seabirdssss6 天前
Appium 在小米平板上的安装受限与闪退排查
android·appium·电脑
小陈的进阶之路10 天前
Selenium 滑动 vs Appium 滑动
python·selenium·测试工具·appium
小陈的进阶之路10 天前
Appium 自动化测试笔记
笔记·appium
linglan42812 天前
APPium环境配置
appium·自动化
lifewange18 天前
Appium是什么
appium·压力测试
柚子+25 天前
Appium+python+雷电模拟器自动化测试入门
数据库·python·appium
@Aurora.1 个月前
【GUI自动化测试】--基于QQ音乐项目的GUI自动化测试
appium
楚轩努力变强2 个月前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化