Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assert_template(self, v, msg=""):
"""
Assert target exists on the current page.
Args:
v: target to touch, either a Template instance
Raise:
AssertionError - if target not found.
Returns:
Position of the template.
"""
if isinstance(v, Template):
try:
pos = loop_find(v, timeout=ST.FIND_TIMEOUT, driver=self)
except TargetNotFoundError:
raise AssertionError("Target template not found on screen.")
else:
return pos
else:
raise IsNotTemplateError("args is not a template")
else:
devices = []
print("do not connect device")
# set base dir to find tpl
dirpath, _ = script_dir_name(args.script)
# set log dir
if args.log:
args.log = script_log_dir(dirpath, args.log)
print("save log in '%s'" % args.log)
else:
print("do not save log")
# guess project_root to be basedir of current .air path
project_root = os.path.dirname(args.script) if not ST.PROJECT_ROOT else None
auto_setup(dirpath, devices, args.log, project_root)
def gen_screen_log(self, element=None):
if ST.LOG_DIR is None:
return None
jpg_file_name = str(int(time.time())) + '.jpg'
jpg_path = os.path.join(ST.LOG_DIR, jpg_file_name)
self.save_screenshot(jpg_path)
saved = {"screen": jpg_file_name}
if element:
size = element.size
location = element.location
x = size['width'] / 2 + location['x']
y = size['height'] / 2 + location['y']
if "darwin" in sys.platform:
x, y = x * 2, y * 2
saved.update({"args": [[x, y]],})
log_in_func(saved)
dirname = dirname.strip(os.path.sep).replace(os.path.sep, "_").replace(cls.SCRIPTEXT, "_sub")
return dirname
def _copy_script(src, dst):
if os.path.isdir(dst):
shutil.rmtree(dst, ignore_errors=True)
os.mkdir(dst)
for f in os.listdir(src):
srcfile = os.path.join(src, f)
if not (os.path.isfile(srcfile) and f.endswith(cls.TPLEXT)):
continue
dstfile = os.path.join(dst, f)
shutil.copy(srcfile, dstfile)
# find script in PROJECT_ROOT
scriptpath = os.path.join(ST.PROJECT_ROOT, scriptpath)
# copy submodule's images into sub_dir
sub_dir = _sub_dir_name(scriptpath)
sub_dirpath = os.path.join(cls.args.script, sub_dir)
_copy_script(scriptpath, sub_dirpath)
# read code
pyfilename = os.path.basename(scriptpath).replace(cls.SCRIPTEXT, ".py")
pyfilepath = os.path.join(scriptpath, pyfilename)
pyfilepath = os.path.abspath(pyfilepath)
with open(pyfilepath, 'r', encoding='utf8') as f:
code = f.read()
# replace tpl filepath with filepath in sub_dir
code = re.sub("[\'\"](\w+.png)[\'\"]", "\"%s/\g<1>\"" % sub_dir, code)
exec(compile(code.encode("utf8"), pyfilepath, 'exec'), cls.scope)
def _cv_match(self, screen):
# in case image file not exist in current directory:
image = self._imread()
image = self._resize_image(image, screen, ST.RESIZE_METHOD)
ret = None
for method in ST.CVSTRATEGY:
# get function definition and execute:
func = MATCHING_METHODS.get(method, None)
if func is None:
raise InvalidMatchingMethodError("Undefined method in CVSTRATEGY: '%s', try 'kaze'/'brisk'/'akaze'/'orb'/'surf'/'sift'/'brief' instead." % method)
else:
ret = self._try_match(func, image, screen, threshold=self.threshold, rgb=self.rgb)
if ret:
break
return ret
def set_logdir(dirpath):
"""set log dir for logfile and screenshots.
Args:
dirpath: directory to save logfile and screenshots
Returns:
"""
if not os.path.exists(dirpath):
os.mkdir(dirpath)
ST.LOG_DIR = dirpath
G.LOGGER.set_logfile(os.path.join(ST.LOG_DIR, ST.LOG_FILE))
def set_logdir(dirpath):
"""set log dir for logfile and screenshots.
Args:
dirpath: directory to save logfile and screenshots
Returns:
"""
if not os.path.exists(dirpath):
os.mkdir(dirpath)
ST.LOG_DIR = dirpath
G.LOGGER.set_logfile(os.path.join(ST.LOG_DIR, ST.LOG_FILE))
def airtest_touch(self, v):
"""
Perform the touch action on the current page by image identification.
Args:
v: target to touch, either a Template instance or absolute coordinates (x, y)
Returns:
Finial position to be clicked.
"""
if isinstance(v, Template):
_pos = loop_find(v, timeout=ST.FIND_TIMEOUT, driver=self)
else:
_pos = v
x, y = _pos
# self.action_chains.move_to_element_with_offset(root_element, x, y)
# self.action_chains.click()
pos = self._get_left_up_offset()
pos = (pos[0] + x, pos[1] + y)
self._move_to_pos(pos)
self._click_current_pos()
time.sleep(1)
return _pos
def match_all_in(self, screen):
image = self._imread()
image = self._resize_image(image, screen, ST.RESIZE_METHOD)
return self._find_all_template(image, screen)
def gen_screen_log(self, element=None):
if ST.LOG_DIR is None:
return None
jpg_file_name = str(int(time.time())) + '.jpg'
jpg_path = os.path.join(ST.LOG_DIR, jpg_file_name)
self.save_screenshot(jpg_path)
saved = {"screen": jpg_file_name}
if element:
size = element.size
location = element.location
x = size['width'] / 2 + location['x']
y = size['height'] / 2 + location['y']
if "darwin" in sys.platform:
x, y = x * 2, y * 2
saved.update({"args": [[x, y]],})
log_in_func(saved)