def start(self): """ start device """ self.mnc = MNCDevice(self.device_id) self.player = ActionPlayer(self.device_id) self.toolkit = PYAToolkit(self.device_id) logger.debug('FDevice [{}] started'.format(self.device_id))
def setUpClass(cls): if not cls.f_device: cls.f_init_device(cls.f_device_id) cls.f_device_id = cls.f_device.device_id if cls.f_runtime_pic_dir_path is not None: cls.f_save_runtime_pic(cls.f_runtime_pic_dir_path) logger.debug('Case [{}] setup finished'.format(cls.__name__))
def detect(template: str, target: str) -> dict: # load template picture fi.load_template(TEMP_TEMPLATE_NAME, pic_path=template) # and find it result = fi.find(TEMP_TARGET_NAME, target_pic_path=target) fi.clear() logger.debug('Detect result: {}'.format(json.dumps(result))) return result
def f_init_device(cls, device_id: str) -> FDevice: """ init device, and return it """ assert cls.f_device_id, 'should set your device id first, likes `cls.f_device_id="1234F"`' assert not cls.f_device, 'device {} already existed, should not be re-init'.format( device_id) cls.f_device = FDeviceManager.add(device_id) logger.debug('Device [{}] init finished'.format(device_id)) return cls.f_device
def stop(self): """ stop device, and clean up """ self.player and self.player.stop() self.mnc = None self.player = None self.toolkit = None logger.debug('FDevice [{}] stopped'.format(self.device_id))
def add(cls, target_device_id: str) -> FDevice: if not cls.is_device_available(target_device_id): new_device = FDevice(target_device_id) cls._device_dict[target_device_id] = new_device logger.debug('Device [{}] register finished'.format(target_device_id)) return new_device # or, reuse old device logger.debug('Device [{}] already registered, reuse'.format(target_device_id)) return cls._device_dict[target_device_id]
def load(self, pic_dir_path): assert os.path.isdir(pic_dir_path), '{} not a dir'.format(pic_dir_path) for each_pic_path in [ os.path.join(pic_dir_path, i) for i in os.listdir(pic_dir_path) ]: each_f_pic = FPic(each_pic_path) each_pic_name = each_f_pic.name self.f_pic_dict[each_pic_name] = each_f_pic logger.debug('Load picture [{}] from [{}]'.format( each_pic_name, each_pic_path))
def is_device_connected(device_id: str): """ return True if device connected, else return False """ _ADB = config.ADB_EXECUTOR try: device_name = subprocess.check_output( [_ADB, '-s', device_id, 'shell', 'getprop', 'ro.product.model']) device_name = device_name.decode(config.DEFAULT_CHARSET).replace( '\n', '').replace('\r', '') logger.debug('Device [{}] available'.format(device_name)) except subprocess.CalledProcessError: return False return True
def screen_shot(self, save_to=None) -> str: """ screen shot and return its path (YOU SHOULD REMOVE IT BY YOURSELF!) """ self.mnc.screen_shot() # save to specific place if save_to: if os.path.isdir(save_to): pic_name = '{}.png'.format(uuid.uuid1()) final_path = os.path.join(save_to, pic_name) else: final_path = save_to # use tempfile else: temp_pic = tempfile.NamedTemporaryFile('w+', delete=False, suffix='.png') temp_pic_name = temp_pic.name final_path = temp_pic_name self.mnc.export_screen(final_path) logger.debug('Screenshot saved in [{}]'.format(final_path)) return final_path
def f_stop_device(cls): """ stop device after usage """ cls.f_device = None FDeviceManager.remove(cls.f_device_id) logger.debug('Device [{}] stopped'.format(cls.f_device_id))
def tearDownClass(cls): if cls.f_device and cls.f_device_kill_after_usage: cls.f_stop_device() cls.f_device = None cls.f_device_id = None logger.debug('Case [{}] teardown finished'.format(cls.__name__))