封裝類:
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
class TestSeting():
def __init__(self):
desired_caps = {} # 配置參數的字典
desired_caps['platformName'] = 'Android' # 平臺名稱,iOS、Android、 FirefoxOS,選擇真機/模擬器對應的就好
desired_caps['deviceName'] = '模擬器' # 設備名稱填寫任意內容(使用的移動設備或模擬器的種類)
# desired_caps['platformVersion'] = '5.1.1' # 平臺版本
# desired_caps['appPackage'] = 'com.android.contacts' # 要運行的Android應用程序的Java包
# desired_caps['appActivity'] = '.activities.PeopleActivity' # 要運行的Android應用程序的主入口
desired_caps['platformVersion'] = '9' # 平臺版本
desired_caps['appPackage'] = 'com.包名' # 要運行的Android應用程序的Java包
desired_caps['appActivity'] = '.Activity' # 要運行的Android應用程序的主入口
desired_caps['unicodeKeyboard'] = True # 設置啓動編碼
desired_caps['resetKeyboard'] = False # 重置鍵盤
desired_caps['noReset'] = 'True' #默認爲False,當設置爲true時不作爲首次進入(在此會話之前,請勿重置應用程序狀態)
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# return self.driver
def waitXpath(self, xpath):
return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_xpath(xpath))
def waitId(self, id):
return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_id(id))
def waitClassName(self, classname):
return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_class_name(classname))
# 在當前類中的Terminal中執行:pytest -s 類名.py --html=./測試報告名.html
案例1:# 啓動app,打開通知欄並人選一條數據進入詳情頁面
from text_homework.scripts import testSeting
import unittest
import time
class testDyhoa5(unittest.TestCase):
def setUp(self):
print("start開始")
def tearDown(self):
print("end結束")
def test_001(self):
driver = testSeting.TestSeting().driver
time.sleep(5)
driver.open_notifications()
driver.find_element_by_id("android:id/status_bar_latest_event_content").click()
tes = testDyhoa5()
tes.test_001()
案例2:模擬器電話本新建用戶 張三,要求必填信息爲:姓名 電話 電子郵件 公司,保存後修改張三姓名改爲李四
from text_homework.scripts import testSeting
import unittest
import time
class testDyhoa2(unittest.TestCase):
def setUp(self):
print("start開始")
def tearDown(self):
print("end結束")
def test_001(self):
driver = testSeting.TestSeting().driver
print("start")
driver.find_element_by_id("com.android.contacts:id/floating_action_button").click()
driver.find_element_by_xpath("//*[contains(@text,'本地保存')]").click()
driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("張**")
driver.find_element_by_xpath("//*[contains(@text,'姓名拼音')]").send_keys("yyy")
driver.find_element_by_xpath("//*[contains(@text,'暱稱')]").send_keys("**")
driver.find_element_by_xpath("//*[contains(@text,'電話')]").send_keys("18810762963")
time.sleep(2)
driver.drag_and_drop(driver.find_element_by_xpath("//*[contains(@text,'電子郵件')]"),
driver.find_element_by_xpath("//*[contains(@text,'張**')]")) # 上下滑動
time.sleep(2)
driver.find_element_by_xpath("//*[contains(@text,'電子郵件')]").send_keys("@18810762963.com")
driver.find_element_by_xpath("//*[contains(@text,'地址')]").send_keys("地址:北京天安門")
driver.find_element_by_xpath("//*[contains(@text,'公司')]").send_keys("公司:故宮")
testSeting.TouchAction(driver).tap(x=79, y=168).perform()
driver.find_element_by_id("com.android.contacts:id/menu_edit").click()
driver.find_element_by_xpath("//*[contains(@text,'張**')]").clear()
driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("韓**")
testSeting.TouchAction(driver).tap(x=79, y=168).perform()
tes = testDyhoa2()
tes.test_001()
案例1:
# https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md 查看詳細參數意義
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import time
desired_caps = {} #配置參數的字典
desired_caps['platformName'] = 'Android' #平臺名稱,iOS、Android、 FirefoxOS,選擇真機/模擬器對應的就好
desired_caps['platformVersion'] = '5.1.1' #平臺版本
desired_caps['deviceName'] = '真機' #設備名稱填寫任意內容(使用的移動設備或模擬器的種類)
# desired_caps['appPackage'] = 'com.dyhoa.school' #要運行的Android應用程序的Java包
# desired_caps['appActivity'] = '.LauncherNewActivity' #要運行的Android應用程序的主入口
desired_caps['appPackage'] = 'com.android.settings' #要運行的Android應用程序的Java包
desired_caps['appActivity'] = '.Settings' #要運行的Android應用程序的主入口
# desired_caps['udid'] = '8c9abb78' #連接的物理設備的唯一設備標識符,可通過adb devices查看(非必選)
desired_caps['unicodeKeyboard'] = True #設置啓動編碼
# desired_caps['resetKeyboard'] = False #重置鍵盤
desired_caps['noReset'] = 'True' #默認爲False,當設置爲true時不作爲首次進入(在此會話之前,請勿重置應用程序狀態)
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
print("start")
time.sleep(4)
# 登錄
# driver.find_element_by_id("com.dyhoa.school:id/image_home_message").click()
# driver.find_element_by_xpath("//android.widget.TextView[@text='密碼登錄 >']").click()
# login_pho = driver.find_element_by_xpath("//android.widget.EditText[@text='輸入手機號']")
# login_pho = driver.find_element_by_xpath("//*[contains(@text,'輸入手機號')]") #方法2 contains模糊查找
# login_pho.send_keys("18810762963")
# login_pwd = driver.find_element_by_xpath("//android.widget.EditText[@text='輸入密碼']")
# # login_pwd = driver.find_element_by_id("com.dyhoa.school:id/login_pwd")
# login_pwd.send_keys("1111")
# driver.find_element_by_id("com.dyhoa.school:id/btn_login").click()
# driver.get_screenshot_as_file('pay.png') #手機截屏
# driver.set_network_connection(6) #0爲什麼都不開,1爲飛行模式,2爲wifi,4爲移動網絡,6爲wifi與網絡
# for x in range(3): #循環步數,點擊三次,keyevent(25)
# driver.keyevent(27)
# print(driver.device_time) #獲取手機系統時間
# driver.background_app(10) #放置後臺啓動 時間一到 會再起顯示
# size = driver.get_window_size() #獲取屏幕分辨率
# driver.find_element_by_xpath("//android.view.View[@text'設置']").click() #通過class與text 定位
# driver.find_element_by_xpath("//*[contains(@text'設置')]").click() #通過text內容模糊查找
# driver.find_element_by_xpath("//*[@resource-id='com.android.settings:id/search']").click() #通過id鍵值查找
#上下滑動
# end = driver.find_element_by_xpath("//*[contains(@text,'藍牙')]")
# start = driver.find_element_by_xpath("//*[contains(@text,'遊戲防沉迷')]")
# driver.drag_and_drop(start,end) #上下滑動
#獲取xy點
# locat = driver.find_element_by_id("com.android.settings:id/relative").location
# 點擊通知內容
# driver.open_notifications()
# driver.find_element_by_id("android:id/status_bar_latest_event_content").click()
# print('網絡爲',driver.network_connection) #獲取當前手機網絡
# 通過元素定位方式敲擊屏幕
# TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'藍牙')]")).perform()
# 通過xy點定位方式敲擊屏幕
# TouchAction(driver).tap(x=0,y=943).perform()
# 通過元素定位方式 手指按壓後離開,輕觸
# TouchAction(driver).press(driver.find_element_by_xpath("//*[contains(@text,'藍牙')]")).release().perform()
# 通過xy點定位方式 手指按壓後離開,輕觸
# TouchAction(driver).press(x=0,y=944).release().perform()
# 通過元素定位方式 手指長按
# TouchAction(driver).long_press(driver.find_element_by_xpath("//*[contains(@text,'藍牙')]"),3000).release().perform()
# 通過xy點定位方式 手指長按
# TouchAction(driver).long_press(x=0,y=944,duration=3000).release().perform()
end = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
start = driver.find_element_by_xpath("//*[contains(@text,'存儲')]")
driver.drag_and_drop(start,end) #上下滑動
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'安全')]")).perform()
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'屏幕鎖定方式')]")).perform()
edit = driver.find_element_by_id("com.android.settings:id/password_entry")
edit.send_keys("123456")
time.sleep(1)
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'下一步')]")).perform()
time.sleep(1)
TouchAction(driver).tap(driver.find_element_by_xpath("//*[contains(@text,'圖案')]")).perform()
# TouchAction(driver).press(x=178,y=640).wait(1000).move_to(x=536,y=640).release().perform()
TouchAction(driver).press(x=244,y=967).wait(1000).move_to(x=479,y=0).wait(1000).move_to(x=0,y=475).wait(1000).move_to(x=-479,y=474).release().perform()
print("end")
測試模擬器通訊錄並添加聯繫人,生成測試報告。
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
class Test_Utils():
def setup(self):
desired_caps = {} # 配置參數的字典
desired_caps['platformName'] = 'Android' # 平臺名稱,iOS、Android、 FirefoxOS,選擇真機/模擬器對應的就好
desired_caps['platformVersion'] = '5.1.1' # 平臺版本
desired_caps['deviceName'] = '模擬器' # 設備名稱填寫任意內容(使用的移動設備或模擬器的種類)
desired_caps['appPackage'] = 'com.android.contacts' # 要運行的Android應用程序的Java包
desired_caps['appActivity'] = '.activities.PeopleActivity' # 要運行的Android應用程序的主入口
desired_caps['unicodeKeyboard'] = True # 設置啓動編碼
desired_caps['resetKeyboard'] = False # 重置鍵盤
# desired_caps['noReset'] = 'False' #默認爲False,當設置爲true時不作爲首次進入(在此會話之前,請勿重置應用程序狀態)
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
def teardown(self):
print("end結束")
def waitXpath(self, xpath):
return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_xpath(xpath))
def waitId(self, id):
return WebDriverWait(self.driver, 5, 0.5).until(lambda x: x.find_element_by_id(id))
def test_001(self):
self.waitId("com.android.contacts:id/floating_action_button").click()
self.waitXpath("//*[contains(@text,'本地保存')]").click()
self.driver.find_element_by_xpath("//*[contains(@text,'姓名')]").send_keys("楊勇勇")
self.driver.find_element_by_xpath("//*[contains(@text,'姓名拼音')]").send_keys("yyy")
self.driver.find_element_by_xpath("//*[contains(@text,'暱稱')]").send_keys("勇勇")
self.driver.find_element_by_xpath("//*[contains(@text,'電話')]").send_keys("18810762963")
time.sleep(2)
self.driver.drag_and_drop(self.driver.find_element_by_xpath("//*[contains(@text,'電子郵件')]"),
self.driver.find_element_by_xpath("//*[contains(@text,'楊勇勇')]")) # 上下滑動
time.sleep(2)
self.driver.find_element_by_xpath("//*[contains(@text,'電子郵件')]").send_keys("@18810762963.com")
self.driver.find_element_by_xpath("//*[contains(@text,'地址')]").send_keys("北京天安門")
self.driver.find_element_by_xpath("//*[contains(@text,'公司')]").send_keys("故宮")
TouchAction(self.driver).tap(x=79, y=168).perform()
def test_002(self):
print("測試2")
def test_003(self):
print("測試3")
# 在當前類中的Terminal中執行:pytest -s 類名.py --html=./測試報告名.html
# pytest -s BaseUtils.py --html=./123.html
API:
(1)獲取當前頁面的activity名,比如: (.ui.login.ViewPage)
current_activity()
比如我們需要實現這個登錄的功能時,主要思路爲如果當前界面爲登錄頁面時,就進行登錄行爲,否則就跳轉到登錄頁面。其僞代碼爲:
1 if driver.current_activity == ".ui.login.ViewPage": 2 // To login_action 3 else: 4 // Trun to loginPage
(2)獲取當前頁面的樹形結構源代碼,與uiautomatorviewer截屏所展示出來的結構是相同的
page_source()
例如當我們完成登錄流程之後,要判斷登錄是否成功,則可以判斷登錄後的頁面有沒有出現特定的內容(比如:運動圈、發現、運動、商城、我的),其僞代碼實現如下:
driver \ .page_source.find(u"運動圈") != -1 and .page_source.find(u"發現") != -1 and .page_source.find(u"運動") != -1 and .page_source.find(u"商城") != -1 and .page_source.find(u"我的") != -1 and
page_source()的返回數據類型爲str。python中,str的find(context)方法,如果str存在context返回值爲context在str的index,如果不存在,則返回值爲-1。因此只需要判斷以上代碼塊返回的布爾值是True or False,就可以判斷是否登錄成功。
(3)獲取到當前窗口的所有context的名稱
contexts()
在native和html5混合頁面測試時,需要在native層和H5層切換,所以首先需要得到context層級的名稱
print driver.contexts >>> ['NATIVE_APP', 'WEBVIEW_com.codoon.gps']
由此可見,我們知道App的H5層名稱爲"WEBVIEW_com.codoon.gps"後,使用driver.switch_to.context("WEBVIEW_com.codoon.gps")就可以實現NATIVE和H5層的切換了。
二、獲取控件類API
(1)通過元素id查找當前頁面的一個目標元素
find_element_by_id()
通過源碼註釋可以得到find_element_by_id這一類的api主要有兩個使用途徑:
driver.find_element_by_id("com.codoon.gps:id/tv_login") // from webdriver.py
在driver下通過id查找一個元素,此用法通常適用於當前界面的driver有且僅有一個唯一的id元素標示,通過調用find_element_by_id可以準確到找到目標元素;另一種使用途徑主要如下:
driver_element = driver.find_element_by_xpath("//android.widget.ListView/android.widget.LinearLayout")
// from webdriverelement.py
driver_element.find_element_by_id("com.codoon.gps:id/activity_active_state")
在driver.find_element_by_xpath返回了driverElement類型,調用find_element_by_id在driverElement下的子元素以id匹配目標元素。
上圖爲uiautomatorviewer對id,name,class的圖示說明。特別說明:若id、name、xpath等在當前driver或者driverElement查找的目標元素不是唯一元素,此時調用find_element_by_id(name\xpath)時,會返回查找匹配到的第一個元素。
(2)通過元素id查找當前頁面的多個目標元素
find_elements_by_id()
在driver下通過id查找多個目標元素,其返回值類型爲list。此用法通常適用於當前driver下查詢listView、LinearLayout、 RelativeLayout等有相同佈局結構的Item;同樣除了driver之外,在driverElement下頁可以跳用find_elements_by_id來匹配listView、LinearLayout、 RelativeLayout。
driver.find_elements_by_id("com.codoon.gps:id/tv_name") // from webdriver.py
driver.find_element_by_id("com.codoon.gps:id/webbase_btn_share") \ .find_elements_by_id("com.codoon.gps:id/ll_layout") // from driverelement.py
Tips: 帶有find_elements關鍵字的方法函數的返回類型都是list數據類型,只有driver與driverelement的實例化有find_element(s)等一系列方法,list類型是不能用find_element(s)方法定位數據的。在實際的項目中可能會遇到這樣的問題,只有遍歷list,取出每一個element實例化對象再進行查找定位元素。
(3) 通過元素name查找當前頁面的一個元素
find_element_by_name()
使用方式與find_element_by_id相同,只是把匹配條件由id變爲name。請參考find_element_by_id的調用方式
driver.find_element_by_name("foo") driver.find_element_by_id("com.codoon.gps:id/tv_name").find_element_by_name("foo") >>> return the driverElement(obj)
(4) 通過元素name查找當前頁面的多個目標元素
find_elements_by_name()
使用方式與find_elements_by_id相同,只是把匹配條件由id變爲name。請參考find_elements_by_id的調用方式,注意其返回數據類型是List,並不是driverElement。
driver.find_elements_by_name("foo") driver.find_element_by_id("com.codoon.gps:id/tv_name").find_elements_by_name("foo") ### return the List<driverElement> >>> ['driverElement1', 'driverElement2', 'driverElement3', ....]
(5)通過元素xpath查找當前頁面的一個目標元素
find_element_by_xpath()
關於find_element_by_xpath的調用方法與通過id、name略有不同,有關Xpath的相關知識點在本章節暫且不表,後續在項目實踐中若有需求再另起專題介紹。
driver.find_element_by_xpath("//android.widget.TextView[contains(@text, '開始')]") driver.find_element_by_xpath("//android.widget.LinearLayout/android.widget.TextView")
在Appium中,xpath所需相關的lib庫並沒有完全支持,所以使用方法是以上兩種(即僅支持在driver下的xpath匹配)。目前的Appium版本無法支持driverelement下的xpath查找,如
driver.find_element_by_xpath("//android.widget.LinearLayout/android.widget.TextView") \ .find_element_by_xpath("//android.widget.TextView[contains(@text, '開始')]") // This is the Error!
按上面的寫法Appium就會報錯,原因是“.find_element_by_xpath("//android.widget.TextView[contains(@text, '開始')]")”不能在Element下查找子元素。
(6) 通過元素xpath查找當前頁面的多個目標元素
find_elements_by_xpath()
參照find_element_by_xpath的調用方式,需注意返回類型爲List,用法參考find_elements_by_name()的例子
(7) 通過元素class name查找當前頁面的的一個元素
find_element_by_class_name()
在實際項目中,測試app中class name並不能做爲界面的唯一標示定位,所以在實際中幾乎沒有使用class name在driver查看元素,在driverelement下查找子元素用class name纔是正確的使用方式。
(8) 通過元素accessibility_id (content-desc)查找當前頁面的一個元素
find_element_by_accessibility_id()
在uiautomatorviewer中,content-desc內容即爲accessibility_id,在selenium庫裏可以用find_element_by_name()來匹配content-desc的內容;在Appium庫裏則用find_element_by_accessibility_id()來匹配content-desc的內容。因爲Appium繼承了Selenium類,所以如果find_element_by_name無法準確定位時,請試試看find_element_by_accessibility_id。
常用的獲取控件類API就是以上這些。其他的查找和匹配的api還有find_element_by_link_text、find_elements_by_link_text、find_element_by_tag_name、find_elements_by_tag_name、find_element_by_css_selector、find_elements_by_css_selector等,用法都與上述類似。
三、元素操作類API
我們在實現PC端瀏覽器Webdriver自動化時,對於網頁上的目標的操作主要有:點擊(click)、 雙擊(double_click)、滾動(scroll)、輸入(send_keys),而移動端特有的輔助類api:輕擊(tap)--支持多點觸控,滑動(swipe),放大元素(pinch),縮小元素(zoom)
(1)點擊事件
click()
tap()
click和tap都能實現單擊的效果。其區別在於click是作用於driverelement的實例化對象,而tap是對屏幕上的座標位置進行點擊。前者對元素的位置變化並不敏感,而後者是針對具體的像素座標點擊,受分辨率和元素位置影響較大。
(2)輸入事件
send_keys()
set_text()
send_keys和set_text也都能滿足輸入文本內容的操作。其區別在於send_keys會調用設備當前系統輸入法鍵盤,而set_text直接對目標元素設置文本。由此可推,send_keys的輸入內容往往和預期內容不一致,而set_text的輸入則是直接賦值,並不是鍵盤事件。
(3)滑動(翻屏)事件
swipe()
flick()
swipe和flick都是滑動操作,它們都是從[start_x, start_y]劃到[end_x, end_y]的過程,唯一不同的是swipe比flick多了一個duration參數,有了這個參數就可以自定義從start到end動作的作用時間,以達到快速滑動或者慢速滑動的效果。
(4)縮放事件
pinch()
zoom()
默認會對目標元素進行放大一倍或者縮小一半的操作,此api方法適合於在測試運動地圖的縮放時的變化。
(5)長按事件
long_press()
長按方法是在TouchAction類中,所以在使用時需要先import TouchAction。在刪除運動歷史記錄時,在記錄列表長按刪除,
action1 = TouchAction(self.driver) driver_element = driver.find_element_by_xpath("sport_history_item_xpath") action1.long_press(driver_element).wait(i * 1000).perform() // i爲長按控件的時間,單位秒
(6)keyevent事件(android only)
在Android keyevent事件中,不同的值代表了不同的含義和功能,比如手機的返回鍵:keyevent(4); 手機的HOME鍵: keyevent(3)等等,具體keyevent與對應值關係請參考http://blog.csdn.net/yun90/article/details/51036544
四、元素事件類API
(1) reset
reset()
用法:driver.reset(),重置應用(類似刪除應用數據),如首次登錄app時出現的引導頁,則可以用reset來實現需求。
(2) is_app_installed
is_app_installed()
檢查app是否有安裝 返回 True or False。例如:在微信登錄時,選擇登錄方式時會判斷是否已安裝微信,若未安裝則有dialog彈框,已安裝則跳轉到微信登錄頁面,
driver.find_element_by_id("weixin_login_button").click() if driver.is_app_installed("weixin.apk"): // To do input User, Passwd else: // show dialog
(3)install_app
install_app()
接上個例子,若未安裝微信出現dialog彈框,檢查完dialog後再安裝微信app。特別說明:例子中的"weixin.apk"是指app_path + package_name,
driver.find_element_by_id("weixin_login_button").click() if driver.is_app_installed("weixin.apk"): // To do input User, Passwd else: check_dialog() driver.install_app("weixin.apk")
(4) remove_app
remove_app()
在測試老版本兼容用例時,用老版本替換新版本時,需要卸載新版本,再安裝老版本,所以需要調用到此方法,
driver.remove_app("new_app.apk") # 卸載 driver.install_app("old_app.apk") # 安裝
(5) launch_app
launch_app()
打開一個capabilities配置的設備應用。此方法目前並沒有使用。待以後用到時再來做更新。
(6) close_app
close_app()
關閉app應用程序。此方法常用在代碼末尾,在清理和釋放對象時使用。結合unittest框架常見tearDown()裏使用,
import unittest class demo(unittest.TestCase): def setUp(self): pass def tearDown(self): driver.close_app() driver.quit()
(7) start_activity
start_activity()
此方法適用於測試中需使用兩個及以上的app程序。例如,在運動相關的測試時,首先需要打開Gps模擬打點工具,開始打點。然後打開咕咚選擇運動類型開始運動,那麼可以在啓動capabilities配置時打開Gps工具,開啓配速打點後再打開咕咚app,
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps={'platformName': 'Android', 'deviceName': 'Android Mechine', 'appPackage': ' Package of GpsTools', 'unicodeKeyboard':True, 'resetKeyboard':True, 'noReset':True, 'appActivity': 'activity of GpsTools'}) # TO DO Gps Mock action driver.start_activity("com.codoon.gps", "ui.login.welcomeActivity")
(8) wait_activity
wait_activity()
此方法適屬於appium等待方法的一種。不論是webdriver還是appium,等待方法分爲三種類型:顯式等待、隱式等待,time.sleep;從wait_activity的源碼可以看出,是屬於隱式等待。有關等待方式以後可以另開專題詳細說明,這裏不做贅述。
此方法主要使用在需要網絡加載時的等待,比如在用戶登錄作爲前提條件時,wait_activity接受三個參數: 需要等待加載的activity的名稱,timeout超時時間(秒),檢測間隔時間(秒),
driver.login_action() driver.wait_activity("homepage.activity", 30, 1) driver.find_element_by_id("我的").click()
其含義是,等待加載app的homepage的activity出現,等待最長時間30秒,每隔1秒檢測一次當前的activity是否等於homepage的activity。若是,則推出等待,執行點擊我的tab的action;若否,則繼續等待,30秒後提示超時拋出異常。
四、其他(此類別下主要對上述沒有提到的api方法的補充)
(1) 截屏
get_screenshot_as_file()
用法:driver.get_screenshot_as_file('../screenshot/foo.png'),接受參數爲保存的圖片路徑和名稱
(2)size 和 location
size()
location()
size 和 location是對element位置和尺寸的獲取,這兩個屬性主要運用在有可划動的控件,如完善個人資料頁,生日、身高和體重都需要划動。之前我們提到的swipe和flick是針對設備屏幕進行划動,顯然在這裏不適用。而且沒有一個特定的方法,所以需要我們自己針對可划動控件進行划動,
swipe_control()
(3)獲取控件各種屬性
View Code
用法: driver.find_element_by_id().get_attribute(name),name即是左側的標誌(class,package,checkable,checked....),返回值爲str類型,即便是true or false,但是其實是"true" or "false"
(4)pull_file
pull_file()
將設備上的文件pull到本地硬盤上,在手機號註冊時需要獲取手機驗證碼,此時的實現方式是用另一個apk提取到驗證碼存在手機內存中,再用pull_file獲取到驗證碼內容,使得appium可以將正確的驗證碼填入。