Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

79 add ocr dsl #94

Merged
merged 7 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion flybirds/core/dsl/globalization/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@
],
"from {param1} find[{selector}]element": [
"向{param1}查找[{selector}]的元素"],

"from {param1} find[{selector}]text": [
"向{param1}扫描[{selector}]的文案"],
"from {param1} find[{selector}]image": [
"向{param1}查找[{selector}]的图像"],
"unblock the current page": ["解除当前页面限制"],
"current page is [{param}]": ["当前页面是[{param}]"],
"current page is not last page": ["当前页面已不是上一个指定页面"],
Expand Down
26 changes: 26 additions & 0 deletions flybirds/core/dsl/step/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,32 @@ def full_screen_swipe_to_ele_aaa(context, param1=None, selector=None):
g_Context.step.full_screen_swipe_to_ele_aaa(context, param1, selector)


@step("from {param1} find[{selector}]text")
@ele_wrap
def full_screen_swipe_to_ocr_txt(context, param1=None, selector=None):
"""
Full screen swipe in the specified direction to find the specified
selector element
:param context: step context
:param param1: slide direction (top/bottom/left/right)
:param selector: locator string for selector element (or None).
"""
g_Context.step.full_screen_swipe_to_ocr_txt(context, param1, selector)


@step("from {param1} find[{selector}]image")
@ele_wrap
def full_screen_swipe_to_img(context, param1=None, selector=None):
"""
Full screen swipe in the specified direction to find the specified
selector element
:param context: step context
:param param1: slide direction (top/bottom/left/right)
:param selector: locator string for selector element (or None).
"""
g_Context.step.full_screen_swipe_to_img(context, param1, selector)


@step("clear [{selector}] and input[{param2}]")
@ele_wrap
def ele_clear_input(context, selector=None, param2=None):
Expand Down
2 changes: 1 addition & 1 deletion flybirds/core/global_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def get_ele_locator(key):
return key
ele_locator = all_locators.get(key)
if ele_locator is None:
log.info(
log.debug(
f"the ele_locator.json has no element locator configuration "
f"for [{key}]")
return key
Expand Down
6 changes: 6 additions & 0 deletions flybirds/core/plugin/plugins/default/app_base_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def swipe_to_ele(self, context, p_selector, param2, c_selector):
def full_screen_swipe_to_ele_aaa(self, context, param1, selector):
step_adjust.full_screen_swipe_to_ele_aaa(context, param1, selector)

def full_screen_swipe_to_ocr_txt(self, context, param1, selector):
step_adjust.full_screen_swipe_to_ocr_txt(context, param1, selector)

def full_screen_swipe_to_img(self, context, param1, selector):
step_adjust.full_screen_swipe_to_img(context, param1, selector)

def position_not_change(self, context, selector, param2):
position_not_change(context, selector, param2)

Expand Down
16 changes: 8 additions & 8 deletions flybirds/core/plugin/plugins/default/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def screen_shot(path):
"""
Take a screenshot and save
"""
log.info(f"[screen_shot] screen shot start. path is:{path}")
log.debug(f"[screen_shot] screen shot start. path is:{path}")
cur_platform = g_Context.platform
try:
if cur_platform is None:
Expand All @@ -44,7 +44,7 @@ def screen_shot(path):
"Screenshot failed path: {}, error: {}".format(path, str(e)),
traceback.format_exc(),
)
log.info("[screen_shot] screen shot end!")
log.debug("[screen_shot] screen shot end!")

@staticmethod
def screen_link_to_behave(scenario, step_index, tag=None, link=True):
Expand Down Expand Up @@ -80,13 +80,13 @@ def screen_link_to_behave(scenario, step_index, tag=None, link=True):
feature_name)
else:
current_screen_dir = os.path.join(feature_name)
log.info(f"[screen_link_to_behave] screen_shot_dir path :"
log.debug(f"[screen_link_to_behave] screen_shot_dir path :"
f"{screen_shot_dir} and "
f"current_screen_dir path: {current_screen_dir}")
file_helper.create_dirs_path_object(current_screen_dir)

src_path = "../screenshot/{}/{}".format(feature_name, file_name)
log.info("[screen_link_to_behave] src_path: {}".format(src_path))
log.debug("[screen_link_to_behave] src_path: {}".format(src_path))
data = (
'embeddingsTags, stepIndex={}, <image class ="screenshot"'
' width="375" src="{}" />'.format(step_index, src_path)
Expand All @@ -102,13 +102,13 @@ def image_ocr(img_path):
"""
Take a screenshot and ocr
"""
log.info(f"[image ocr path] image path is:{img_path}")
log.debug(f"[image ocr path] image path is:{img_path}")
ocr = g_Context.ocr_driver_instance
g_Context.ocr_result = ocr.ocr(img_path, cls=True)
g_Context.image_size = Image(img_path).size
log.info(f"[image ocr path] image size is:{g_Context.image_size}")
for line in g_Context.ocr_result:
log.info(f"[image ocr result] scan line info is:{line}")
log.debug(f"[image ocr path] image size is:{g_Context.image_size}")
# for line in g_Context.ocr_result:
# log.info(f"[image ocr result] scan line info is:{line}")
# box = line[0]
# log.info(f"[image ocr result] scan box info is:{box}")
# x = (box[0][0] + box[1][0]) / 2
Expand Down
7 changes: 4 additions & 3 deletions flybirds/core/plugin/plugins/default/step/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"""
Step implement of element click.
"""
import re
import flybirds.core.global_resource as gr
import flybirds.core.plugin.plugins.default.ui_driver.poco.findsnap \
as find_snap
import flybirds.utils.dsl_helper as dsl_helper
from flybirds.core.global_context import GlobalContext as g_Context
from flybirds.core.plugin.plugins.default.step.verify import ocr_txt_exist
from flybirds.core.plugin.plugins.default.step.verify import ocr_txt_contain
from flybirds.core.plugin.plugins.default.step.common import img_verify


Expand Down Expand Up @@ -109,9 +110,9 @@ def click_coordinates(context, x, y):


def click_ocr_text(context, param):
ocr_txt_exist(context, param)
ocr_txt_contain(context, param)
for line in g_Context.ocr_result:
if line[1][0] == param:
if re.search(param, line[1][0], flags=0) is not None:
box = line[0]
x = (box[0][0] + box[1][0]) / 2
y = (box[0][1] + box[2][1]) / 2
Expand Down
96 changes: 96 additions & 0 deletions flybirds/core/plugin/plugins/default/step/page_show_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,99 @@ def full_screen_swipe_to_ele_aaa(context, param1, param2):
distance,
duration,
)


def full_screen_swipe_to_ocr_txt(context, param1, param2):
poco_instance = gr.get_value("pocoInstance")
handled_param2_temp = handle_str(param2)
param2_dict = dsl_helper.params_to_dic(handled_param2_temp)

search_dsl_str = param2_dict["selector"]

if "swipeCount" in param2_dict.keys():
swipe_count = int(param2_dict["swipeCount"])
else:
swipe_count = gr.get_frame_config_value("swipe_search_count", 5)

direction = param1.strip()

screen_size = gr.get_device_size()

start_x = None
if "startX" in param2_dict.keys():
start_x = float(param2_dict["startX"])
start_y = None
if "startY" in param2_dict.keys():
start_y = float(param2_dict["startY"])

if "distance" in param2_dict.keys():
distance = float(param2_dict["distance"])
else:
distance = gr.get_frame_config_value("swipe_search_distance", 0.3)

duration = None
if gr.get_frame_config_value("use_search_swipe_duration", False):
duration = gr.get_frame_config_value("search_swipe_duration", 1)
if "duration" in param2_dict.keys():
duration = float(param2_dict["duration"])

ps.full_screen_swipe_search_ocr(
context,
poco_instance,
search_dsl_str,
swipe_count,
direction,
screen_size,
start_x,
start_y,
distance,
duration,
)


def full_screen_swipe_to_img(context, param1, param2):
poco_instance = gr.get_value("pocoInstance")
handled_param2_temp = handle_str(param2)
param2_dict = dsl_helper.params_to_dic(handled_param2_temp)

search_dsl_str = param2_dict["selector"]

if "swipeCount" in param2_dict.keys():
swipe_count = int(param2_dict["swipeCount"])
else:
swipe_count = gr.get_frame_config_value("swipe_search_count", 5)

direction = param1.strip()

screen_size = gr.get_device_size()

start_x = None
if "startX" in param2_dict.keys():
start_x = float(param2_dict["startX"])
start_y = None
if "startY" in param2_dict.keys():
start_y = float(param2_dict["startY"])

if "distance" in param2_dict.keys():
distance = float(param2_dict["distance"])
else:
distance = gr.get_frame_config_value("swipe_search_distance", 0.3)

duration = None
if gr.get_frame_config_value("use_search_swipe_duration", False):
duration = gr.get_frame_config_value("search_swipe_duration", 1)
if "duration" in param2_dict.keys():
duration = float(param2_dict["duration"])

ps.full_screen_swipe_search_img(
context,
poco_instance,
search_dsl_str,
swipe_count,
direction,
screen_size,
start_x,
start_y,
distance,
duration,
)
41 changes: 26 additions & 15 deletions flybirds/core/plugin/plugins/default/step/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,17 @@ def wait_ocr_text_appear(context, param):
ocr(context)
txts = [line[1][0] for line in g_Context.ocr_result]
fixed_txt = paddle_fix_txt(txts)
if param in fixed_txt:
text_exist = True
else:
for txt in fixed_txt:
if re.search(param, txt, flags=0) is not None:
text_exist = True
if text_exist is False:
if time.time() - start > timeout/2:
time.sleep(timeout/2)
else:
time.sleep(5)
if time.time() - start > timeout:
for line in g_Context.ocr_result:
log.info(f"[image ocr result] scan line info is:{line}")
message = "text not found in {} seconds, expect text:{}" \
.format(timeout, param)
raise FlybirdVerifyException(message)
Expand All @@ -293,19 +296,24 @@ def ocr_txt_exist(context, param):
fixed_txt = paddle_fix_txt(txts)
verify.text_container(param, fixed_txt)
else:
for line in g_Context.ocr_result:
log.info(f"[image ocr result] scan line info is:{line}")
message = "ocr result is null"
raise FlybirdVerifyException(message)


def ocr_txt_contain(context, param):
def ocr_txt_contain(context, param, islog=True):
if len(g_Context.ocr_result) >= 1:
txts = [line[1][0] for line in g_Context.ocr_result]
fixed_txt = paddle_fix_txt(txts)
result = None
result = False
for txt in fixed_txt:
if param in txt:
if re.search(param, txt, flags=0) is not None:
result = True
if result is None:
return result
if result is False and islog is True:
for line in g_Context.ocr_result:
log.info(f"[image ocr result] scan line info is:{line}")
message = "ocr result not contain {}".format(param)
raise FlybirdVerifyException(message)
else:
Expand All @@ -319,6 +327,8 @@ def ocr_txt_not_exist(context, param):
fixed_txt = paddle_fix_txt(txts)
verify.text_not_container(param, fixed_txt)
else:
for line in g_Context.ocr_result:
log.info(f"[image ocr result] scan line info is:{line}")
message = "ocr result is null"
raise FlybirdVerifyException(message)

Expand All @@ -334,22 +344,23 @@ def paddle_fix_txt(txt):
return txt


def img_exist(context, param):
def img_exist(context, param, islog=True):
start = time.time()
step_index = context.cur_step_index - 1
result = img_verify(context, param)
if len(result) == 0:
src_path = "../../../{}".format(param)
data = (
'embeddingsTags, stepIndex={}, <image class ="screenshot"'
' width="375" src="{}" />'.format(step_index, src_path)
)
context.scenario.description.append(data)
# context.cur_step_index += 1
if islog is True:
src_path = "../../../{}".format(param)
data = (
'embeddingsTags, stepIndex={}, <image class ="screenshot"'
' width="375" src="{}" />'.format(step_index, src_path)
)
context.scenario.description.append(data)
raise Exception("[image exist verify] image not found !")
else:
log.info(f"[image exist verify] cost time:{time.time() - start}")
log.info(f"[image exist verify] result:{result}")
return True


def img_not_exist(context, param):
Expand Down
Loading