Python - 雷電模擬器控制臺(tái)(Dnconsole)
1.Dnconsole控制臺(tái)
雷電模擬器9.0
雷電模擬器9.0,32+64位并行,支持對(duì)畫(huà)質(zhì)、性能要求較高的一類游戲。關(guān)于使用Python實(shí)現(xiàn)雷電模擬器的控制代碼,主要適用于雷電模擬器9.0版本。不同的模擬器版本的控制臺(tái)程序命名也不同(9.0版本為“l(fā)dconsole.exe”),可以使用的指令也不同,通常越新的版本可使用的命令越多。
雷電模擬器官網(wǎng):https://www.ldmnq.com/
2.Dnconsole模塊代碼(Python)
控制臺(tái)類構(gòu)造方法:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
class Dnconsole:
'''
【雷電控制臺(tái)類】
version: 9.0
import該文件會(huì)自動(dòng)實(shí)例化為 Dc
'''
def __init__( self, installation_path:str ):
'''
【構(gòu)造方法】
'''
# if 模擬器安裝路徑存在性檢測(cè)
if os.path.exists(installation_path) is False:
print('模擬器安裝路徑不存在!')
# 獲取模擬器安裝路徑
self.ins_path = installation_path
# Dnconsole程序路徑
self.console_path = self.ins_path + r'\ldconsole.exe '
# if Dnconsole程序路徑檢測(cè)
if os.path.exists(self.console_path) is False:
print('Dnconsole程序路徑不存在!\n請(qǐng)確認(rèn)模擬器安裝文件是否完整或模擬器版本是否不符!')
# adb程序路徑
self.adb_path = self.ins_path + r'\adb.exe '
# if adb程序路徑檢測(cè)
if os.path.exists(self.adb_path) is False:
print('Dnconsole程序路徑不存在!\n請(qǐng)確認(rèn)模擬器安裝文件是否完整!')
# 模擬器截屏程序路徑
self.screencap_path = r'/system/bin/screencap'
# 模擬器截圖保存路徑
self.devicess_path = r'/sdcard/autosS.png'
# 本地圖片保存路徑
self.images_path = r'D:\PycharmWorkspace\images'
# 構(gòu)造完成
print('Class-Dnconsole is ready.(%s)' % (self.ins_path))
控制臺(tái)執(zhí)行:
def CMD( self, cmd:str ):
'''
【執(zhí)行控制臺(tái)命令語(yǔ)句】
:param cmd: 命令
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
CMD = self.console_path + cmd # 控制臺(tái)命令
process = os.popen(CMD)
result = process.read()
process.close()
return result
def ADB( self, cmd:str ):
'''
【執(zhí)行ADB命令語(yǔ)句】
:param cmd: 命令
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
CMD = self.adb_path + cmd # adb命令
process = os.popen(CMD)
result = process.read()
process.close()
return result
關(guān)閉模擬器:
def quit( self, index:int = 0 ):
'''
【關(guān)閉模擬器】
:param index: 模擬器序號(hào)
'''
cmd = 'quit --index %d' %(index)
self.CMD(cmd)
def quitall(self):
'''
【關(guān)閉所有模擬器】
'''
cmd = 'quitall'
self.CMD(cmd)
啟動(dòng)模擬器:
def launch( self, index:int = 0 ):
'''
【啟動(dòng)模擬器】
:param index: 模擬器序號(hào)
:return: True=已啟動(dòng) / False=不存在
'''
cmd = 'launch --index %d' %(index)
if self.CMD(cmd) == '': return True
else: return False
重啟模擬器:
def reboot( self, index:int = 0 ):
'''
【重啟模擬器】
:param index: 模擬器序號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'reboot --index %d' %(index)
return self.CMD(cmd)
查看模擬器列表:
def list(self):
'''
【獲取模擬器列表(僅標(biāo)題)】
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'list'
return self.CMD(cmd)
查看模擬器運(yùn)行狀態(tài):
def runninglist(self):
'''
【獲取正在運(yùn)行的模擬器列表(僅標(biāo)題)】
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'runninglist'
return self.CMD(cmd)
def isrunning( self, index:int = 0 ):
'''
【檢測(cè)模擬器是否啟動(dòng)】
:param index: 模擬器序號(hào)
:return: True=已啟動(dòng) / False=未啟動(dòng)
'''
cmd = 'isrunning --index %d' %(index)
if self.CMD(cmd) == 'running': return True
else: return False
查看模擬器詳細(xì)信息列表:
def list2(self):
'''
【取模擬器列表】
:return: 列表(索引、標(biāo)題、頂層句柄、綁定句柄、是否進(jìn)入android、進(jìn)程PID、VBox進(jìn)程PID)
'''
cmd = 'list2'
return self.CMD(cmd)
添加、復(fù)制、移除及重命名模擬器:
def add( self, name:str ):
'''
【添加模擬器】
:param name: 新模擬器標(biāo)題
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'add %s' %(name)
return self.CMD(cmd)
def copy( self, name:str, index:int ):
'''
【復(fù)制模擬器】
:param name: 新模擬器標(biāo)題
:param index: 原模擬器序號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
if not name:
cmd = 'copy --from %d' %(index)
else:
cmd = 'copy --name %s --from %d' %(name, index)
return self.CMD(cmd)
def remove( self, index:int ):
'''
【移除模擬器】
:param index: 模擬器序號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'remove --index %d' %(index)
return self.CMD(cmd)
def rename( self, index:int, newtitle:str ):
'''
【重命名模擬器】
:param index: 模擬器序號(hào)
:param newtitle: 模擬器新標(biāo)題
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'rename --index %d --title %s' %(index, newtitle)
return self.CMD(cmd)
修改模擬器配置:
def modifyResolution( self, index:int, width, height, dpi ):
'''
【修改模擬器配置 - 分辨率】
:param index: 模擬器序號(hào)
:param width: 顯示寬度
:param height: 顯示高度
:param dpi: 每英寸點(diǎn)數(shù)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'modify --index %d --resolution %s,%s,%s' %(index, width, height, dpi)
return self.CMD(cmd)
def modifyCPU( self, index:int, cpu, memory ):
'''
【修改模擬器配置 - CPU與內(nèi)存】
:param index: 模擬器序號(hào)
:param cpu: 模擬器CPU數(shù)量(1,2,3,4)
:param memory: 模擬器內(nèi)存大?。?56,512,768,1024,1536,2048,3072,4096,6144,8192)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'modify --index %d --cpu %s --memory %s' %(index, cpu, memory)
return self.CMD(cmd)
def modifyManufacturer( self, index:int, manufacturer, model, pnumber ):
'''
【修改模擬器配置 - 制造商信息】
:param index: 模擬器序號(hào)
:param manufacturer: 制造商
:param model: 型號(hào)
:param pnumber: 電話號(hào)碼
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'modify --index %d --manufacturer %s --model %s --pnumber %s' %(index, manufacturer, model, pnumber)
return self.CMD(cmd)
def modifyOthers( self, index:int, autorotate, lockwindow, root ):
'''
【修改模擬器配置 - 其他選項(xiàng)】
:param index: 模擬器序號(hào)
:param autorotate: 自動(dòng)旋轉(zhuǎn)(1/0)
:param lockwindow: 鎖定窗口(1/0)
:param root: ROOT(1/0)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'modify --index %d --autorotate %s --lockwindow %s --root %s' %(index, autorotate, lockwindow, root)
return self.CMD(cmd)
App控制:
def installappOfFile( self, index:int, filename:str ):
'''
【安裝App(用文件名)】
:param index: 模擬器序號(hào)
:param filename: 文件名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'installapp --index %d --filename %s' %(index, filename)
return self.CMD(cmd)
def installappOfPkg( self, index:int, packagename:str):
'''
【安裝App(用包名)】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'installapp --index %d --packagename %s' %(index, packagename)
return self.CMD(cmd)
def uninstallapp( self,index:int, packagename:str ):
'''
【卸載App】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'uninstallapp --index %d --packagename %s' %(index, packagename)
return self.CMD(cmd)
def runApp( self, index:int, packagename:str ):
'''
【運(yùn)行App】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'runapp --index %d --packagename %s' %(index, packagename)
return self.CMD(cmd)
def killApp( self, index:int, packagename:str ):
'''
【終止App】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'killapp --index %d --packagename %s' %(index, packagename)
return self.CMD(cmd)
模擬操作模擬器:
def actionOfInput( self, index:int, text:str ):
'''
【輸入操作】
:param index: 模擬器序號(hào)
:param text: 文本內(nèi)容
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'action --index %d --key call.input --value "%s"' %(index, text)
return self.CMD(cmd)
def actionOfKeyboard( self, index:int, key:str ):
'''
【按鍵操作】
:param index: 模擬器序號(hào)
:param key: 鍵值(back,home,menu,volumeup,volumedown)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'action --index %d --key call.keyboard --value %s' %(index, key)
return self.CMD(cmd)
def actionOfShake( self, index:int ):
'''
【搖一搖操作】
:param index: 模擬器序號(hào)
:return:
'''
cmd = 'action --index %d --key call.shake --value null' %(index)
return self.CMD(cmd)
def actionOfRebootApp(self,index:int, packagename:str):
'''
【重啟模擬器和App】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'action --index %d --key call.reboot --value %s' %(index, packagename)
return self.CMD(cmd)
def actionOfLocate( self, index:int, Lng, Lat ):
'''
【定位操作】
:param index: 模擬器序號(hào)
:param Lng: 經(jīng)度
:param Lat: 維度
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'action --index %d --key call.locate --value %f,%f' %(index, Lng, Lat)
return self.CMD(cmd)
def actionOfNetwork( self, index:int, ifconnect:bool ):
'''
【網(wǎng)絡(luò)斷連操作】
:param index: 模擬器序號(hào)
:param ifconnect: 是否連網(wǎng)(True/False)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
if ifconnect:
cmd = 'action --index %d --key call.network --value connect' %(index)
else:
cmd = 'action --index %d --key call.network --value offline' %(index)
return self.CMD(cmd)
def actionOfGravity(self, index:int, x:int, y:int, z:int):
'''
【改變重力感應(yīng)操作】
:param index: 模擬器序號(hào)
:param x: x
:param y: y
:param z: z
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'action --index %d --key call.gravity --value %d,%d,%d' %(index, x, y, z,)
return self.CMD(cmd)
def scan( self, index:int, filepath:str ):
'''
【掃描二維碼】
:param index: 模擬器序號(hào)
:param filepath: 圖片路徑
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'scan --index %d --file %s' %(index, filepath)
return self.CMD(cmd)
對(duì)模擬器窗口排版:
def sortWnd(self):
'''
【對(duì)模擬器窗口排版】
'''
cmd = 'sortWnd'
self.CMD(cmd)
文件操作:
def pull( self, index:int, remote:str, local:str ):
'''
【復(fù)制文件】
:param index: 模擬器序號(hào)
:param remote: 模擬器文件路徑
:param local: 本地路徑
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'pull --index %d --remote %s --local %s' %(index, remote, local)
return self.CMD(cmd)
def push( self, index:int, remote:str, local:str ):
'''
【發(fā)送文件】
:param index: 模擬器序號(hào)
:param remote: 模擬器文件路徑
:param local: 本地路徑
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'push --index %d --remote %s --local %s' %(index, remote, local)
return self.CMD(cmd)
全局設(shè)置:
def globalSetting( self, fps:int, audio:int, fastplay:int, cleanmode:int ):
'''
【全局設(shè)置】
:param fps: 幀率(0~60)
:param audio: 音頻(1=開(kāi)/0=關(guān))
:param fastplay: 高幀率模式(1=開(kāi)/0=關(guān))
:param cleanmode: 除廣告模式(1=開(kāi)/0=關(guān))
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'globalsetting --fps %d --audio %d --fastplay %d --cleanmode %d' %(fps, audio, fastplay, cleanmode)
return self.CMD(cmd)
啟動(dòng)模擬器及App(新命令):
def launchx( self, index:int, packagename:str ):
'''
【同時(shí)啟動(dòng)模擬器和App】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'launchex --index %d --packagename "%s"' %(index, packagename)
return self.CMD(cmd)
查看連接的設(shè)備:
def device(self):
'''
【列出所有連接的設(shè)備】
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'devices -l'
return self.ADB(cmd)
連接/斷開(kāi)設(shè)備:
def connect( self, ip:str, port:str ):
'''
【連接設(shè)備】
:param ip: ip地址
:param port: 端口號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'connect %s:%s' %(ip, port)
return self.ADB(cmd)
def disconnect( self, ip:str, port:str ):
'''
【斷開(kāi)設(shè)備】
:param ip: ip地址
:param port: 端口號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
if ip != '' and port != '':
cmd = 'disconnect %s:%s' %(ip, port)
elif ip != '' and port == '':
cmd = 'disconnect %s' %(ip)
else:
cmd = 'disconnect'
return self.ADB(cmd)
查看adb版本:
def versionOfADB(self):
'''
【查看ADB版本號(hào)】
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'version'
return self.ADB(cmd)
查看設(shè)備相關(guān):
def dumpstate( self, index:int ):
'''
【獲取設(shè)備系統(tǒng)狀態(tài)信息(需要root權(quán)限)】
:param index: 模擬器序號(hào)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'adb --index %d --command "shell dumpstate"' %(index)
return self.CMD(cmd)
def getPackageList( self, index:int ):
'''
【獲取設(shè)備包名列表】
:param index: 模擬器序號(hào)
:return: 包名列表
'''
cmd = 'adb --index %d --command "shell pm list package"' %(index)
return self.CMD(cmd)
def getResolution( self, index:int ):
'''
【獲取設(shè)備分辨率】
:param index: 模擬器序號(hào)
:return: 分辨率(例如'1920x1080')
'''
cmd = 'adb --index %d --command "shell wm size"' %(index)
return self.CMD(cmd).replace('Physical size: ', '')
截屏(并保存截圖到PC本地):
def screenShot ( self, index:int ):
'''
【截屏并保存到本地】
:param index: 模擬器序號(hào)
'''
cmd1 = 'adb --index %d --command "shell %s -p %s"' %(index, self.screencap_path, self.devicess_path)
cmd2 = 'adb --index %d --command "pull %s %s"' %(index, self.devicess_path, self.images_path)
self.CMD(cmd1)
self.CMD(cmd2)
查看App信息:
def appVersion( self, index:int, packagename:str ):
'''
【獲取App版本號(hào)】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'adb --index %d --command "shell dumpsys package %s|grep versionName"' %(index, packagename)
return self.CMD(cmd)
def appIsrunning( self, index:int, packagename:str ):
'''
【獲取App運(yùn)行狀態(tài)】
:param index: 模擬器序號(hào)
:param packagename: 包名
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'adb --index %d --command "shell pidof %s"' %(index, packagename)
return self.CMD(cmd)
模擬操作模擬器(adb命令):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-401698.html
def actionOfTap( self, index:int, x:int, y:int ):
'''
【點(diǎn)擊操作】
:param index: 模擬器序號(hào)
:param x: x
:param y: y
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'adb --index %d --command "shell input tap %d %d"' %(index, x, y)
return self.CMD(cmd)
def actionOfSwipe( self, index:int, x0:int, y0:int, x1:int, y1:int, ms:int = 200 ):
'''
【滑動(dòng)操作】
:param index: 模擬器序號(hào)
:param x0,y0: 起點(diǎn)坐標(biāo)
:param x1,y1: 終點(diǎn)坐標(biāo)
:param ms: 滑動(dòng)時(shí)長(zhǎng)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
cmd = 'adb --index %d --command "shell input swipe %d %d %d %d %d"' %(index, x0, y0, x1, y1, ms)
return self.CMD(cmd)
def actionOfKeyCode( self, index:int, keycode:int ):
'''
【鍵碼輸入操作】
:param index: 模擬器序號(hào)
:param keycode: 鍵碼(0~9,10=空格)
:return: 控制臺(tái)調(diào)試內(nèi)容
'''
try:
list = ['KEYCODE_0', 'KEYCODE_1', 'KEYCODE_2', 'KEYCODE_3', 'KEYCODE_4', 'KEYCODE_5',
'KEYCODE_6', 'KEYCODE_7', 'KEYCODE_8', 'KEYCODE_9', 'KEYCODE_HOME']
cmd = 'adb --index %d --command "shell input keyevent %s"' %(index, list[keycode])
return self.CMD(cmd)
except Exception as e:
print(e)
實(shí)例化控制臺(tái)類:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-401698.html
# 實(shí)例化控制臺(tái)類
global Dc
Dc = Dnconsole(r'D:\Games\leidian9.0.35\LDPlayer9.0.35')
到了這里,關(guān)于Python - 控制雷電模擬器(Dnconsole)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!