-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtns_test.py
165 lines (146 loc) · 5.87 KB
/
tns_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# pylint: disable=broad-except
import inspect
import os
import unittest
from core.base_test.test_context import TestContext
from core.enums.os_type import OSType
from core.log.log import Log
from core.settings import Settings
from core.utils.appium.appium_driver import AppiumDriver
from core.utils.device.adb import Adb
from core.utils.device.device_manager import DeviceManager
from core.utils.file_utils import Folder, File
from core.utils.gradle import Gradle
from core.utils.process import Process
from core.utils.xcode import Xcode
from products.nativescript.tns import Tns
# noinspection PyBroadException,PyUnresolvedReferences
class TnsTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Get class name and log
TestContext.STARTED_PROCESSES = []
TestContext.STARTED_DEVICES = []
TestContext.TEST_APP_NAME = None
TestContext.CLASS_NAME = cls.__name__
try:
for item in inspect.stack():
TestContext.CLASS_NAME = item[0].f_locals['cls'].__name__
except Exception:
pass
Log.test_class_start(class_name=TestContext.CLASS_NAME)
# Kill processes
Adb.restart()
Tns.kill()
Gradle.kill()
TnsTest.kill_emulators()
TnsTest.__clean_backup_folder_and_dictionary()
# Ensure log folders are create
Folder.create(Settings.TEST_OUT_HOME)
Folder.create(Settings.TEST_OUT_LOGS)
Folder.create(Settings.TEST_OUT_IMAGES)
Folder.create(Settings.TEST_OUT_TEMP)
# Set default simulator based on Xcode version
if Settings.HOST_OS == OSType.OSX:
if Xcode.get_version() < 10:
Settings.Simulators.DEFAULT = Settings.Simulators.SIM_IOS11
else:
if Xcode.get_version() < 11:
Settings.Simulators.DEFAULT = Settings.Simulators.SIM_IOS12
else:
Settings.Simulators.DEFAULT = Settings.Simulators.SIM_IOS13
def setUp(self):
TestContext.TEST_NAME = self._testMethodName
Log.test_start(test_name=TestContext.TEST_NAME)
Tns.kill()
Gradle.kill()
AppiumDriver.kill()
TnsTest.__clean_backup_folder_and_dictionary()
def tearDown(self):
# pylint: disable=no-member
# Kill processes
Tns.kill()
AppiumDriver.kill()
Gradle.kill()
Process.kill_all_in_context()
TnsTest.restore_files()
# Get outcome
if hasattr(self, '_outcome'): # Python 3.4+
result = self.defaultTestResult() # these 2 methods have no side effects
self._feedErrorsToResult(result, self._outcome.errors)
else: # Python 3.2 - 3.3 or 3.0 - 3.1 and 2.7
result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups)
# Take screen on test fail
if result.errors or result.failures:
self.get_screenshots()
self.archive_apps()
outcome = 'FAILED'
else:
outcome = 'PASSED'
Log.test_end(test_name=TestContext.TEST_NAME, outcome=outcome)
@classmethod
def tearDownClass(cls):
"""
Logic executed after all core_tests in class.
"""
Tns.kill()
TnsTest.kill_emulators()
Process.kill_all_in_context()
Folder.clean(Settings.TEST_OUT_TEMP)
Log.test_class_end(TestContext.CLASS_NAME)
@staticmethod
def kill_emulators():
DeviceManager.Emulator.stop()
if Settings.HOST_OS is OSType.OSX:
DeviceManager.Simulator.stop()
TestContext.STARTED_DEVICES = []
@staticmethod
def get_screenshots():
# get host snapshot
base_path = os.path.join(Settings.TEST_OUT_IMAGES, TestContext.CLASS_NAME, TestContext.TEST_NAME)
try:
import pyautogui
png_path = os.path.join(base_path, 'host.png')
File.delete(png_path)
Folder.create(folder=os.path.dirname(png_path))
pyautogui.screenshot().save(png_path)
Log.info("Saved host os screen at {0}".format(png_path))
except Exception:
Log.warning('Failed to take screenshot of host os.')
# get device screenshots
for device in TestContext.STARTED_DEVICES:
try:
png_path = os.path.join(base_path, device.name + '.png')
File.delete(png_path)
device.get_screen(png_path)
except AssertionError:
Log.warning('Failed to take screenshot of {0}'.format(device.id))
@staticmethod
def archive_apps():
if TestContext.TEST_APP_NAME is not None:
app_path = os.path.join(Settings.TEST_RUN_HOME, TestContext.TEST_APP_NAME)
if Folder.exists(app_path):
archive_path = os.path.join(Settings.TEST_OUT_HOME, TestContext.CLASS_NAME, TestContext.TEST_NAME,
TestContext.TEST_APP_NAME)
Log.info('Archive app under test at: {0}'.format(archive_path))
@staticmethod
def __clean_backup_folder_and_dictionary():
TestContext.BACKUP_FILES.clear()
Folder.clean(Settings.BACKUP_FOLDER)
@staticmethod
def restore_files():
if TestContext.BACKUP_FILES:
for file_path in TestContext.BACKUP_FILES:
file_name = TestContext.BACKUP_FILES[file_path]
file_temp_path = os.path.join(Settings.BACKUP_FOLDER, file_name)
# delete file not from the original template
if not File.exists(file_temp_path):
File.delete(file_path)
else:
File.copy(file_temp_path, file_path)
File.delete(file_temp_path)
TnsTest.__clean_backup_folder_and_dictionary()
else:
Log.info('No files to restore!')
if __name__ == '__main__':
unittest.main()