詳解基于Android的Appium+Python自動化腳本編寫
1.Appium
Appium是一個開源測試自動化框架,可用于原生,混合和移動Web應用程序測試, 它使用WebDriver協議驅動iOS,Android和Windows應用程序。
通過Appium,我們可以模擬點擊和屏幕的滑動,可以獲取元素的id和classname,還可以根據操作生成相關的腳本代碼。下面開始Appium的配置。
appPackage和APPActivity的獲取
任意下載一個app解壓
但是解壓出來的xml文件可能是亂碼,所以我們需要反編譯文件。逆向AndroidManifest.xml下載AXMLPrinter2.jar文件,逆向xml文件:命令行輸入以下命令:java -jar AXMLPrinter2.jar AndroidManifest.xml ->AndroidManifest.txt獲得以下可以查看的TXT文件
尋找帶有launcher 的Activity
尋找manifest里面的package
Devicename的獲取
通過命令行輸入 adb devices:
appium的功能介紹
下面將根據上圖序號一一介紹功能:
選中界面元素,顯示元素相關信息
模擬滑動屏幕,先點擊一下代表觸摸起始位置,在點擊一下代表觸摸結束為止
模擬點擊屏幕
模擬手機的返回按鈕
刷新左邊的頁面,使之與手機同步
記錄模擬操作,生成相關腳本
根據元素的id或者其他相關信息查找元素
復制當前界面的xml布局
文件退出
2.Python的腳本
元素定位的使用
(1).xpath定位
xpath定位是一種路徑定位方式,主要是依賴于元素絕對路徑或者相關屬性來定位,但是絕對路徑xpath執行效率比較低(特別是元素路徑比較深的時候),一般使用比較少。通常使用xpath相對路徑和屬性定位。by_xpath.py
from find_element.capability import driverdriver.find_element_by_xpath(’//android.widget.EditText[@text='請輸入用戶名']’).send_keys(’123456’)driver.find_element_by_xpath(’//*[@ and @index='3']’).send_keys(’123456’)driver.find_element_by_xpath(’//android.widget.Button’).click()driver.find_element_by_xpath(’//[@class='android.widget.Button']’).click()
(2).classname定位
classname定位是根據元素類型來進行定位,但是實際情況中很多元素的classname都是相同的,如用戶名和密碼都是clasName屬性值都是:“android.widget.EditText” 因此只能定位第一個元素也就是用戶名,而密碼輸入框就需要使用其他方式來定位,這樣其實很雞肋.一般情況下如果有id就不必使用classname定位。by_classname.py
from find_element.capability import driverdriver.find_element_by_class_name(’android.widget.EditText’).send_keys(’123565’)driver.find_element_by_class_name(’android.widget.EditText’).send_keys(’456879’)driver.find_element_by_class_name(’android.widget.Button’).click()
(3).id定位
日常生活中身邊可能存在相同名字的人,但是每個人的身份證號碼是唯一的,在app界面元素中也可以使用id值來區分不同的元素,然后進行定位操作。Appium中可以使用 find_element_by_id() 方法來進行id定位。
driver.find_element_by_id(’android:id/button2’).click()driver.find_element_by_id(’com.tal.kaoyan:id/tv_skip’).click()
3.示例:模擬軟件的自動注冊
首先配置連接屬性
desired_caps={}# 所使用的平臺desired_caps[’platformName’]=’Android’# 所使用的手機的名字 可以通過 adb devices 獲得desired_caps[’deviceName’]=’127.0.0.1:62001’# ANDROID 的版本desired_caps[’platforVersion’]=’5.1.1’# app 的路徑desired_caps[’app’]=r’D:extendkaoyanbang.apk’# app的包名desired_caps[’appPackage’]=’com.tal.kaoyan’# app 加載頁面desired_caps[’appActivity’]=’com.tal.kaoyan.ui.activity.SplashActivity’# 設置每次是否清除數據desired_caps[’noReset’]=’False’# 是否使用unicode鍵盤輸入,在輸入中文字符和unicode字符時設置為truedesired_caps[’unicodeKeyboard’]='True'# 是否將鍵盤重置為初始狀態,設置了unicodeKeyboard時,在測試完成后,設置為true,將鍵盤重置desired_caps[’resetKeyboard’]='True'# appium服務器的連接地址driver=webdriver.Remote(’http://localhost:4723/wd/hub’,desired_caps)driver.implicitly_wait(2)
編寫操作腳本
import randomimport timedriver.find_element_by_id(’com.tal.kaoyan:id/login_register_text’).click()username=’zx2019’+’F2LY’+str(random.randint(1000,9000))print(’username: %s’ %username)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_username_edittext’).send_keys(username)password=’zxw2018’+str(random.randint(1000,9000))print(’password: %s’ %password)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_password_edittext’).send_keys(password)email=’51zxw’+str(random.randint(1000,9000))+’@163.com’print(’email: %s’ %email)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_email_edittext’).send_keys(email)#點擊進入考研幫driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_register_btn’).click()#專業選擇driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_major’).click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_subject_title’)[1].click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_group_title’)[2].click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_search_item_name’)[1].click()#院校選擇driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_school’).click()driver.tap([(182,1557),])driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/android.widget.’ ’RelativeLayout/android.widget.ExpandableListView/android.widget.’ ’LinearLayout[1]/android.widget.TextView[1]’).click()driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.RelativeLayout/android.widget.ExpandableListView/’ ’android.widget.LinearLayout[4]/android.widget.TextView’).click()time.sleep(2)driver.tap([(983,1354),])# driver.find_elements_by_id(’com.tal.kaoyan:id/more_forum_title’)[1].click()# driver.find_elements_by_id(’com.tal.kaoyan:id/university_search_item_name’)[1].click()driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_goBtn’).click()print(’注冊成功’)
到此這篇關于詳解基于Android的Appium+Python自動化腳本編寫的文章就介紹到這了,更多相關Android的Appium+Python自動化腳本內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: