介紹做ppt高大上圖表的網(wǎng)站西安網(wǎng)站搭建
一、press_keycode
1)方法說明
????????press_keycode方法是appium的鍵盤相關(guān)函數(shù),可以實(shí)現(xiàn)鍵盤的相關(guān)操作,比如返回、按鍵、音量調(diào)節(jié)等等。也可以使用keyevent方法,功能與press_keycode方法類似。
? ? ? ? 常見按鍵編碼:https://www.cnblogs.com/bluestorm/p/4886662.html。
# KeyCode:各種操作對應(yīng)的鍵值碼
driver.press_keycode(KeyCode)
2)使用示例
# 返回鍵
driver.press_keycode(4)# 回車鍵
driver.press_keycode(66)# 回車鍵
driver.keyevent(66)# 退格鍵
driver.keyevent(67)
二、scroll方法
1)方法說明
????????scroll方法是滑動頁面,不過不是滑動滾動條,而是獲取兩個元素,然后從一個元素滾動到另一個元素。
????????要求:兩個元素都在界面上可見,否則會報錯。而且滑動持續(xù)時間設(shè)置越短,滑動越快,滑動效果會不太準(zhǔn)確。所以使用scroll方法時,滑動持續(xù)時間盡量設(shè)置大一些。
# origin_el:滾動的起始元素
# destination_el:滾動的結(jié)束元素
# duration:滑動的持續(xù)時間,默認(rèn)是600ms,時間越大滑動越慢
driver.scroll(origin_el, destination_el, duration)
2)使用示例
????????這里通過代碼,將設(shè)置頁從 “應(yīng)用兼容性” 滑動到 “更多”。
start_element = driver.find_element('xpath', "//*[@text='應(yīng)用兼容性']")
end_element = driver.find_element('xpath', "//*[@text='更多']")
driver.scroll(start_element, end_element, 5000)
三、drag_and_drop方法
1)方法說明
????????drag_and_drop方法是也是滑動頁面,從一個元素滑動到另一個元素,第二個元素代替第一個元素原本屏幕上的位置。
????????要求:兩個元素都在界面上可見,否則會報錯。但是drag_and_drop方法不能設(shè)置滑動持續(xù)時間,但滑動效果比scroll方法更加精確,幾乎沒有慣性。drag_and_drop方法有點(diǎn)類似于慢速版的scroll方法。
# origin_el:滾動的起始元素
# destination_el:滾動的結(jié)束元素
driver.drag_and_drop(origin_el, destination_el)
2)使用示例
????????這里通過代碼,將設(shè)置頁從 “應(yīng)用兼容性” 滑動到 “更多”
start_element = driver.find_element('xpath', "//*[@text='應(yīng)用兼容性']")
end_element = driver.find_element('xpath', "//*[@text='更多']")
driver.drag_and_drop(start_element, end_element)
四、swipe方法
1)方法說明
????????swipe方法是從一個坐標(biāo)點(diǎn)滑動到另一個坐標(biāo)點(diǎn),也就是說是兩點(diǎn)之間的滑動。
# start_x:起始坐標(biāo)點(diǎn)的橫坐標(biāo)
# start_y:起始坐標(biāo)點(diǎn)的縱坐標(biāo)
# end_x:結(jié)束坐標(biāo)點(diǎn)的橫坐標(biāo)
# end_y:結(jié)束坐標(biāo)點(diǎn)的縱坐標(biāo)
# duration:滑動的持續(xù)時間
driver.swipe(start_x, start_y, end_x, end_y, duration)
2)使用示例
????????這里通過代碼,將設(shè)置頁從第一個坐標(biāo)點(diǎn)(660,1483)滑動到第二個坐標(biāo)點(diǎn)(660,533)。
driver.swipe(660, 1483, 660, 533, 5000)
五、TouchAction
1)TouchAction使用步驟
? ? ? ? TouchAction可以實(shí)現(xiàn)一些針對手勢的操作,比如滑動、拖動、長按等,我們可以將這些基本手勢組合成一個相對復(fù)雜的手勢,比如我們解鎖手機(jī)或者一些應(yīng)用軟件都有手勢解鎖的這種方式。
- 使用TouchAction具體方法之前,需要導(dǎo)入TouchAction庫。
from appium.webdriver.common.touch_action import TouchAction
- 創(chuàng)建TouchAction對象。
- 通過對象調(diào)用想要執(zhí)行的手勢操作的方法。
- 通過perform方法執(zhí)行動作(所有手勢必須通過perform方法來觸發(fā))。
2)tap方法
????????tap()方法用來模擬手指對某個元素或坐標(biāo)按下并快速抬起。
? ? ? ? tap和click方法的作用差不多,但tap可以接收一個坐標(biāo)值作為點(diǎn)擊的區(qū)域,而click只能接收元素對象作為參數(shù)。另外,tap()方法還可以設(shè)置count參數(shù),表示點(diǎn)擊次數(shù),count=2用來表示雙擊。
def tap(self,element: Optional['WebElement'] = None,x: Optional[int] = None,y: Optional[int] = None,count: int = 1,
) -> 'TouchAction':"""Perform a tap action on the elementArgs:element: the element to tapx : x coordinate to tap, relative to the top left corner of the element.y : y coordinate. If y is used, x must also be set, and vice versa
TouchAction(driver).tap(user_name).perform()
3)press方法
????????press()方法用來模擬手指對某個元素或坐標(biāo)一直按下。
def press(self,el: Optional['WebElement'] = None,x: Optional[int] = None,y: Optional[int] = None,pressure: Optional[float] = None,
) -> 'TouchAction':"""Begin a chain with a press down action at a particular element or pointArgs:el: the element to pressx: x coordiate to press. If y is used, x must also be sety: y coordiate to press. If x is used, y must also be set
TouchAction(driver).press(x=100, y=200).perform()
4)release方法
????????release()方法用來模擬手指抬起。
def release(self) -> 'TouchAction':"""End the action by lifting the pointer off the screen
Returns:`TouchAction`: Self instance
"""self._add_action('release', {})return self
TouchAction(driver).press(x=100, y=200).release().perform()
5)wait方法
????????wait()方法用來模擬手指等待。
def wait(self, ms: int = 0) -> 'TouchAction':"""Pause for `ms` milliseconds.
Args:ms: The time to pauseReturns:`TouchAction`: Self instance
"""
TouchAction(driver).press(x=100, y=200).wait(3000).release().perform()
6)move_to方法
????????wait()方法用來模擬手指移動到某個元素或坐標(biāo)。
def move_to(self, el: Optional['WebElement'] = None, x: Optional[int] = None, y: Optional[int] = None
) -> 'TouchAction':"""Move the pointer from the previous point to the element or point specified
Args:el: the element to be moved tox: x coordiate to be moved to. If y is used, x must also be sety: y coordiate to be moved to. If x is used, y must also be setReturns:`TouchAction`: Self instance
"""
TouchAction(driver).press(x=400, y=500).move_to(500, 600).perform()
7)使用示例
????????通過代碼繪制出下圖中的圖案。
8)報錯分析
from appium import webdriver
import time# 設(shè)置啟動參數(shù)
desired_cap = {}
desired_cap['platformName'] = 'Android'
desired_cap['platformVersion'] = '6.0.1'
desired_cap['deviceName'] = '127.0.0.1:7555'
# 必須參數(shù),指定被測軟件的包名
desired_cap['appPackage'] = 'com.android.settings'
# 必須參數(shù),指定要打開app的哪個頁面
desired_cap['appActivity'] = '.ChooseLockPattern'
desired_cap['automationName'] = 'Uiautomator2'
desired_cap['noReset'] = True
desired_cap['newCommandTimeout'] = 6000
desired_cap['unicodeKeyboard'] = True
desired_cap['resetKeyboard'] = Truedriver = webdriver.Remote('http://localhost:4723/wd/hub', desired_cap)
????????通過adb命令獲取到繪制圖案頁面的activity為.ChooseLockPattern,但是通過執(zhí)行上面代碼不能打開設(shè)置app,報錯:Original error: Cannot start the 'com.android.settings' application.?Original error: The permission to start '.ChooseLockPattern' activity has been denied.
? ? ? ? 原因是app應(yīng)用沒開權(quán)限,對于activity是禁止外部調(diào)用的。
9)解決辦法
- 將AndroidManifest.xml文件中將Activity設(shè)置成允許調(diào)用:Android:exported=”true”,加上權(quán)限后再重新打apk包。
- 通過代碼打開app應(yīng)用的啟動頁,然后通過定位菜單元素,逐層點(diǎn)擊進(jìn)入下一層界面,直到打開繪制圖案界面為止。
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time# 設(shè)置啟動參數(shù)
desired_cap = {}
desired_cap['platformName'] = 'Android'
desired_cap['platformVersion'] = '6.0.1'
desired_cap['deviceName'] = '127.0.0.1:7555'
# 必須參數(shù),指定被測軟件的包名
desired_cap['appPackage'] = 'com.android.settings'
# 必須參數(shù),指定要打開app的哪個頁面
desired_cap['appActivity'] = '.Settings'
desired_cap['automationName'] = 'Uiautomator2'
desired_cap['noReset'] = True
desired_cap['newCommandTimeout'] = 6000
desired_cap['unicodeKeyboard'] = True
desired_cap['resetKeyboard'] = Truedriver = webdriver.Remote('http://localhost:4723/wd/hub', desired_cap)
time.sleep(5)# 1.獲取手機(jī)設(shè)備寬、高信息
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']# 2.將設(shè)置app的啟動界面向上滑動半屏,使 “安全” 菜單項(xiàng)可見
driver.swipe(x * 0.5, y * 0.5, x * 0.5, 0)# 3.依次點(diǎn)擊菜單 “安全” -> “屏幕鎖定方式” -> “圖案”
driver.find_element('xpath', '//*[@text="安全"]').click()
time.sleep(1)
driver.find_element('xpath', '//*[@text="屏幕鎖定方式"]').click()
time.sleep(1)
driver.find_element('xpath', '//*[@text="圖案"]').click()
time.sleep(1)# 4.繪制圖案
ta = TouchAction(driver)
# 按住第一個點(diǎn)
ta.press(x=145, y=564).wait(1000)
ta.move_to(x=449, y=564).wait(1000)
ta.move_to(x=748, y=564).wait(1000)
ta.move_to(x=748, y=863).wait(1000)
ta.move_to(x=748, y=1165).wait(1000)
# 移動到最后一個點(diǎn)之后松手
ta.release().perform()
10)如何定位不可見元素?
? ? ? ? 在上述示例中,打開app應(yīng)用的啟動頁后,需要先打開 “安全” 菜單,但界面菜單項(xiàng)過多,屏幕太小,導(dǎo)致?“安全” 菜單不可見。所以需要先將屏幕進(jìn)行滑動,使得?“安全” 菜單可見后,再進(jìn)行操作。
? ? ? ? 在上述示例中,將屏幕向上滑動半屏后,就能看見?“安全” 菜單,所以在代碼中將滑動屏幕操作直接寫死了,直接滑動半屏,然后進(jìn)行定位、操作等等。
? ? ? ? 但有時候要定位的元素,可能需要滑動好多屏才能看見,所以不能直接在代碼中寫死,而應(yīng)該在while循環(huán)中邊滑動屏幕、邊對元素進(jìn)行定位,當(dāng)定位到目標(biāo)元素后,跳出循環(huán)。
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time# 設(shè)置啟動參數(shù)
desired_cap = {'platformName': 'android','platformVersion': '6.0.1','deviceName': '127.0.0.1:7555','appPackage': 'com.android.settings','appActivity': '.Settings','automationName': 'Uiautomator2','noReset': True,'unicodeKeyboard': True,'resetKeyboard': True
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_cap)
time.sleep(5)# 1.獲取手機(jī)設(shè)備的寬、高信息
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']# 2.不斷向上滑動半屏,查找“開發(fā)者選項(xiàng)”菜單
while True:try:driver.find_element('xpath', '//*[@text="開發(fā)者選項(xiàng)"]').click()breakexcept NoSuchElementException:print("當(dāng)前屏幕未找到“開發(fā)者選項(xiàng)”菜單!")# 每次向上滑動半屏driver.swipe(0.5 * x, 0.5 * y, 0.5 * x, 0)
六、使用appium在腳本中啟動其他app
# appPackage:被啟動應(yīng)用的包名
# appActivity:被啟動應(yīng)用的activity名字
driver.start_activity(appPackage, appActivity)
七、使用appium獲取包名和Activity的名字
# 獲取當(dāng)前打開的app應(yīng)用包名
driver.current_package# 獲取當(dāng)前打開的app應(yīng)用所顯示界面的activity名
driver.current_activity
八、使用appium關(guān)閉當(dāng)前打開的app應(yīng)用
# 關(guān)閉當(dāng)前打開的app應(yīng)用
driver.close_app()# 退出driver對象
driver.quit()
九、使用appium安裝和卸載app應(yīng)用
????????安裝、卸載app應(yīng)用,以及判斷app是否安裝,返回值為bool類型,True表示已安裝,False表示未安裝。
# 1.安裝app應(yīng)用
# app_path:apk文件的路徑
driver.install_app(app_path)# 2.卸載app應(yīng)用
# app_id:app應(yīng)用包名
driver.remove_app(app_id)# 3.判斷app是否安裝
# app_id:app應(yīng)用包名
driver.is_app_installed(app_id)
十、使用appium將應(yīng)用置于后臺
????????模擬home鍵,將app應(yīng)用置于后臺。注意:停留秒數(shù)到達(dá)后,app應(yīng)用會自動回到前臺。
? ? ? ? 某些應(yīng)用在進(jìn)入后臺一段時間后,重新回到前臺時會要求輸入密碼,如果自動化需要測試這種功能,可以使用這個api來測試。這里涉及兩個概念:
- 熱啟動:應(yīng)用從后臺回到前臺,叫熱啟動。
- 冷啟動:第一次打開某個應(yīng)用,叫冷啟動。
# seconds:在后臺停留多少秒
driver.background_app(seconds)
十一、獲取手機(jī)設(shè)備分辨率
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
十二、獲取手機(jī)截圖
# filename:截圖保存的位置
driver.get_screenshot_as_file(filename)
driver.get_screenshot_as_file(r'E:\AndroidSDK\test.png')
十三、獲取和設(shè)置手機(jī)設(shè)備網(wǎng)絡(luò)狀態(tài)
from appium.webdriver.connectiontype import ConnectionType# 1.獲取手機(jī)網(wǎng)絡(luò)狀態(tài)
driver.network_connection# 2.設(shè)置手機(jī)網(wǎng)絡(luò)狀態(tài)
# connectionType:網(wǎng)絡(luò)狀態(tài)碼
driver.set_network_connection(ConnectionType.NO_CONNECTION | ConnectionType.AIRPLANE_MODE | ConnectionType.WIFI_ONLY | ConnectionType.DATA_ONLY | ConnectionType.ALL_NETWORK_ON)
1:飛行模式、 2:只開wifi、4:只開流量、6:網(wǎng)絡(luò)全開。
Value (Alias)? | Data | Wifi | Airplane Mode |
0 (None)? | 0 | 0 | 0 |
1 (Airplane Mode) | 0 | 0 | 1 |
2 (Wifi only)? | 0 | 1 | 0 |
4 (Data only)? | 1 | 0 | 0 |
6 (All network on)? | 1 | 1 | 0 |
十四、操作手機(jī)通知欄
????????測試某些應(yīng)用進(jìn)行消息推送時,需要檢查通知欄是否有消息通知。
# 打開通知欄
driver.open_notifications()