Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def install(filepath, **kwargs):
"""
Install application on device
:param filepath: the path to file to be installed on target device
:param kwargs: platform specific `kwargs`, please refer to corresponding docs
:return: None
:platforms: Android
"""
return G.DEVICE.install_app(filepath, **kwargs)
def try_log_screen(screen=None):
"""
Save screenshot to file
Args:
screen: screenshot to be saved
Returns:
None
"""
if not ST.LOG_DIR:
return
if screen is None:
screen = G.DEVICE.snapshot()
filename = "%(time)d.jpg" % {'time': time.time() * 1000}
filepath = os.path.join(ST.LOG_DIR, filename)
aircv.imwrite(filepath, screen)
return filename
Args:
query: image template to be found in screenshot
timeout: time interval how long to look for the image template
threshold: default is None
interval: sleep interval before next attempt to find the image template
intervalfunc: function that is executed after unsuccessful attempt to find the image template
Raises:
TargetNotFoundError: when image template is not found in screenshot
Returns:
TargetNotFoundError if image template not found, otherwise returns the position where the image template has
been found in screenshot
"""
G.LOGGING.info("Try finding:\n%s", query)
start_time = time.time()
while True:
screen = G.DEVICE.snapshot(filename=None)
if screen is None:
G.LOGGING.warning("Screen is None, may be locked")
else:
if threshold:
query.threshold = threshold
match_pos = query.match_in(screen)
if match_pos:
try_log_screen(screen)
return match_pos
if intervalfunc is not None:
intervalfunc()
def using(path):
if not os.path.isabs(path):
abspath = os.path.join(ST.PROJECT_ROOT, path)
if os.path.exists(abspath):
path = abspath
G.LOGGING.debug("using path: %s", path)
if path not in sys.path:
sys.path.append(path)
G.BASEDIR.append(path)
def test_connect(self):
old = len(G.DEVICE_LIST)
d = connect_device("Android://localhost:5037/?cap_method=JAVACAP")
self.assertEqual(len(G.DEVICE_LIST) - old, 1)
self.assertIs(d, G.DEVICE)
self.assertEqual(d.cap_method, CAP_METHOD.JAVACAP)
set_current(0)
def filepath(self):
if self._filepath:
return self._filepath
for dirname in G.BASEDIR:
filepath = os.path.join(dirname, self.filename)
if os.path.isfile(filepath):
self._filepath = filepath
return self._filepath
return self.filename
def import_device_cls(platform):
"""lazy import device class"""
platform = platform.lower()
if platform in G.CUSTOM_DEVICES:
cls = G.CUSTOM_DEVICES[platform]
elif platform == "android":
from airtest.core.android.android import Android as cls
elif platform == "windows":
from airtest.core.win.win import Windows as cls
elif platform == "ios":
from airtest.core.ios.ios import IOS as cls
elif platform == "linux":
from airtest.core.linux.linux import Linux as cls
else:
raise RuntimeError("Unknown platform: %s" % platform)
return cls
def device_platform(device=None):
if not device:
device = G.DEVICE
return device.__class__.__name__
def init_device(platform="Android", uuid=None, **kwargs):
"""
Initialize device if not yet, and set as current device.
:param platform: Android, IOS or Windows
:param uuid: uuid for target device, e.g. serialno for Android, handle for Windows, uuid for iOS
:param kwargs: Optional platform specific keyword args, e.g. `cap_method=JAVACAP` for Android
:return: device instance
"""
cls = import_device_cls(platform)
dev = cls(uuid, **kwargs)
for index, instance in enumerate(G.DEVICE_LIST):
if dev.uuid == instance.uuid:
G.LOGGING.warn("Device:%s updated %s -> %s" % (dev.uuid, instance, dev))
G.DEVICE_LIST[index] = dev
break
else:
G.add_device(dev)
return dev
def log(message, traceback=""):
"""
Insert user log, will be displayed in Html report.
:param message: log message
:param traceback: log traceback if exists, use traceback.format_exc to get best format
:return: None
"""
if G.LOGGER:
G.LOGGER.log("info", {"name": message, "traceback": traceback}, 0)