import os
import sys
import subprocess
from time import sleep
import logging
logging.basicConfig(level=logging.DEBUG)
class ScreenCapture():
def get_screen_size(self):
"""获取手机分辨率"""
action = subprocess.Popen(['adb', 'shell', 'wm', 'size'], stdout=subprocess.PIPE)
result = action.stdout.read()
_, size = result.decode().split(':')
self.width,self.height = size.strip().split('x')
print(f'获取到手机分辨率为:{self.width},{self.height}')
self.x1=int(self.width)/2
self.x2=int(self.width)/2
self.y1=int(self.height)/4
self.y2=int(self.height)/1.5
print(f"x1是{self.x1},x2是{self.x2},y1是{self.y1},y2是{self.y2}")
@staticmethod
def show_menu():
print("========================================================== ")
print("|| <1>远程连接设备 <2>向左滑动 <3>向右滑动 ||\n")
print("|| <4>向上滑动 <5>向下滑动 <6>跑Monkey ||\n")
print("|| <7>单场景chrome测试 <8> <9> ||\n")
print(" ========================================================= \n")
def up_swipe(self):
up_num = int(input("向上滑动多少次,用数字替代:"))
interval_time = int(input("输入间隔的时间:"))
for i in range(up_num):
subprocess.Popen(
['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],
stdout=subprocess.PIPE)
print("第{}次滑动".format(i + 1))
sleep(interval_time)
print("滑动结束,退出。。")
sys.exit()
def down_swipe(self):
up_num = int(input("向下滑动多少次,用数字替代:"))
interval_time=int(input("输入间隔的时间:"))
for i in range(up_num):
subprocess.Popen(
['adb', '-s',{self.ip_input},'shell', 'input', 'swipe', str(self.x1), str(self.y2), str(self.x1), str(self.y1), '500'],
stdout=subprocess.PIPE)
sleep(interval_time)
print("滑动结束,退出。。")
sys.exit()
def left_swipe(self):
x1=str(int(self.width)*0.8)
x2=str(int(self.width)*0.2)
y1=str(int(self.height)*0.5)
print(x1)
print("滑动开始前")
up_num = int(input("向左滑动多少次,用数字替代:"))
for i in range(up_num):
subprocess.Popen(
['adb', 'shell', 'input', 'swipe', x2, y1, x1, y1, '500'],
stdout=subprocess.PIPE)
print("第{}次滑动".format(i+1))
sleep(3)
print("滑动结束,退出。。")
sys.exit()
def right_swipe(self):
x1 = str(int(self.width) * 0.8)
x2 = str(int(self.width) * 0.2)
y1 = str(int(self.height) * 0.5)
print(x1)
print("滑动开始前")
up_num = int(input("向左滑动多少次,用数字替代:"))
for i in range(up_num):
subprocess.Popen(
['adb', 'shell', 'input', 'swipe', x1, y1, x2, y1, '300'],
stdout=subprocess.PIPE)
print("第{}次滑动".format(i + 1))
sleep(3)
print("滑动结束,退出。。")
sys.exit()
def get_device_id(self):
self.adb_devices=os.popen("adb devices").read()
self.adb_output = self.adb_devices.split("List of devices attached\n", 1)[1]
lines = self.adb_output.strip().split('\n')
self.device_ids = []
for line in lines:
if line and "device" in line:
device_id, _ = line.split(maxsplit=1)
self.device_ids.append(device_id.strip())
self.device_ids=self.device_ids[0]
logging.info(f'device_ids号是: {self.device_ids}')
return self.device_ids
def adb_connect(self, device_ip):
adb_command = ['adb', 'connect', f'{device_ip}:5555']
try:
subprocess.Popen("adb tcpip 5555", stdout=subprocess.PIPE)
output = subprocess.check_output(adb_command, shell=True, stderr=subprocess.STDOUT)
print(f"Connected to device at {device_ip}:5555")
print(output.decode())
except:
print(f"Failed to connect to device at {device_ip}:5555", "请检查错误")
sys.exit()
def get_and_connect_ip(self):
ip_command = f'adb -s {self.get_device_id()} shell ip route'
result = subprocess.check_output(ip_command, shell=True, text=True)
ip_address = result.strip('\n').strip(' ').split(' ')[-1]
print(f"Detected IP address: {ip_address},远程连接需拔掉USB设备")
self.ip_input = input("输入你的IP地址(或者按回车使用检测到的IP):")
if not self.ip_input:
self.ip_input = ip_address
self.adb_connect(self.ip_input)
self.run()
def chrome_switch(self):
os.system("adb devices")
while True:
os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://baidu.com"')
sleep(5)
for i in range(20):
subprocess.Popen(
['adb', '-s',self.ip_input,'shell', 'input', 'swipe', str(self.x1), str(self.y2),
str(self.x1), str(self.y1), '500'],
stdout=subprocess.PIPE)
sleep(3)
os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://163.com"')
sleep(5)
for i in range(20):
subprocess.Popen(
['adb', '-s', self.ip_input, 'shell', 'input', 'swipe', str(self.x1), str(self.y2),
str(self.x1),
str(self.y1), '500'],
stdout=subprocess.PIPE)
sleep(3)
def run(self):
ScreenCapture.show_menu()
num = int(input("输入你要选择的功能,用数字替代:"))
action_mapping = {
1: self.get_and_connect_ip,
2: self.left_swipe,
3: self.right_swipe,
4: self.up_swipe,
5: self.down_swipe,
7: self.chrome_switch,
}
if num in action_mapping:
action_mapping[num]()
elif num == 6:
pass
else:
print("\n\t输入有误!!!\t\n")
self.run()
if __name__ == '__main__':
ScreenCapture().run()