# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from appium import webdriver
caps = {}
caps["deviceName"] = "127.0.0.1:62001"
caps["platformName"] = "Android"
caps["appPackage"] = "tv.danmaku.bili"
caps["appActivity"] = "tv.danmaku.bili.ui.splash.SplashActivity"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
el2 = driver.find_element_by_id("tv.danmaku.bili:id/app_bar")
el2.click()
el3 = driver.find_element_by_accessibility_id("搜索查询")
el3.click()
el3.send_keys("B站晚会")
el4 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout")
el4.click()
driver.press_keycode(66, undefined, undefined);
el5 = driver.find_element_by_id("tv.danmaku.bili:id/play_button")
el5.click()
driver.quit()
##网易云音乐app
from appium import webdriver
import time
caps = {}
caps["deviceName"] = "127.0.0.1:62001"#连接本机模拟器
caps["platformName"] = "Android"#app所处平台
caps["appPackage"] = "com.netease.cloudmusic"#被测app包名
caps["appActivity"] = ".activity.LoadingActivity"#被测app入口
caps["noReset"] = True # 禁止app在自动化后重置 一些设置(权限等)
caps["newCommandTimeout"] = 3600#连接超时时间 秒
caps["autmationName"] = "UiAutomator2"#指定新驱动UI2
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)#驱动appuim
driver.implicitly_wait(10)
#el1=driver.find_element_by_link_text('每日推荐').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@text="每日推荐"]').click()
#songs=driver.find_elements_by_id("com.netease.cloudmusic:id/ata")[:3]
#songs=songs=driver.find_elements_by_class_name("android.widget.LinearLayout")
#songs=driver.find_element_by_id('com.netease.cloudmusic:id/acx')
aa=driver.find_elements_by_xpath('//*[@text="所念皆星河"]')
for song in aa:
print(song.text)
#driver.quit()
连接夜神模拟器
adb connect 127.0.0.1:62001
查找app包名
adb shell dumpsys activity recents | findstr intent
一般cmp后是包名和app入口,cmp=com.netease.cloudmusic/.activity.LoadingActivity
Python 安装pip install Appium-Python-Client Selenium
2 自动化测试涉及到的一些基本操作
2.1 查找元素:find_element_by_id,find_element_by_xpath 这两个方法比较常用
2.2 模拟键盘的回车操作 driver.press_keycode(66)
2.3 元素的点击事件ele.click()
2.4模拟输入信息ele.send_keys("输入的内容")
2.5由于网络或者其他原因,到时对应的元素还没有出来,这个时候如果直接获取数据会报错,可以设置固定的等待时间driver.implicitly_wait(3) ,值得单位是秒。这样不需要用sleep的方式