appium+python自動化46-安裝app三種方式
adb安裝
1.在app自動化之前,首先手機上有要被測試的app,如何把電腦本地上的app安裝到手機上呢?可以在運行自動化代碼前,在cmd輸入adb指令,把電腦app安裝到手機上
adb install [apk在電腦上的絕對路徑]
2.也可以用python執行adb命令安裝app,代碼實現如下:
# coding:utf-8
import os
# 安裝app,為了方便,把app放到當前腳本同一目錄
os.system("adb install baidu.apk")
appium本地安裝
1.appium也可以實現自動安裝app,在工程下單獨建個app文件夾,待安裝的app放到此文件夾下
2.在工程下再建個case文件夾,寫用例時候先獲取app的絕對路徑
3.為了獲取路徑方便,可以寫個函數去獲取,這樣方便調用
4.進一步優化代碼,用lambda函數更簡潔,功能等價於上一步
# coding:utf-8 import os # 獲取項目的根目錄路徑 p = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)),"..")) print(p) appPath = lambda x:os.path.join(p, "app", x) print(appPath("baidu.apk"))
5.運行代碼安裝指定文件夾下app
# coding:utf-8 from appium import webdriver import os # 獲取項目的根目錄路徑 p = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)),"..")) # 獲取app路徑 appPath = lambda x:os.path.join(p, "app", x) print(appPath("baidu.apk")) desired_caps = { ‘platformName‘: ‘Android‘, ‘deviceName‘: ‘127.0.0.1:62001‘, ‘platformVersion‘: ‘4.4.2‘, ‘app‘: appPath("baidu.apk"), ‘appPackage‘: ‘com.baidu.yuedu‘, ‘appActivity‘: ‘com.baidu.yuedu.splash.SplashActivity‘, ‘noReset‘: ‘true‘, } driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)
appium在線安裝
1.如果沒有本地的app包,有個在線下載地址也是可以實現自動安裝的。比如打完測試包後上傳到一個指定的下載地址,通過這個下載地址也能實現安裝
2.以百度閱讀app為案例,百度搜索:百度閱讀app,從下載頁面獲取到app的下載地址
http://p.gdown.baidu.com/b8d3f4be0e333cca250c2d3663bd2ba59f87740a58b4288dd0725134e7f455721f254aeb5c678ff9e6aa0d90b7322b1b06c5f3239bd7b726d2b0f2ac187f7c95e08cf4ef572d854a546c1814c9ad9101c58f29b5df2eeb11e751cff1d1a9d6d1e74aab7e8d53a9039a3c5cbea8e461ecb3348cd38123bebedf6a9976c2fa7f1f1df6a6543c6d73124796e97a34cca9792f10878c878d124be5e1582911aea7870e909130f73adfac
3.代碼裏面只需把app參數後面的本地地址改成在線地址就可以了
"app": "app下載在線地址”
4.代碼參考:
# coding:utf-8 from appium import webdriver import os appUrl = "http://p.gdown.baidu.com/b8d3f4be0e333cca250c2d3663bd2ba59f87740a58b4288dd0725134e7f455721f254aeb5c678ff9e6aa0d90b7322b1b06c5f3239bd7b726d2b0f2ac187f7c95e08cf4ef572d854a546c1814c9ad9101c58f29b5df2eeb11e751cff1d1a9d6d1e74aab7e8d53a9039a3c5cbea8e461ecb3348cd38123bebedf6a9976c2fa7f1f1df6a6543c6d73124796e97a34cca9792f10878c878d124be5e1582911aea7870e909130f73adfac" desired_caps = { ‘platformName‘: ‘Android‘, ‘deviceName‘: ‘127.0.0.1:62001‘, ‘platformVersion‘: ‘4.4.2‘, ‘app‘: appUrl, ‘appPackage‘: ‘com.baidu.yuedu‘, ‘appActivity‘: ‘com.baidu.yuedu.splash.SplashActivity‘, ‘noReset‘: ‘true‘, } driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)
appium+python自動化46-安裝app三種方式