(Appium + python) UI Automation Automation _07_UI example [for example] to fight a lot to find a product

Foreword

UI Automation beginner's junior partner, after configuring appium + python automation environment, often do not know how to start automated. Greatly accelerates the time of the initial study have this doubt, to fight a lot in this search as an example to show how under appium is to automate.

Prerequisite: already installed and configured appium + python automation environment

First, connect your phone to start app

1, connect the phone

      - USB phone connected to the computer

      - Open developer mode mobile phones, USB debugging function

2, basic configuration information

Basic connection information is as follows (in micro-channel app example):

'platformName': platform 
'deviceName': Device name
'platformVersion': system version number
'appPackage': apk package name
'appActivity': APK Activity
'NoReset': Do not reset prior to the session state of the application, ie non-initialized . Parameter values: true, false
More information is available in the configuration parameters: https://www.cnblogs.com/D666/p/9165086.html

Note: No acquisition device / app package name / activity details to view  https://www.cnblogs.com/mini-monkey/p/11691862.html

3, start appium service

1, start the service address acquisition appium appium

appium start page for host and port, the default configuration host: 127.0.0.1, port: 4723,

即默认appium服务地址为:http://127.0.0.1:4723/wd/hub(若配置其他host&port更新http://后内容为host:port即可)

4,连接设备,启动app

 Remote括号中地址:appium服务地址(步骤3中获取)

二、编写搜索脚本

1,打开定位工具uiautomatorviewer,查看元素属性

关于定位工具的可参考:https://www.cnblogs.com/mini-monkey/p/11819549.html

点击Android adk->tools->automatorviewer

鼠标选中页面元素,右下角查看元素属性

 2,定位元素

元素定位方法可参考:https://www.cnblogs.com/mini-monkey/articles/11836650.htmlhttps://www.cnblogs.com/mini-monkey/articles/11836690.html

appium常用API可参考:https://www.cnblogs.com/mini-monkey/articles/11841354.htmlhttps://www.cnblogs.com/mini-monkey/articles/11841370.html

一般元素id存在时,先根据id定位,其次是class,然后再是xpath、uiautomator等去定位。优先级不一定是这样的,小编一般习惯这样定位,大家可以根据自己的喜好选择定位方式。

拼多多搜索商品过程如下:

首页点击搜索tab->点击搜索框->输入搜索关键词,点击搜索btn(进入搜索结果页,展示搜索结果)

实现搜索自动化便可根据以上步骤展开编写,拼多多搜索商品脚本如下(以搜索T恤为例):

备注:由于app有的页面加载需要时间,可在需要等待的地方添加等待时间。

三、实例代码详情

拼多多搜索商品的总代码如下:

#appium拼多多搜索商品实例
from appium import webdriver
from time import sleep

#基础配置
desired_caps = {
'platformName': 'Android', # 平台
'deviceName': "emulator-5554", # 手机设备名称
'platformVersion': "6.0.1", # 安卓系统版本号
'appPackage': 'com.xunmeng.pinduoduo', # apk包名
'appActivity': 'com.xunmeng.pinduoduo.ui.activity.MainFrameActivity', # apk activity
'unicodeKeyboard': True, # 设置编码格式为unicode
'resetKeyboard': True, # 隐藏手机键盘
'noReset': True # 非初始化
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 启动app
sleep(5) #等待淘宝首页加载【app有时加载数据有时需加载几秒,故在需要加载等待的地方可加上等待时间】

driver.find_elements_by_id("com.xunmeng.pinduoduo:id/cne")[2].click() #首页_点击底栏搜索tab
sleep(2) #等待页面跳转
driver.find_element_by_id("com.xunmeng.pinduoduo:id/bf1").click() #点击搜索框进入搜索页
driver.find_element_by_id("com.xunmeng.pinduoduo:id/mr").send_keys("T恤") #输入关键字T恤
sleep(2)
driver.find_element_by_id("com.xunmeng.pinduoduo:id/bfa").click() #点击搜索btn

 

Guess you like

Origin www.cnblogs.com/mini-monkey/p/11854012.html