Skip to content

Commit 2cb2535

Browse files
authored
feat: test for preview with NG (#62)
* Add test for preview NG. Extract common logic in run_app method * fix adb install method * Fix linter errors
1 parent 3598ede commit 2cb2535

File tree

7 files changed

+166
-25
lines changed

7 files changed

+166
-25
lines changed

core/utils/device/adb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ def install(apk_path, device_id):
177177
:param apk_path: File path to .apk.
178178
:param device_id: Device id.
179179
"""
180-
result = Adb.__run_adb_command(command='install -r {0} {1}'.format(apk_path, device_id), timeout=60)
180+
result = Adb.__run_adb_command(command='-s {0} install -r {1}'.format(device_id, apk_path), timeout=60)
181181
assert 'Success' in result.output, 'Failed to install {0}. Output: {1}'.format(apk_path, result.output)
182182
Log.info('{0} installed successfully on {1}.'.format(apk_path, device_id))

data/sync/hello_world_js.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
"""
44

55
import os
6-
import time
76

87
from core.enums.app_type import AppType
9-
from core.enums.platform_type import Platform
108
from core.settings import Settings
119
from core.utils.file_utils import File
1210
from core.utils.wait import Wait
@@ -133,20 +131,8 @@ def __sync_hello_world_js_ts(app_type, app_name, platform, device,
133131

134132
def preview_hello_world_js_ts(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
135133
instrumented=False):
136-
result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify)
137-
138-
# Read the log and extract the url to load the app on emulator
139-
log = File.read(result.log_file)
140-
url = Preview.get_url(log)
141-
Preview.run_app(url, device.id, platform)
142-
# When you run preview on ios simulator on first run confirmation dialog is showh. This script will dismiss it
143-
if platform == Platform.IOS:
144-
time.sleep(2)
145-
Preview.dismiss_simulator_alert()
146-
147-
# Verify logs
148-
strings = TnsLogs.preview_initial_messages(platform=platform, hmr=hmr, bundle=bundle, instrumented=instrumented)
149-
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
134+
result = Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify, platform=platform,
135+
device=device, instrumented=instrumented)
150136

151137
# Verify app looks properly
152138
device.wait_for_text(text=Changes.JSHelloWord.JS.old_text, timeout=60, retry_delay=5)

data/sync/hello_world_ng.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
76
from core.enums.app_type import AppType
87
from core.enums.platform_type import Platform
98
from core.settings import Settings
@@ -12,6 +11,7 @@
1211
from products.nativescript.run_type import RunType
1312
from products.nativescript.tns import Tns
1413
from products.nativescript.tns_logs import TnsLogs
14+
from products.nativescript.preview_helpers import Preview
1515

1616

1717
def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False, aot=False, hmr=False,
@@ -86,3 +86,56 @@ def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False,
8686

8787
# Assert final and initial states are same
8888
device.screen_match(expected_image=initial_state, tolerance=1.0, timeout=30)
89+
90+
91+
def preview_hello_world_ng(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
92+
instrumented=False):
93+
result = Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify, platform=platform,
94+
device=device, instrumented=instrumented)
95+
96+
# Verify app looks properly
97+
device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text)
98+
device.wait_for_main_color(color=Colors.WHITE)
99+
initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name, 'initial_state.png')
100+
device.get_screen(path=initial_state)
101+
return result
102+
103+
104+
def preview_sync_hello_world_ng(app_name, platform, device, bundle=False, hmr=False, uglify=False,
105+
aot=False, instrumented=False):
106+
result = preview_hello_world_ng(app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr,
107+
uglify=uglify, aot=aot, instrumented=instrumented)
108+
109+
# Edit TS file and verify changes are applied
110+
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.TS)
111+
device.wait_for_text(text=Changes.NGHelloWorld.TS.new_text)
112+
strings = TnsLogs.preview_file_changed_messages(platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
113+
file_name='item.service.ts', hmr=hmr, instrumented=instrumented)
114+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
115+
116+
# Edit HTML file and verify changes are applied
117+
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.HTML)
118+
if platform == Platform.IOS:
119+
for number in ["10", "1"]:
120+
device.wait_for_text(text=number)
121+
else:
122+
for number in ["8", "9"]:
123+
device.wait_for_text(text=number)
124+
assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
125+
strings = TnsLogs.preview_file_changed_messages(platform=platform, bundle=bundle, file_name='items.component.html',
126+
hmr=hmr, instrumented=instrumented)
127+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
128+
129+
# Edit CSS file and verify changes are applied
130+
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.CSS)
131+
device.wait_for_main_color(color=Colors.DARK)
132+
if platform == Platform.IOS:
133+
for number in ["10", "1"]:
134+
device.wait_for_text(text=number)
135+
else:
136+
for number in ["8", "9"]:
137+
device.wait_for_text(text=number)
138+
assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
139+
strings = TnsLogs.preview_file_changed_messages(platform=platform, bundle=bundle, file_name='app.css',
140+
hmr=hmr, instrumented=instrumented)
141+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)

products/nativescript/preview_helpers.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import os
22
import re
3-
3+
import time
44
from core.enums.platform_type import Platform
55
from core.settings import Settings
66
from core.settings.Settings import TEST_SUT_HOME, TEST_RUN_HOME
77
from core.utils.device.adb import Adb, ADB_PATH
88
from core.utils.device.simctl import Simctl
99
from core.utils.file_utils import File
1010
from core.utils.run import run
11+
from products.nativescript.tns import Tns
12+
from products.nativescript.tns_logs import TnsLogs
1113

1214

1315
class Preview(object):
@@ -72,9 +74,9 @@ def get_url(output):
7274
return url
7375

7476
@staticmethod
75-
def run_app(url, device_id, platform):
77+
def run_url(url, device_id, platform):
7678
"""
77-
Runs project in the Preview App on simulator or emulator.
79+
Runs your project in the Preview App on simulator or emulator
7880
"""
7981
if platform is Platform.IOS:
8082
cmd = "xcrun simctl openurl {0} {1}.".format(device_id, url)
@@ -93,3 +95,22 @@ def dismiss_simulator_alert():
9395
dismiss_sim_alert = os.path.join(TEST_RUN_HOME, 'assets', 'scripts', 'send_enter_to_simulator.scpt')
9496
command = "osascript " + dismiss_sim_alert
9597
run(command)
98+
99+
@staticmethod
100+
def run_app(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
101+
instrumented=False):
102+
result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify)
103+
104+
# Read the log and extract the url to load the app on emulator
105+
log = File.read(result.log_file)
106+
url = Preview.get_url(log)
107+
Preview.run_url(url, device.id, platform)
108+
# When you run preview on ios simulator on first run confirmation dialog is showh. This script will dismiss it
109+
if platform == Platform.IOS:
110+
time.sleep(2)
111+
Preview.dismiss_simulator_alert()
112+
113+
# Verify logs
114+
strings = TnsLogs.preview_initial_messages(platform=platform, hmr=hmr, bundle=bundle, instrumented=instrumented)
115+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
116+
return result

products/nativescript/tns_logs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def preview_file_changed_messages(platform, file_name, run_type=RunType.INCREMEN
237237
logs.extend(TnsLogs.__webpack_messages())
238238
logs.append(file_name)
239239
else:
240-
logs.append('Successfully synced {0} for platform {1}'.format(file_name, str(platform)))
240+
logs.append('Successfully synced')
241+
logs.append('{0} for platform {1}'.format(file_name.replace('.ts', '.js'), str(platform)))
241242
if hmr:
242243
logs.append('hot-update.json')
243244
logs.append('HMR: Checking for updates to the bundle with hmr hash')

tests/cli/preview/templates/hello_word_js_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_200_preview_android_bundle(self):
6565
preview_sync_hello_world_js_ts(app_type=AppType.JS, app_name=self.app_name, platform=Platform.ANDROID,
6666
device=self.emu, instrumented=True, bundle=True)
6767

68-
def test_110_preview_android_hmr(self):
68+
def test_205_preview_android_hmr(self):
6969
"""Preview project on emulator. Make valid changes in JS, CSS and XML"""
7070
# preview_hello_world_js_ts(self.app_name, Platform.ANDROID, self.emu)
7171
preview_sync_hello_world_js_ts(app_type=AppType.JS, app_name=self.app_name, platform=Platform.ANDROID,
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
1+
import os
2+
import unittest
13
from core.base_test.tns_test import TnsTest
4+
from core.base_test.tns_run_test import TnsRunTest
5+
from core.enums.os_type import OSType
6+
from core.enums.platform_type import Platform
7+
from core.settings import Settings
8+
from core.utils.file_utils import File, Folder
9+
from data.sync.hello_world_ng import preview_sync_hello_world_ng
10+
from data.templates import Template
11+
from products.nativescript.tns import Tns
12+
from products.nativescript.preview_helpers import Preview
213

314

4-
class TnsRunPreviewNGTests(TnsTest):
15+
class TnsPreviewNGTests(TnsRunTest):
16+
app_name = Settings.AppName.DEFAULT
17+
source_project_dir = os.path.join(Settings.TEST_RUN_HOME, app_name)
18+
target_project_dir = os.path.join(Settings.TEST_RUN_HOME, 'data', 'temp', app_name)
519

620
@classmethod
721
def setUpClass(cls):
8-
TnsTest.setUpClass()
22+
TnsRunTest.setUpClass()
23+
24+
# Download Preview and Playground packages
25+
Preview.get_app_packages()
26+
27+
# Install Preview and Playground
28+
Preview.install_preview_app(cls.emu, Platform.ANDROID)
29+
if Settings.HOST_OS is OSType.OSX:
30+
Preview.install_preview_app(cls.sim, Platform.IOS)
31+
Preview.install_playground_app(cls.sim, Platform.IOS)
32+
33+
# Create app
34+
Tns.create(app_name=cls.app_name, template=Template.HELLO_WORLD_NG.local_package, update=True)
35+
src = os.path.join(Settings.TEST_RUN_HOME, 'assets', 'logs', 'hello-world-ng', 'main.ts')
36+
target = os.path.join(Settings.TEST_RUN_HOME, cls.app_name, 'src')
37+
File.copy(src=src, target=target)
38+
src = os.path.join(Settings.TEST_RUN_HOME, 'assets', 'logs', 'hello-world-ng', 'items.component.ts')
39+
target = os.path.join(Settings.TEST_RUN_HOME, cls.app_name, 'src', 'app', 'item')
40+
File.copy(src=src, target=target)
41+
42+
# Copy TestApp to data folder.
43+
Folder.copy(source=cls.source_project_dir, target=cls.target_project_dir)
44+
45+
def setUp(self):
46+
TnsTest.setUp(self)
47+
# "src" folder of TestApp will be restored before each test.
48+
# This will ensure failures in one test do not cause common failures.
49+
source_src = os.path.join(self.target_project_dir, 'src')
50+
target_src = os.path.join(self.source_project_dir, 'src')
51+
Folder.clean(target_src)
52+
Folder.copy(source=source_src, target=target_src)
53+
54+
55+
class PreviewNGTests(TnsPreviewNGTests):
56+
57+
def test_100_preview_android(self):
58+
"""Preview project on emulator. Make valid changes in TS, CSS and HTML"""
59+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.ANDROID,
60+
device=self.emu, instrumented=True)
61+
62+
@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'iOS tests can be executed only on macOS.')
63+
def test_100_preview_ios(self):
64+
"""Preview project on simulator. Make valid changes in TS, CSS and HTML"""
65+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.IOS,
66+
device=self.sim, instrumented=True)
67+
68+
def test_200_preview_android_bundle(self):
69+
"""Preview project on emulator with --bundle. Make valid changes in TS, CSS and HTML"""
70+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.ANDROID,
71+
device=self.emu, instrumented=True, bundle=True)
72+
73+
@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'iOS tests can be executed only on macOS.')
74+
def test_200_preview_ios_hmr(self):
75+
"""Preview project on simulator with --bundle. Make valid changes in TS, CSS and HTML"""
76+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.IOS,
77+
device=self.sim, instrumented=True, bundle=True)
78+
79+
def test_205_preview_android_hmr(self):
80+
"""Preview project on emulator with --hmr. Make valid changes in TS, CSS and HTML"""
81+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.ANDROID,
82+
device=self.emu, instrumented=True, hmr=True)
83+
84+
@unittest.skipIf(Settings.HOST_OS != OSType.OSX, 'iOS tests can be executed only on macOS.')
85+
def test_205_preview_ios_hmr(self):
86+
"""Preview project on simulator with --hmr. Make valid changes in TS, CSS and HTML"""
87+
preview_sync_hello_world_ng(app_name=self.app_name, platform=Platform.IOS,
88+
device=self.sim, instrumented=True, hmr=True)

0 commit comments

Comments
 (0)