移動端自動化測試入門:用appium 控制安卓手機打開app(第二版,已完成)

基本需求:通過python代碼自動打開指定app,並打開相關的內容,停留一段時間。

用到的工具

  • jdk 1.8
  • sdk
  • adb工具(android debug bridge)
  • uiautomaorviewer

順序

  1. 安裝 jdk、安卓sdk
  2. 設置環境變量(Android SDK 和 java的環境變量)設置環境變量(Android SDK 和 java的環境變量)
  3. 打開命令行打開命令行
  4. 啓動adb服務器:adb start-server
  5. 啓動adb服務器:adb start-server啓動adb服務器:adb start-server
  6. 查看已連接的設備,獲取設備名稱:adb devices
  7. 通過adb 獲取app包名和activity名稱(通過代碼打開時需要用到):通過adb 獲取app包名和activity名稱(通過代碼打開時需要用到):
    1) adb devices (獲取設備名
    2) adb -s [設備名] shell (進入設備的shell環境)
    3) dumpsys activity | grep mFocusedActivity (前臺的app)
  8. 通過升級版 uiautomaorviewer,查看控件名,獲取控件Xpath名稱。
  9. 通過 driver.find_element_by_xpath 獲取組件,通過send_keys傳值,通過click點擊。 關鍵是要獲取正確唯一穩定的id。 不管是xpath 、resource_id還是text。 xpath偶爾會不穩定,不知道爲啥。

第一版

# iqiyi_test.py  第一版
# @Date: 2019-02-26 16:31:49

# 注意事項:
# 1.需要確保足夠電量,不要彈出電量不足的框框(或者定位到該框框,點擊關閉)

import time 
from appium import webdriver
desired_caps_iqiyi={
  "platformName": "Android",
  "platformVersion": "5.1.1",
  "deviceName": "172.16.18.43:5555",
  "appPackage": "com.qiyi.video",
  "appActivity": "org.qiyi.android.video.MainActivity",
  "noReset": True,
  "unicodeKeyboard":True,
  "resetKeyboard":True
}
driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps_iqiyi)

def iqiyi_tv_show(key_word,num_tag):
	#在搜索框輸入 完全匹配的關鍵詞
	if num_tag != 1:
		driver.find_element_by_id("com.qiyi.video:id/brs").send_keys(key_word)
	else:
		driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/eav']").send_keys(key_word)
	#點擊搜索框
	driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/eah']").click()

	# 點擊第一集 text  要修改源碼支持 by_name
	driver.find_element_by_name("1").click()
	# time.sleep(5)
	# 按順序點擊 第 2 3 4 集
	driver.find_element_by_name("2").click()
	# time.sleep(5)
	driver.find_element_by_name("3").click()
	# time.sleep(5)
	driver.find_element_by_name("4").click()
	# time.sleep(5)
	# 返回搜索頁面 開了會員就沒有廣告
	try:
		#廣告頁的返回
		driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/player_ads_back_pre_ad']").click()
	except:
		#視頻播放頁的返回
		driver.find_element_by_xpath("//android.widget.ImageView[@resource-id='com.qiyi.video:id/player_piecemeal_layer_back']").click()

def iqiyi_movie(key_word):	
	#電影
	driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.qiyi.video:id/brs']").send_keys(key_word)
	driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/eah']").click()
	# 點擊播放
	driver.find_element_by_id("com.qiyi.video:id/button1").click()
	time.sleep(5)
	try:
		# 廣告頁的返回
		driver.find_element_by_id("com.qiyi.video:id/player_ads_back_pre_ad").click()
	except:
		# 視頻播放頁的返回
		driver.find_element_by_id("com.qiyi.video:id/player_piecemeal_layer_back").click()

def main():
	tv_show_names=['小女花不棄','獨孤皇后','黃金瞳']
	num_tag=1
	for name in  tv_show_names:
		iqiyi_tv_show(name,num_tag)
		num_tag+=1
	movies_names=['靈魂擺渡黃泉','晝顏','狂龍伏妖']
	for name in  movies_names:
		iqiyi_movie(name)
main()

第二版

# @Date: 2019-02-26 16:31:49
# 注意事項:
# 1.需要確保足夠電量,不要彈出電量不足的框框(或者定位到該框框,點擊關閉)

import time 
from appium import webdriver
# import config
# from  iqiyi_request_save import get_channel
# channel_dict = {"channel":"空"}
driver = ''

desired_caps_iqiyi={
  "platformName": "Android",
  "platformVersion": "5.1.1",
  "deviceName": "172.16.18.43:5555",
  "appPackage": "com.qiyi.video",
  "appActivity": "org.qiyi.android.video.MainActivity",
  "noReset": True,
  "unicodeKeyboard":True,
  "resetKeyboard":True
}

# 連接 app
def connect_open_app():
	global driver
	driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps_iqiyi)

# 點擊播放頁的劇集
def click_episode_by_xpath(num):
	try:
		driver.find_element_by_xpath("//android.support.v7.widget.RecyclerView/android.support.v7.widget.RecyclerView[2]/android.widget.RelativeLayout["+num+"]").click()
	except:
		driver.find_element_by_xpath("//android.support.v7.widget.RecyclerView[@resource-id='com.qiyi.video:id/recycler']/android.widget.RelativeLayout["+num+"]").click()
# 點擊播放頁的劇集
def click_episode_by_name(num):
	driver.find_element_by_name(str(num)).click()

# 從播放頁返回搜索頁
def back_to_search_page():
	# 返回搜索頁面 開了會員就沒有廣告
	# global driver
	try:
		driver.find_element_by_id("com.qiyi.video:id/player_ads_back_pre_ad").click() #廣告頁的返回
	except:
		driver.find_element_by_id("com.qiyi.video:id/player_piecemeal_layer_back").click() #視頻播放頁的返回
def input_in_search_box(key_word):
	 try:
	 	#com.qiyi.video:id/brs 
		driver.find_element_by_id("com.qiyi.video:id/brs").send_keys(key_word)
		#com.qiyi.video:id/eah
		driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/eah']").click()
	except:
		driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.qiyi.video:id/eav']").send_keys(key_word)
		driver.find_element_by_xpath("//android.widget.ImageView[@resource-id='com.qiyi.video:id/right_search_icon']").click()
		

def click_first_episode_by_name(num):
	driver.find_element_by_name(num).click()

def iqiyi_playing_by_name(key_word,num_tag):
	#在搜索框輸入,並點擊搜索 (完全匹配的關鍵詞)
	input_in_search_box(key_word)
	time.sleep(5)
	# 點擊第一集 text  要修改源碼支持 by_name
	click_first_episode_by_name("1")
	# time.sleep(5)
	# 按順序點擊 第 2 3 4 集
	for num in ["2","3","4"]:
		click_episode_by_name(num)
		# time.sleep(5)
	# 返回搜索頁面
	back_to_search_page()

def iqiyi_movie(key_word):	
	#電影
	#在搜索框輸入,並點擊搜索 (完全匹配的關鍵詞)
	input_in_search_box(key_word)
	# 點擊播放
	driver.find_element_by_id("com.qiyi.video:id/button1").click()
	time.sleep(5)
	# 返回搜索頁面
	back_to_search_page()

def iqiyi_playing_by_box(key_word,num_tag):
	#在搜索框輸入,並點擊搜索 (完全匹配的關鍵詞)
	input_in_search_box(key_word)
	time.sleep(5)
	# 點擊第一集 text  要修改源碼支持 by_name
	click_first_episode_by_name("1")
	# time.sleep(5)				
	# 按順序點擊 第 2 3 4 集
	for num in ["2","3","4"]:
		click_episode_by_xpath(num)
		# time.sleep(5)

	back_to_search_page()

def change_channel(channel):
	f = open("request_data\\channel.txt", "w")
	f.write(channel)
	f.close()


def click_circle_in_channel(names,channel_names,click_func_name):
	num_tag=1
	# 修改txt文件中的頻道名稱
	change_channel(channel_names)
	for name in  names:
		click_func_name(name,num_tag)
		num_tag+=1
	time.sleep(5)

def main():
	connect_open_app()
	tv_show_names=['小女花不棄','獨孤皇后','黃金瞳']
	click_circle_in_channel(tv_show_names,"tv_show",iqiyi_playing_by_name)
	time.sleep(5)
	movies_names=['小女花不棄','獨孤皇后','黃金瞳']
	click_circle_in_channel(movies_names,"tv_show",iqiyi_movie)
	time.sleep(5)

	dongman_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)	
	# dongman_names_guoman=['次元二Bi治']
	# click_circle_in_channel(children_names,"children",iqiyi_playing_by_box)	
	
	documentary_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)

	variety_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)

	children_names_like_tvshow=['暴速合體','熊出沒之探險日記2',,'名偵探柯南 普通話']
	click_circle_in_channel(children_names_like_tvshow,"children",iqiyi_playing_by_name)	

	children_names=['小豬佩奇 第四季']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_box)	


	info_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)

	original_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)

	entertainment_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
	click_circle_in_channel(children_names,"children",iqiyi_playing_by_name)	
	dongman_names=['賀少的閃婚暖妻','畫江湖之不良人 第3季','熱血女王']
		




# 立即播放
#圖標  //android.widget.RelativeLayout/android.widget.LinearLayout[7]
#圖標  //android.widget.RelativeLayout/android.widget.LinearLayout[7]
#文字 //android.widget.TextView[@text='立即播放']

#第一集 圖標  //android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]
#第一集 圖標 //android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]
#第二集 圖標  //android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.LinearLayout[1]
#第三集 圖標	 //android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[3]/android.widget.LinearLayout[1]
#第四集 圖標	 //android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[4]/android.widget.LinearLayout[1]

#播放界面 返回圖標 //android.widget.TextView[@resource-id='com.qiyi.video:id/player_ads_back_pre_ad']
#播放界面 第二集   //android.support.v7.widget.RecyclerView/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[2]/android.widget.ImageView[1]
#播放界面 第三集   //android.support.v7.widget.RecyclerView/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[3]/android.widget.ImageView[1]
#播放界面 第四集  //android.support.v7.widget.RecyclerView[@resource-id='com.qiyi.video:id/recycler']/android.widget.RelativeLayout[4]/android.widget.ImageView[1]
# webdriver.wait()
# iqiyi_request_save.py
# from mitmproxy import ctx
import time
from imp import reload
from handle_excel import save_data  

channel = "null 22222"
def read_channel():
	f = open("request_data\\channel.txt", "r")
	channel = f.read()
	print("read_channel:"+channel)
	f.close()
	return channel
#必須這麼寫
def request(flow):
	scheme = flow.request.scheme
	method = flow.request.method
	host = flow.request.host
	path = flow.request.path
	headers = str(flow.request.headers)
	query = str(flow.request.query)
	timestamp_start = str(flow.request.timestamp_start)

	if scheme =='http':
		channel=read_channel()
		li = [scheme,method,host,path,headers,query,timestamp_start,channel]
		print(li)
		save_data(li)
		print("done:::"+host)

# handle_excel.py
# @Date: 2019-02-27 17:44:58
import pandas as pd 
def save_data(li):
	df = pd.DataFrame([li], columns=['scheme','method','host','path','headers','query','timestamp_start','channel'])
	df.to_csv('request_data/data.csv', mode='a', header=False)	

mitmproxy-env.bat 自動啓動服務。。。

@echo off
start cmd /k  "cd D:規則管理\autoget &d: "
rem mitmdump -p 8080 -s iqiyi_request_save.py
 rem 命令1 & 命令2 & 命令3 ... (無論前面命令是否故障,照樣執行後面)   
 rem 命令1 && 命令2 && 命令3....(僅當前面命令成功時,才執行後面)   
 rem 命令1 || 命令2 || 命令3.... (僅當前面命令失敗時.才執行後面)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章