from appium import webdriver #导入appium库
from time import *
cs={ ‘platformName’:‘Android’,#平台系统: Android or los
‘deviceName’:‘vivo X9i’,#设备名称vivo x9i
‘platformVersion’:‘android-4.4W’, #平台版本
‘appPackage’:‘com.baidu.yuedu’,#包名
‘appActivity’:‘com.baidu.yuedu.splash.SplashActivity’, #activity
‘unicodeKeyboard’: True, #使用unicode编码方式发送字符串
}
d=webdriver.Remote(‘http://127.0.0.1:4723/wd/hub’,cs)
sleep(15)
#d.find_element_by_id(“com.baidu.yuedu:id/iv_book_store_icon”).click() #id地位
#d.find_element_by_class_name(“android.widget.ImageView”).click() #class定位
#d.find_element_by_xpath(“//android.widget.ImageView[@index=‘0’]”).click() #xpath定位
#d.find_element_by_android_uiautomator(‘new UiSelector().text(“精选”)’).click() #文本定位
d.find_element_by_android_uiautomator(‘new UiSelector().textContains(“精”)’).click() #包含部分文本内容定位
d.find_element_by_class_name(“android.widget.TextView”)[1].click()
============================================================
案例1:
准备工作:aapt d badging D:\app\Yshop.apk
查看包名:uni.UNIC7A519E
活动路径名:io.dcloud.PandoraEntry
或者
(1)安装包:
adb install D:\app\墨迹天气和知乎apk\com.moji.mjweather\base.apk
(2)查看:包名和活动路径名
命令:aapt d badging D:\app\墨迹天气和知乎apk\com.moji.mjweather\base.apk
包名:com.moji.mjweather
活动路径名:'com.moji.mjweather.LauncherActivity
(3)修改代码
from appium import webdriver #f导入appium模块
from time import *
cs={'platformName':"Android",
'deviceName':'vivo X9i ',
'platformVersion':'android-4.4W',
'appActivity':'com.moji.mjweather.LauncherActivity',
'appPackage':'com.moji.mjweather',
'unicodeKeyboard': True }
dx=webdriver.Remote('http://127.0.0.1:4723/wd/hub',cs)
sleep(10)
dx.find_element_by_android_uiautomator('new UiSelector().text("北京市")').click()
1.UiSelector 类介绍
功能:通过各种属性与节点关系定位组件
操作步骤:找到对象->操作对象
2.四种匹配关系的介绍
(1)完全匹配
(2)包含匹配
(3)正则匹配
(4)起始匹
例子:完全匹配:应用
通过文本定位
new UiSelector().text(“text文本”)
通过 textContains 模糊匹配
new UiSelector().textContains(“text文本”)
通过某个文本开头匹配
new UiSelector().textStartWith(“text文本”)
正则表达式匹配
new UiSelector().textMatches(“text文本”)
案例2:
import unittest
from appium import webdriver
from time import sleep
from time import strftime
from UI.HTMLTestRunner3_New import HTMLTestRunner
class Cms(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print(“测试开始”)
@classmethod
def tearDownClass(cls) -> None:
print(“测试结束”)
def setUp(self) -> None:
desired_caps = {
‘platformName’: ‘Android’, # 平台系统:Android or Ios
‘deviceName’: ‘OPPO-R17’, # 设备名称
‘platformVersion’: ‘android-4.4W’, # 平台版本
‘appPackage’: ‘com.moji.mjweather’, # 包名
‘appActivity’: ‘com.moji.mjweather.LauncherActivity’, # activity
‘unicodeKeyboard’: True, # 使用unicode编码方式发送字符串
‘resetKeyboard’: True # 是将键盘隐藏起来
}
self.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub’, desired_caps)
sleep(15)
def tearDown(self) -> None:
self.driver.quit()
sleep(2)
def test1_cxtq(self):
self.driver.find_element_by_id(‘com.moji.mjweather:id/iv_add_city’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/b2t’).click()
sleep(5)
self.driver.find_element_by_id(‘com.moji.mjweather:id/azz’).send_keys(‘温州’)
sleep(5)
self.driver.find_element_by_id(‘com.moji.mjweather:id/akv’).click()
sleep(5)
def test2_txcs(self):
self.driver.find_element_by_id(‘com.moji.mjweather:id/iv_add_city’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/b2u’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/af1’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/b8s’).click()
sleep(6)
def test3_delcs(self):
self.driver.find_element_by_id(‘com.moji.mjweather:id/iv_add_city’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/b2u’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/aes’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/aev’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/b8s’).click()
sleep(6)
def test4_cssj(self):
self.driver.find_element_by_android_uiautomator(‘new UiSelector().text(“时景”)’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/dmg’).click()
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/a0o’).send_keys(‘温州’)
sleep(6)
self.driver.find_element_by_id(‘com.moji.mjweather:id/d_2’).click()
sleep(6)
def test5_csmn(self):
self.driver.find_element_by_android_uiautomator(‘new UiSelector().text(“时景”)’).click()
sleep(6)
self.driver.find_element_by_android_uiautomator(‘new UiSelector().text(“穿搭”)’).click()
sleep(6)
if name == ‘main’:
lj=r"D:\py\UI"
d=unittest.TestLoader().discover(start_dir=lj,pattern=“appz*”)
bglj=r"D:\py\UI"
new=strftime(“%y-%m-%d-%H-%M-%S”)
file=bglj+‘/’+new+‘unittest.html’
f=open(file,‘wb’)
r=HTMLTestRunner(
stream=f, title=‘app自动化报告’, description=‘多测师运行用例’,
tester=‘zhw’
)
r.run(d)
====================================================