def popup_get_time_test(): # os.system("adb kill-server") # os.system("adb server") # 启atx os.system("python3 -m uiautomator2 clear-cache") os.system("python3 -m uiautomator2 init") # 连接设备 d = u2.connect_adb_wifi(get_devices_name()) # 启动蜗牛 d.app_stop("com.netease.snailread") d.app_start("com.netease.snailread") sleep(7) if if_daily_popup_exist(d): d(resourceId="com.netease.snailread:id/daily_get_free").click() sleep(7) d.press("back") d(text="书桌").click() sleep(3) if if_activity_popup_exist(d):# 有活动弹窗则先消去 d.press("back") sleep(1) if d.exists(text="120"): print("成功领取120分钟广告时长") elif d.exists(text="60"): print("成功领取60分钟免费时长") else: print("时长领取失败") else: os.system("python3 -m uiautomator2 screenshot screenshot.jpg") print("无领时长弹窗") d.app_stop("com.netease.snailread")
def get_device(): global d # 这里的端口号可以用 adb devices 查看。我的是第2个模拟器开始25,第3个26。 # 强制用端口号连接,有时候在 adb devices 找不到设备时仍可强制连接。 for port in range(62025, 62030): device = f'127.0.0.1:{port}' try: d = uiautomator2.connect_adb_wifi(device) # connect to device break except Exception: pass log.info(formatJSON(d.info))
def __init__(self, ipport, LogStorageAddress): #类内参数赋值 self.__success_count = 0 self.__fail_count = 0 self.__ipport = ipport self.__LogStorageAddress = LogStorageAddress #创建log文件,并且将log写入文件 self.filepath = self.__LogStorageAddress + time.strftime( '%y%m%d_%H%M%S') + '.txt' #创建测试结果日志文件名字 self.log = Logger(self.filepath, level='debug') #将测试日志写入日志文件 #对安卓设备进行连接 self.d = u2.connect_adb_wifi(self.__ipport) # 连接安卓设备
def init_devices(self): return u2.connect_adb_wifi(self.__ipport)
def get_android_driver(pro_name, current_thread_name_index, connected_android_device_list): """ 【 获取'Android'驱动、设备名称 】 :param pro_name :param current_thread_name_index: 当前线程名字的索引 :param connected_android_device_list: 已连接设备信息列表 [ { "thread_index": 1, "device_name": "小米5S", "device_udid": "192.168.31.136:5555"} } , { "thread_index": 2, "device_name": "坚果Pro", "device_udid": "192.168.31.253:4444" } } ] :return: 【 步 骤 】 1.获取 Appium 服务启动应用所需的能力参数 (指定设备,指定应用) 2.通过'当前线程名索引' 获取已连接设备列表中对应的'Android'设备信息和'Appium'服务 3.获取设备驱动 """ # 通过'当前线程名索引' 获取已连接设备列表中对应的'Android'设备信息和'Appium'服务 device_name = None device_udid = None for connected_android_devices_dict in connected_android_device_list: if current_thread_name_index == connected_android_devices_dict[ "thread_index"]: device_udid = connected_android_devices_dict["device_udid"] device_name = connected_android_devices_dict["device_name"] break log.info("\n\n") log.info("device_name -> " + device_name) log.info("device_udid -> " + device_udid) log.info("\n\n") driver = None try: # 连接设备 ADB_WIFI driver = uiautomator2.connect_adb_wifi(device_udid) # 连接设备 WIFI # driver = uiautomator2.connect('192.168.31.136') # driver = uiautomator2.connect_wifi('192.168.31.136') # 连接设备 USB # driver = uiautomator2.connect('15a6c95a') # driver = uiautomator2.connect_usb('15a6c95a') # 配置accessibility服务的最大空闲时间,超时将自动释放 driver.set_new_command_timeout(gv.NEW_COMMAND_TIMEOUT) # 全局默认的元素定位超时时间 driver.implicitly_wait(gv.IMPLICITY_WAIT) # 解锁(点亮屏幕)相当于点击了home健 driver.unlock() except Exception as e: log.error(("显示异常:" + str(e))) if "Uiautomator started failed" in str(e): error_msg = pro_name + " 项目 " + device_name + " 设备 启动 ATX 服务时 未授权" else: error_msg = pro_name + "项目 " + device_name + " 设备 启动 ATX 服务的其他异常情况" send_DD_for_FXC(title=pro_name, text="#### " + error_msg + "") raise Exception(error_msg) finally: return driver, device_name
def __init__(self): self.d = u2.connect_adb_wifi("192.168.1.105:5555")
def connect(): d = u2.connect_adb_wifi("172.16.222.55:5555") # d.healthcheck() d.settings['xpath_debug'] = True # 开启xpath插件的调试功能 d.settings['wait_timeout'] = 20.0 # 默认控件等待时间(原生操作,xpath插件的等待时间) return d
#被调用时,设置家居场景为默认状态 def once_nomal_state(): d(text="场景").click() d(text="自定义").click() #xpath点击有问题,暂时弃用 #d.xpath('//*[@resource-id="com.xiaomi.smarthome:id/ca7"]/android.widget.RelativeLayout[5]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.TextView[1]').click() d.click(0.916, 0.844) return_home() # 构建Socket实例、设置端口号和监听队列大小 listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.bind(('127.0.0.1', 2333)) listener.listen(5) print('等待客户端连接...') #—————————————————————————————————————— #第一次运行启动时,清除adb,连接模拟器,打开米家应用 os.system("adb kill-server") d = u.connect_adb_wifi("127.0.0.1:62001") d.app_start("com.xiaomi.smarthome") #—————————————————————————————————————— #鉴于通过ui包无法合理获取到电灯、插座和窗帘的运行状态,目前快速完成方案为python被unity调用运行时,把所有的灯和窗帘设置为关闭、插座为开启状态。 return_home() once_nomal_state() #—————————————————————————————————————— # 进入死循环,等待新的客户端连入。一旦有客户端连入,就分配一个线程去做专门处理。然后自己继续等待。 while True: client_executor, addr = listener.accept() t = threading.Thread(target=message_receiver, args=(client_executor, addr)) t.start()
# coding: utf-8 # import uiautomator2 as u2 import time import autoit if __name__ == '__main__': ipport = '10.9.40.106:8888' d = u2.connect_adb_wifi(ipport) # 连接安卓设备 uccess_count = 0 fail_count = 0 for i in range(1, 1000): print('——————————test%s——————————' % i) d.app_start( package_name="com.sensetime.sensepass.factoryexam", activity="com.sensetime.sensepass.factoryexam.MainActivity") time.sleep(3) d(resourceId= "com.sensetime.sensepass.factoryexam:id/btn_customized_args_verify" ).click() # 点击定制数据检测 time.sleep(10) #等待mcu版本被读出来 tv_receive_data = d.xpath( '//*[@resource-id="com.sensetime.sensepass.factoryexam:id/tv_exec_cmd_result"]' ).get_text() print(tv_receive_data) flag = tv_receive_data.find('Mcu版本 : 7') if flag != -1: print('测试成功') d(resourceId="com.sensetime.sensepass.factoryexam:id/iv_test_pass" ).click() # da打勾
def adb_screencap(ip, screenshotname, StoragePath): os.popen('adb -s %s shell screencap -p /sdcard/%s.png' % (ip, str(screenshotname))) #开始截取屏幕图片 time.sleep(2) os.popen('adb -s %s pull /sdcard/%s.png %s' % (ip, str(screenshotname), str(StoragePath))) #把截取图片导到电脑指定文件夹里 time.sleep(2) os.popen('adb -s %s shell rm -rf /sdcard/%s.png' % (ip, str(screenshotname))) #图片导完之后,删除设备端图片,防止设备端空间不足 time.sleep(2) if __name__ == '__main__': ip = '10.9.66.153:8888' d = u2.connect_adb_wifi(ip) for i in range(1, 2000): testTime = time.strftime("%y%m%d_%H%M%S") print('——————[%s]:test %s——————' % (testTime, i)) flag = False while not flag: d.long_click(0.422, 0.435, 5.0) flag = d(text="请输入登录密码").exists() print(flag) time.sleep(2) d(resourceId="com.sensetime.sensepassege:id/et_pwd").set_text( "admin1234", timeout=5) time.sleep(2) d(resourceId="com.sensetime.sensepassege:id/tv_confirm").click() d(resourceId="com.sensetime.sensepassege:id/ib_left").click() # 截图
import uiautomator2 as u2 import json d = u2.connect_adb_wifi("172.16.2.12") app = d.session("com.woniu.mobile9yin") # 启动应用并获取session app(resourceId='com.woniu.mobile9yin:id/et_name').click() app.send_keys("") app(resourceId='com.woniu.mobile9yin:id/et_pwd').click() app.send_keys("") app(resourceId='com.woniu.mobile9yin:id/signin_button').click() app.xpath("//*[@text='江湖六区']").click() app.xpath("//*[@text='气贯长虹']").click() app(resourceId='com.woniu.mobile9yin:id/citan_btn').click() app(resourceId='com.woniu.mobile9yin:id/item_citan_btn').click() app(resourceId='com.woniu.mobile9yin:id/citan_jingyewei').click() # xml = app.dump_hierarchy() def get_citan_cishu(app): shengyu = app( resourceId='com.woniu.mobile9yin:id/citan_shengyu_cishu').get_text() zong = app( resourceId='com.woniu.mobile9yin:id/citan_zong_cishu').get_text() return zong - shengyu