-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpreview_helpers.py
126 lines (112 loc) · 5.36 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
import os
import re
import time
from core.enums.device_type import DeviceType
from core.enums.platform_type import Platform
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
from products.nativescript.tns import Tns
from products.nativescript.tns_logs import TnsLogs
class Preview(object):
@staticmethod
def get_app_packages():
"""Copy Preview App packages from Shares to local folder"""
File.copy(src=Settings.Packages.PREVIEW_APP_IOS, target=TEST_SUT_HOME)
File.copy(src=Settings.Packages.PREVIEW_APP_ANDROID, target=TEST_SUT_HOME)
File.copy(src=Settings.Packages.PLAYGROUND_APP_IOS, target=TEST_SUT_HOME)
File.copy(src=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):
"""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)
@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):
# pylint: disable=no-member
# pylint: disable=no-name-in-module
# pylint: disable=import-error
"""
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.
"""
url = re.findall(r"(nsplay[^\s']+)", output)[0]
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)
result = Adb.run_adb_command(command=cmd, device_id=device.id)
assert 'error' not in result.output
elif device.type == DeviceType.SIM:
result = Simctl.run_simctl_command(command='openurl {0} {1}.'.format(device.id, url))
assert 'error' not in result.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, platform, device, bundle=False, hmr=False, instrumented=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:
time.sleep(2)
Preview.dismiss_simulator_alert()
# Verify logs
strings = TnsLogs.preview_initial_messages(platform=platform, hmr=hmr, bundle=bundle, instrumented=instrumented)
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
return result