一、基本環境搭建
1、下載Windows Python可執行安裝包,鏈接如下:https://www.python.org/downloads/windows/
選中安裝包進行下載,我這裏選中的是3.6.7,傾向於穩定的安裝包,雲盤也有。
2、下載好啓動執行文件,這裏我選中默認安裝(install now),安裝之前勾選下方選項,Python環境自動搭建
3、安裝成功後打開cmd,輸入python,顯示內容如下即安裝成功。你可以在當前頁面輸入python代碼
你也可以在啓動欄中查看python自帶的ide,並編寫代碼
二、安裝常用的開發工具pycharm
1、官網鏈接如下:https://www.jetbrains.com/pycharm/,點擊下載即可
選擇默認安裝,傻瓜式安裝即可,注意選中64bit
三、創建python項目
1、打開pycharm,選擇左上角file,選中new project,創建項目,再創建一個python file即可。跟java的類似。
注意:創建項目時選中編譯版本
2、在編輯頁面中敲入代碼,鼠標右擊,選擇run+項目,編譯運行。右上角點擊運行也可以。
四、利用Appium+python對安卓手機進行操作
1、這裏利用百度導航進行試驗,appium的安裝以及錄製參照
https://blog.csdn.net/Eayonz/article/details/106015720
此文章。
2、此次試驗用的是夜神模擬器,系統版本5.1.1,機型是127.0.0.1:62001,試驗內容是重新安裝軟件,並進行地名搜索,並截圖存放至D盤
3、代碼如下:
# 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 time import sleep
from appium import webdriver
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.baidu.BaiduMap"
caps["appActivity"] = "com.baidu.baidumaps.MapsActivity"
caps["unicodeKeyboard"] = True
caps["resetKeyBoard"] = True
caps["noReset"]="true"
caps["fullReset"]="false"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
sleep(2)#等待2秒,防止運行太快程序退出
el1 = driver.find_element_by_id("com.baidu.BaiduMap:id/ok_btn")
el1.click()
el2 = driver.find_element_by_id("android:id/button1")
el2.click()
el3 = driver.find_element_by_id("com.baidu.BaiduMap:id/btn_enter_map")
el3.click()
el4 = driver.find_element_by_id("com.baidu.BaiduMap:id/guide_close")
el4.click()
el5 = driver.find_element_by_accessibility_id("搜索地點、公交和路線")
el5.click()
el6 = driver.find_element_by_id("com.baidu.BaiduMap:id/tvSearchBoxInput")
el6.click()
el6.send_keys("北京")
sleep(2)
driver.save_screenshot('D:/foo.png')#截圖存放至D盤
el7 = driver.find_element_by_id("com.baidu.BaiduMap:id/tvSearchButton")
el7.click()
driver.quit()
4、如果沒有from appium import webdriver,webdriver會報錯,此時光標移至報錯處,點擊導入文件,pycharm會自動下載需要的包並給你導入,不像java的還要自己手動導入jar包,此處python比java方便多了。
5、當運行時driver報錯,而代碼中pycharm沒有報錯,此時abd檢查設備是否接入。
adb devices
6、while循環執行
要注意n=1與while n<10下方的代碼要有縮進,否則會報錯。
n=1
while n<10:
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
XXXX
XXXX
XXXX
n+=1
7、appium中文字符輸入
caps["unicodeKeyboard"] = True
caps["resetKeyBoard"] = True
網上一大堆True這邊加了雙引號,報錯resetKeyBoard、unicodeKeyboard需要布爾類型,後來發現caps[“unicodeKeyboard”] = True
caps[“resetKeyBoard”] = True
大寫True這邊是不需要加上雙引號的,小寫true的時候才需要加上雙引號
caps[“unicodeKeyboard”] = “true”
caps[“resetKeyBoard”] = "true"
8、設備不每次重新安裝啓動
caps["noReset"]="true"
caps["fullReset"]="false"
五、appium控制多臺設備進行操作
1、在appium中進行下圖的設置,advanced中設置Server Port以及Bootstrap Port,寫好後點擊右下角的save進行保存,一般端口累加比較好記。接下來在presets中查看保存信息並可執行server。
注意:開了幾臺設備就要對應開幾個服務器界面。如果你安裝了命令版本的可以直接在命令窗口啓動服務,命令爲:
appium -p 4723 -U 127.0.0.1:62001 -bp 4724
appium -p 4725 -U 127.0.0.1:62025 -bp 4726
夜神模擬器第一個模擬器udid一般爲:127.0.0.1:62001,對應端口4723,4724;第二個模擬器udid一般爲:127.0.0.1:62025,對應端口4725,4726
2、在代碼中要有多線程
threads = []
t1 = threading.Thread(target=task2)
threads.append(t1)
t2 = threading.Thread(target=task1)
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
其中開啓多臺設備必須指定udid參數,caps[‘udid’] = “127.0.0.1:62001”,udid參數即是adb devices中顯示出來的設備id
如下代碼主要是實現app註冊人員:定位到主頁面,點擊註冊按鈕,點擊上傳圖片,輸入姓名,輸入有效期,選擇分組,點擊提交。完整代碼如下:
import threading
import time
from time import sleep
from appium import webdriver
caps = {}
caps["noReset"] = "true"
caps["fullReset"] = "false"
caps["unicodeKeyboard"] = "true"
caps["resetKeyBoard"] = "true"
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps['udid'] = "127.0.0.1:62001"
caps["appPackage"] = "com.xx.xx"
caps["appActivity"] = "com.xx.xx.main.view.MainActivity"
caps1 = {}
caps1["noReset"] = "true"
caps1["fullReset"] = "false"
caps1["unicodeKeyboard"] = "true"
caps1["resetKeyBoard"] = "true"
caps1["platformName"] = "Android"
caps1["platformVersion"] = "5.1.1"
caps1["deviceName"] = "127.0.0.1:62025"
caps1['udid'] = "127.0.0.1:62025"
caps1["appPackage"] = "com.xx.xx"
caps1["appActivity"] = "com.xx.xx.main.view.MainActivity"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
def task1():
el1 = 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.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.ScrollView/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout[3]/android.widget.ImageView")
el1.click()
# i = 1963
# while i < 5000:
el1 = driver.find_element_by_id("com.xx.xx:id/tv_picture")
el1.click()
el2 = driver.find_element_by_id("com.xx.xx:id/tv_select_album")
el2.click()
el3 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[1]/android.widget.ImageView")
el3.click()
el4 = driver.find_element_by_id("com.xx.xx/btn_crop_confirm")
el4.click()
el5 = driver.find_element_by_id("com.xx.xx/tv_name")
el5.click()
el6 = driver.find_element_by_id("com.xx.xx/et_data")
el6.click()
# el6.send_keys("A註冊%d" % i)
el6.send_keys("雙機測試00000")
el6.click()
el7 = driver.find_element_by_id("com.xx.xx/btn_right")
el7.click()
el8 = driver.find_element_by_id("com.xx.xx/tv_valid_period")
el8.click()
el9 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView")
el9.click()
el1 = driver.find_element_by_id("com.xx.xx/tv_group")
el1.click()
sleep(1)
el2 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView[1]")
el2.click()
el13 = driver.find_element_by_id("com.xx.xx/tv_save")
el13.click()
sleep(15)
el14 = driver.find_element_by_id("com.xx.xx/tv_positive")
el14.click()
# print("循環執行%d" % i)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))#時間輸出
# i += 1
driver2 = webdriver.Remote("http://localhost:4725/wd/hub", caps1)
def task2():
el1 = driver2.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.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.ScrollView/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout[3]/android.widget.ImageView")
el1.click()
# i = 526
# while i < 5000:
el1 = driver2.find_element_by_id("com.xx.xx/tv_picture")
el1.click()
el2 = driver2.find_element_by_id("com.xx.xx/tv_select_album")
el2.click()
el3 = driver2.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout[1]/android.widget.ImageView")
el3.click()
el4 = driver2.find_element_by_id("com.xx.xx/btn_crop_confirm")
el4.click()
el5 = driver2.find_element_by_id("com.xx.xx/tv_name")
el5.click()
el6 = driver2.find_element_by_id("com.xx.xx/et_data")
el6.click()
el6.send_keys("雙機測試11111")
el6.click()
el7 = driver2.find_element_by_id("com.xx.xx/btn_right")
el7.click()
el8 = driver2.find_element_by_id("com.xx.xx/tv_valid_period")
el8.click()
el9 = driver2.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView")
el9.click()
el1 = driver2.find_element_by_id("com.xx.xx/tv_group")
el1.click()
sleep(1)
el2 = driver2.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView[1]")
el2.click()
el13 = driver2.find_element_by_id("com.xx.xx/tv_save")
el13.click()
sleep(15)
el14 = driver2.find_element_by_id("com.xx.xx/tv_positive")
el14.click()
# print("循環執行%d" % i)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))#時間輸出
# i += 1
threads = []
t1 = threading.Thread(target=task2)
threads.append(t1)
t2 = threading.Thread(target=task1)
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
# driver.quit()