def _saveScreen(self, filename, random_name=True, tempdir=True): # use last snapshot file if self._snapshot_file and self._keep_capture: return self._snapshot_file if random_name: filename = base.random_name(filename) if tempdir: filename = os.path.join(self._tmpdir, filename) parent_dir = os.path.dirname(filename) or '.' if not os.path.exists(parent_dir): base.makedirs(parent_dir) self.dev.snapshot(filename) if self._device == 'windows': return filename rotation = self._getRotation() # the origin screenshot is UP, so need to rotate it here for human if rotation != 'UP': angle = dict(RIGHT=Image.ROTATE_90, LEFT=Image.ROTATE_270).get(rotation) Image.open(filename).transpose(angle).save(filename) self._log(dict(type='snapshot', filename=filename)) self._snapshot_file = filename return filename
def get_jsonlog(filename='log/airtest.log'): logfile = os.getenv('AIRTEST_LOGFILE', filename) if os.path.exists(logfile): backfile = logfile+'.'+time.strftime('%Y%m%d%H%M%S') os.rename(logfile, backfile) else: base.makedirs(base.dirname(logfile)) jlog = jsonlog.JSONLog(logfile) return jlog
def __init__(self, serialno=None, pkgname=None): self._last_point = None self._threshold = 0.3 # for findImage self._rotation = None # UP,DOWN,LEFT,RIGHT self._tmpdir = 'tmp' if not os.path.exists(self._tmpdir): base.makedirs(self._tmpdir) self.pkgname = pkgname self.adb, self._serialno = ViewClient.connectToDeviceOrExit(verbose=False, serialno=serialno) self.adb.reconnect = True # this way is more stable w = self.adb.getProperty("display.width") h = self.adb.getProperty("display.height") self._width = min(w, h) self._height = max(w, h) self.vc = ViewClient(self.adb, serialno) ViewClient.connectToDeviceOrExit() brand = self.adb.getProperty('ro.product.brand') serialno = self.adb.getProperty('ro.boot.serialno') log.debug('wake phone: brand:{brand}, serialno:{serialno}'.format( brand=brand, serialno=self._serialno)) try: self.adb.wake() if not self.adb.isScreenOn(): time.sleep(1) log.debug('isScreenOn: %s', self.adb.isScreenOn()) if self.adb.isLocked(): (w, h) = self._getShape() self.drag((w*0.2, h*0.5), (w*0.6, h*0.5)) except: print 'Device not support screen detect' @patch.go def monitor(interval=3): log.debug('MONITOR started') if not self.pkgname: log.debug('MONITOR finished, no package provided') return while True: start = time.time() mem = self._getMem() jlog.writeline({'type':'record', 'mem':mem}) cpu = self._getCpu() jlog.writeline({'type':'record', 'cpu':cpu}) dur = time.time()-start if interval > dur: time.sleep(interval-dur) monitor()
def writeline(self, d, *args): ''' @param d (dict or string): content needed write to file @param args(array): only when d is string, support writeline('hello %s', 'world') ''' with Lock(self._lock) as _: base.makedirs(base.dirname(self._filename)) with open(self._filename, 'a') as file: if isinstance(d, dict): d.update({'timestamp': int(time.time())}) outline = json.dumps(d) else: outline = str(d) % args file.write(outline.rstrip() + '\n')
if d == 'UP': return x, y if d == 'DOWN': return w-x, y-y if d == 'RIGHT': return y, w-x if d == 'LEFT': return h-y, x # prepare log and tmp dir logfile = os.getenv('AIRTEST_LOGFILE', 'log/airtest.log') if os.path.exists(logfile): backfile = logfile+'.'+time.strftime('%Y%m%d%H%M%S') os.rename(logfile, backfile) else: base.makedirs(base.dirname(logfile)) jlog = jsonlog.JSONLog(logfile) @patch.record(jlog) class AndroidDevice(object): def __init__(self, serialno=None, pkgname=None): self._last_point = None self._threshold = 0.3 # for findImage self._rotation = None # UP,DOWN,LEFT,RIGHT self._tmpdir = 'tmp' if not os.path.exists(self._tmpdir): base.makedirs(self._tmpdir) self.pkgname = pkgname self.adb, self._serialno = ViewClient.connectToDeviceOrExit(verbose=False, serialno=serialno)