Skip to content

Commit 209d48f

Browse files
committed
reafactor: get_text()
1 parent b098195 commit 209d48f

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

core/utils/device/device.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import time
34

@@ -50,15 +51,27 @@ def is_text_visible(self, text):
5051
if self.type is DeviceType.EMU or self.type is DeviceType.ANDROID:
5152
is_visible = Adb.is_text_visible(id=self.id, text=text)
5253
if self.type is DeviceType.SIM:
53-
# Try to find text with macOS automation
5454
is_visible = SimAuto.is_text_visible(self, text)
55-
# Retry find with ORC if macOS automation fails
56-
if not is_visible:
57-
is_visible = Simctl.is_text_visible(id=self.id, text=text)
5855
if self.type is DeviceType.IOS:
5956
is_visible = IDevice.is_text_visible(id=self.id, text=text)
57+
58+
# Retry find with ORC if macOS automation fails
59+
if not is_visible:
60+
actual_text = self.get_text()
61+
if text in actual_text:
62+
is_visible = True
63+
else:
64+
Log.info('Current text on {0}: {1}{2}'.format(self.id, os.linesep, actual_text))
65+
6066
return is_visible
6167

68+
def get_text(self):
69+
img_name = "actual_{0}_{1}.png".format(self.id, time.time())
70+
actual_image_path = os.path.join(Settings.TEST_OUT_IMAGES, img_name)
71+
File.clean(actual_image_path)
72+
self.get_screen(path=actual_image_path, log_level=logging.DEBUG)
73+
return ImageUtils.get_text(image_path=actual_image_path)
74+
6275
def wait_for_text(self, text, timeout=30, retry_delay=1):
6376
t_end = time.time() + timeout
6477
found = False
@@ -72,12 +85,16 @@ def wait_for_text(self, text, timeout=30, retry_delay=1):
7285
else:
7386
Log.info(error_msg + ' Waiting ...')
7487
time.sleep(retry_delay)
88+
if not found:
89+
text = self.get_text()
90+
Log.info('Current text: {0}'.format(text))
7591
assert found, error_msg
7692

77-
def get_screen(self, path):
93+
def get_screen(self, path, log_level=logging.INFO):
7894
"""
7995
Save screen of mobile device.
8096
:param path: Path to image that will be saved.
97+
:param log_level: Log level.
8198
"""
8299
File.clean(path)
83100
base_path, file_name = os.path.split(path)
@@ -96,7 +113,8 @@ def get_screen(self, path):
96113
if size > 10:
97114
image_saved = True
98115
if image_saved:
99-
Log.info("Image of {0} saved at {1}".format(self.id, path))
116+
message = "Image of {0} saved at {1}".format(self.id, path)
117+
Log.log(level=log_level, message=message)
100118
else:
101119
message = "Failed to save image of {0} saved at {1}".format(self.id, path)
102120
Log.error(message)
@@ -117,7 +135,7 @@ def screen_match(self, expected_image, tolerance=0.1, timeout=30):
117135
diff_image = None
118136
while time.time() < t_end:
119137
actual_image = expected_image.replace('.png', '_actual.png')
120-
self.get_screen(path=actual_image)
138+
self.get_screen(path=actual_image, log_level=logging.DEBUG)
121139
result = ImageUtils.image_match(actual_image=actual_image,
122140
expected_image=expected_image,
123141
tolerance=tolerance)
@@ -139,13 +157,13 @@ def screen_match(self, expected_image, tolerance=0.1, timeout=30):
139157
Log.info('Expected image not found!')
140158
Log.info('Actual image will be saved as expected: ' + expected_image)
141159
time.sleep(timeout)
142-
self.get_screen(path=expected_image)
160+
self.get_screen(path=expected_image, log_level=logging.DEBUG)
143161
assert False, "Expected image not found!"
144162

145163
def get_pixels_by_color(self, color):
146164
image_path = os.path.join(Settings.TEST_OUT_IMAGES, self.name,
147165
'screen_{0}.png'.format(int(time.time() * 1000)))
148-
self.get_screen(image_path)
166+
self.get_screen(image_path, log_level=logging.DEBUG)
149167
return ImageUtils.get_pixels_by_color(image_path, color)
150168

151169
# noinspection PyShadowingBuiltins
@@ -170,7 +188,7 @@ def wait_for_color(self, color, pixel_count, delta=10, timeout=30):
170188

171189
def get_main_color(self):
172190
image_path = os.path.join(Settings.TEST_OUT_IMAGES, self.name, 'screen_{0}.png'.format(int(time.time() * 1000)))
173-
self.get_screen(image_path)
191+
self.get_screen(image_path, log_level=logging.DEBUG)
174192
return ImageUtils.get_main_color(image_path)
175193

176194
# noinspection PyUnresolvedReferences

core/utils/device/idevice.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ def get_devices():
1010
@staticmethod
1111
def is_text_visible(id, text):
1212
return True
13+
14+
@staticmethod
15+
def get_screen(id, file_path):
16+
return None

core/utils/device/simctl.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import time
44

55
from core.log.log import Log
6-
from core.settings import Settings
76
from core.utils.file_utils import File
8-
from core.utils.image_utils import ImageUtils
97
from core.utils.process import Run
108
from core.utils.wait import Wait
119

@@ -109,24 +107,3 @@ def get_screen(id, file_path):
109107
result = Simctl.__run_simctl_command('io {0} screenshot {1}'.format(id, file_path))
110108
assert result.exit_code == 0, 'Failed to get screenshot of {0}'.format(id)
111109
assert File.exists(file_path), 'Failed to get screenshot of {0}'.format(id)
112-
113-
@staticmethod
114-
def get_screen_text(id):
115-
img_name = "actual_{0}_{1}.png".format(id, time.time())
116-
actual_image_path = os.path.join(Settings.TEST_OUT_IMAGES, img_name)
117-
File.clean(actual_image_path)
118-
Simctl.get_screen(id=id, file_path=actual_image_path)
119-
return ImageUtils.get_text(image_path=actual_image_path)
120-
121-
@staticmethod
122-
def is_text_visible(id, text, case_sensitive=False):
123-
if case_sensitive:
124-
if text in Simctl.get_screen_text(id=id):
125-
return True
126-
else:
127-
return False
128-
else:
129-
if text.lower() in Simctl.get_screen_text(id=id).lower():
130-
return True
131-
else:
132-
return False

0 commit comments

Comments
 (0)