Skip to content
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,pycharm,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,pycharm,macos
appium_logs
logs
### macOS ###
# General
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion tests/selenium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_template_matching(self, selenium_driver: TestUIDriver):
"https://github.com/testdevlab/Py-TestUI#image-recognition"
)
selenium_driver.find_image_match(
"resources/comp.png", 0.9, True, image_match="image.png"
"resources/comp.png", 0.9, True, image_match="./logs/image.png"
)
selenium_driver.raise_errors()

Expand Down
34 changes: 12 additions & 22 deletions testui/support/appium_driver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import atexit
import datetime
import os
import subprocess
import threading
import time
from pathlib import Path
from time import sleep

Expand Down Expand Up @@ -411,26 +413,20 @@ def __local_run(url, desired_caps, use_port, udid, log_file):
if udid is None:
desired_caps = __set_android_device(desired_caps, device)
logger.log(f'setting device for automation: {desired_caps["udid"]}')
root_dir = (
os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
+ "/"
)
Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True)
log_dir = os.path.join("./logs", "appium_logs")
Path(log_dir).mkdir(parents=True, exist_ok=True)
file_path: str
if log_file == "appium-stdout.log":
file = f"appium_logs/testui-{udid}-" + log_file
file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file)
else:
file = f"appium_logs/{log_file}"
with open(root_dir + file, "wb") as out:
file_path = os.path.join(log_dir, log_file)
with open(file_path, "wb") as out:
process = subprocess.Popen(
["appium", "-p", port.__str__(), "-bp", bport.__str__()],
stdout=out,
stderr=subprocess.STDOUT,
)
atexit.register(process.kill)
file_path = root_dir + file
while True:
sleep(0.5)
out = open(file_path)
Expand Down Expand Up @@ -465,26 +461,20 @@ def __local_run_ios(url, desired_caps, use_port, udid, log_file):
os.getenv("PYTEST_XDIST_WORKER").split("w")[1]
)
logger.log(f"running: appium -p {port.__str__()}")
root_dir = (
os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
+ "/"
)
Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True)
log_dir = os.path.join("./logs", "appium_logs")
Path(log_dir).mkdir(parents=True, exist_ok=True)
file_path: str
if log_file == "appium-stdout.log":
file = f"appium_logs/testui-{udid}-" + log_file
file_path = os.path.join(log_dir, f"testui-{udid}-{time.time()}-" + log_file)
else:
file = f"appium_logs/{log_file}"
with open(root_dir + file, "wb") as out:
file_path = os.path.join(log_dir, log_file)
with open(file_path, "wb") as out:
process = subprocess.Popen(
["appium", "-p", port.__str__()],
stdout=out,
stderr=subprocess.STDOUT,
)
atexit.register(process.kill)
file_path = root_dir + file
if udid is None:
desired_caps = __set_ios_device(desired_caps, device)
while True:
Expand Down
4 changes: 1 addition & 3 deletions testui/support/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def error_with_traceback(exception):
)
line: str
for line in traceback.extract_stack().format():
if line.__contains__(root_dir) and not line.__contains__(
"traceback.extract_stack()"
):
if root_dir in line and not "traceback.extract_stack()" in line:
exception += logger.bcolors.FAIL + line + logger.bcolors.ENDC
return exception
25 changes: 7 additions & 18 deletions testui/support/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from pathlib import Path

LOG_DIR = "./logs"

class bcolors:
"""
Expand Down Expand Up @@ -145,19 +146,13 @@ def __file_log(log_file="stdout.log"):
:param log_file: String
:return: String
"""
root_dir = (
os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
+ "/"
)
Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True)
Path(LOG_DIR).mkdir(parents=True, exist_ok=True)
file_name: str
if log_file == "stdout.log":
file_name = f"appium_logs/TEST_UI-{log_file}"
file_name = os.path.join(LOG_DIR, f"TEST_UI-{log_file}")
else:
file_name = f"appium_logs/{log_file}"
return root_dir + file_name
file_name = os.path.join(LOG_DIR, log_file)
return file_name


def __file_tests(log_file="report_cases.txt"):
Expand All @@ -166,11 +161,5 @@ def __file_tests(log_file="report_cases.txt"):
:param log_file: String
:return: String
"""
root_dir = (
os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
+ "/"
)
Path(root_dir + "appium_logs").mkdir(parents=True, exist_ok=True)
return root_dir + log_file
Path(LOG_DIR).mkdir(parents=True, exist_ok=True)
return os.path.join(LOG_DIR, log_file)
26 changes: 17 additions & 9 deletions testui/support/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,38 @@ def remove_logs():
"""
Will remove the logs from the previous execution.
"""
root_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
log_dir = "./logs"
i = 0
try:
for filename in os.listdir(root_dir + "/report_screenshots"):
for filename in os.listdir(os.path.join(log_dir, "report_screenshots")):
file_path = os.path.join(
root_dir + "/report_screenshots/", filename
os.path.join(log_dir, "report_screenshots"), filename
)
os.remove(file_path)
if i == 0:
logger.log("Cleaning report_screenshots folder...")
i += 1
except FileNotFoundError:
except (IsADirectoryError, PermissionError, FileNotFoundError):
pass
i = 0
try:
for filename in os.listdir(root_dir + "/appium_logs"):
file_path = os.path.join(root_dir + "/appium_logs/", filename)
for filename in os.listdir(os.path.join(log_dir, "appium_logs")):
file_path = os.path.join(log_dir, filename)
os.remove(file_path)
if i == 0:
logger.log("Cleaning appium_logs folder...")
i += 1
except FileNotFoundError:
except (IsADirectoryError, PermissionError, FileNotFoundError):
pass
i = 0
try:
for filename in os.listdir(log_dir):
file_path = os.path.join(log_dir, filename)
os.remove(file_path)
if i == 0:
logger.log("Cleaning logs folder...")
i += 1
except (IsADirectoryError, PermissionError, FileNotFoundError):
pass


Expand Down
33 changes: 13 additions & 20 deletions testui/support/testui_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def get_dimensions(self):
image_name = f"{self.device_udid}{current_time}.png"
path_s = self.save_screenshot(image_name)
dimensions = ImageRecognition(
original=path_s
original=path_s, path=""
).image_original_size()
logger.log(f"Deleting screenshot: {path_s}")
self.__delete_screenshot(path_s)
Expand All @@ -297,21 +297,19 @@ def save_screenshot(self, image_name=""):
"""
config = self.__configuration

root_dir = config.screenshot_path
log_dir = config.screenshot_path
if not config.screenshot_path:
root_dir = path.dirname(
path.dirname(path.dirname(path.abspath(__file__)))
)
root_dir = path.join(root_dir, "report_screenshots")
log_dir = "./logs"
log_dir = path.join(log_dir, "report_screenshots")

Path(root_dir).mkdir(parents=True, exist_ok=True)
Path(log_dir).mkdir(parents=True, exist_ok=True)

current_time = datetime.now().strftime("%Y-%m-%d%H%M%S")

if not image_name:
image_name = f"ERROR-{self.device_name}-{current_time}.png"

final_path = path.join(root_dir, image_name)
final_path = path.join(log_dir, image_name)

self.get_driver().save_screenshot(final_path)

Expand All @@ -328,9 +326,6 @@ def __delete_screenshot(cls, image_name):
:param image_name:
:return:
"""
root_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
os.remove(image_name)

def get_driver(self) -> WebDriver:
Expand Down Expand Up @@ -436,9 +431,9 @@ def stop_recording_screen(self, file_name="testui-video.mp4"):
"""
file = self.get_driver().stop_recording_screen()
decoded_string = base64.b64decode(file)
root_dir = self.configuration.screenshot_path
logger.log(f"Recording stopped in {os.path.join(root_dir, file_name)}")
with open(os.path.join(root_dir, file_name), "wb") as wfile:
log_dir = self.configuration.screenshot_path
logger.log(f"Recording stopped in {os.path.join(log_dir, file_name)}")
with open(os.path.join(log_dir, file_name), "wb") as wfile:
wfile.write(decoded_string)

def stop_recording_and_compare(
Expand All @@ -464,18 +459,16 @@ def stop_recording_and_compare(
current_time = now.strftime("%Y-%m-%d%H%M%S")
video_name = f"{self.device_udid}{current_time}.mp4"
self.stop_recording_screen(video_name)
root_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
log_dir = "./logs"
if self.__configuration.screenshot_path != "":
root_dir = self.__configuration.screenshot_path
log_dir = self.__configuration.screenshot_path
found = ImageRecognition(
video_name, comparison, threshold,
device_name=self.device_name,
path=root_dir
path=log_dir
).compare_video(keep_image_as, frame_rate_reduction=fps_reduction)

os.remove(os.path.join(root_dir, video_name))
os.remove(os.path.join(log_dir, video_name))
if not found and not not_found:
if assertion:
raise Exception(
Expand Down
6 changes: 1 addition & 5 deletions testui/support/testui_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,12 @@ class ImageRecognition:
"""

def __init__(
self, original: str, comparison="", threshold=0.9, device_name="Device", path=""
self, original: str, comparison="", threshold=0.9, device_name="Device", path="./logs"
):
self.__original = original
self.__comparison = comparison
self.__threshold = threshold
self.__device_name = device_name
if path == "":
path = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
self.__path = path

def compare(self, image_match="", max_scale=2.0, min_scale=0.3):
Expand Down