-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpreview_helpers.py
160 lines (144 loc) · 7.06 KB
/
preview_helpers.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
import os
import re
import time
import requests
from products.nativescript.tns import Tns
from products.nativescript.tns_logs import TnsLogs
from core.enums.device_type import DeviceType
from core.enums.platform_type import Platform
from core.enums.os_type import OSType
from core.log.log import Log
from core.settings import Settings
from core.settings.Settings import TEST_SUT_HOME, TEST_RUN_HOME
from core.utils.device.adb import Adb
from core.utils.device.simctl import Simctl
from core.utils.file_utils import File
from core.utils.run import run
if Settings.HOST_OS is OSType.OSX:
from core.utils.device.simauto import SimAuto
class Preview(object):
@staticmethod
def get_app_packages():
"""Copy Preview App packages from Shares to local folder"""
File.copy(source=Settings.Packages.PREVIEW_APP_IOS, target=TEST_SUT_HOME)
File.copy(source=Settings.Packages.PREVIEW_APP_ANDROID, target=TEST_SUT_HOME)
File.copy(source=Settings.Packages.PLAYGROUND_APP_IOS, target=TEST_SUT_HOME)
File.copy(source=Settings.Packages.PLAYGROUND_APP_ANDROID, target=TEST_SUT_HOME)
@staticmethod
def unpack_ios_simulator_packages():
"""Unpack the .tgz file to get the nsplaydev.app"""
File.unpack_tar(os.path.join(TEST_SUT_HOME, 'nsplaydev.tgz'), TEST_SUT_HOME)
File.unpack_tar(os.path.join(TEST_SUT_HOME, 'nsplay.tgz'), TEST_SUT_HOME)
@staticmethod
def install_preview_app(device_info, platform, timeout=60):
"""Installs Preview App on emulator and simulator"""
package_android = os.path.join(TEST_SUT_HOME, 'app-universal-release.apk')
package_ios = os.path.join(TEST_SUT_HOME, 'nsplaydev.app')
if platform is Platform.IOS:
# Unpack the .tgz file to get the nsplaydev.app
File.unpack_tar(os.path.join(TEST_SUT_HOME, 'nsplaydev.tgz'), TEST_SUT_HOME)
Simctl.install(device_info, package_ios)
elif platform is Platform.ANDROID:
Adb.install(package_android, device_info.id, timeout)
@staticmethod
def install_preview_app_no_unpack(device_info, platform, uninstall=True):
"""Installs Preview App on emulator and simulator"""
package_android = os.path.join(TEST_SUT_HOME, 'app-universal-release.apk')
package_ios = os.path.join(TEST_SUT_HOME, 'nsplaydev.app')
if platform is Platform.IOS:
if uninstall:
Simctl.uninstall(device_info, Settings.Packages.PREVIEW_APP_ID)
Simctl.install(device_info, package_ios)
elif platform is Platform.ANDROID:
if uninstall:
Adb.uninstall(Settings.Packages.PREVIEW_APP_ID, device_info.id, False)
Adb.install(package_android, device_info.id)
@staticmethod
def install_playground_app(device_info, platform):
"""Installs Playground App on emulator and simulator"""
package_android = os.path.join(TEST_SUT_HOME, "app-release.apk")
package_ios = os.path.join(TEST_SUT_HOME, 'nsplay.app')
if platform is Platform.IOS:
# Unpack the .tgz file to get the nsplay.app
File.unpack_tar(os.path.join(TEST_SUT_HOME, 'nsplay.tgz'), TEST_SUT_HOME)
Simctl.install(device_info, package_ios)
elif platform is Platform.ANDROID:
Adb.install(package_android, device_info.id)
# noinspection PyUnresolvedReferences
@staticmethod
def get_url(output):
"""
Get preview URL form tns log.
This is the url you need to load in Preview app in order to see and sync your project.
:param output: Output of `tns preview` command.
:return: Playground url.
"""
# pylint: disable=no-member
# pylint: disable=no-name-in-module
# pylint: disable=import-error
url = re.findall(r"(https[^\s']+)(?=.)", output)
url = requests.get(url[0], allow_redirects=False)
url = url.headers['Location']
if Settings.PYTHON_VERSION < 3:
import urllib
url = urllib.unquote(url)
else:
from urllib.parse import unquote
url = unquote(url, 'UTF-8')
return url
@staticmethod
def run_url(url, device):
"""
Runs project in the Preview App.
:param url: Playground url.
:param device: DeviceInfo object.
"""
# Url needs to be escaped before open with adb or simctl
url = url.replace(r'?', r'\?')
url = url.replace(r'&', r'\&')
# Run url
Log.info('Open "{0}" on {1}.'.format(url, device.name))
if device.type == DeviceType.EMU or device.type == DeviceType.ANDROID:
cmd = 'shell am start -a android.intent.action.VIEW -d "{0}" org.nativescript.preview'.format(url)
output = Adb.run_adb_command(command=cmd, device_id=device.id).output
assert 'error' not in output, 'Failed to open URL!' + os.linesep + 'Error:' + os.linesep + output
elif device.type == DeviceType.SIM:
output = Simctl.run_simctl_command(command='openurl {0} {1}.'.format(device.id, url)).output
assert 'error' not in output, 'Failed to open URL!' + os.linesep + 'Error:' + os.linesep + output
else:
raise NotImplementedError('Open url not implemented for real iOS devices.')
@staticmethod
def dismiss_simulator_alert():
"""When preview url is loaded in simulator there is alert for confirmation.
This method will dismiss it. It is implemented only for one instance of simulator for the moment"""
dismiss_sim_alert = os.path.join(TEST_RUN_HOME, 'assets', 'scripts', 'send_enter_to_simulator.scpt')
command = "osascript " + dismiss_sim_alert
run(command)
@staticmethod
def run_app(app_name, device, bundle=True, hmr=True, instrumented=False, click_open_alert=False):
result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr)
# Read the log and extract the url to load the app on emulator
log = File.read(result.log_file)
url = Preview.get_url(log)
Preview.run_url(url=url, device=device)
# When you run preview on ios simulator on first run confirmation dialog is shown.
if device.type == DeviceType.SIM:
if click_open_alert is True:
if SimAuto.find(device_info=device, text="Open"):
time.sleep(5)
device.click("Open")
# Verify logs
strings = TnsLogs.preview_initial_messages(device=device, hmr=hmr, bundle=bundle,
instrumented=instrumented)
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=200)
return result
@staticmethod
def is_running_on_ios(device_info, app_id):
"""
Get preview URL form tns log.
This is the url you need to load in Preview app in order to see and sync your project.
:param device_info: Information about the device we will search in.
:param app_id: the App ID of the process.
:return: boolean.
"""
return Simctl.is_process_running(device_info, app_id)