Skip to content

Commit 4a6b3f7

Browse files
authored
feat: add vue tests (#64)
New: - Livesync tests for vue blank template Refactor: - Remove `app_type=AppType.NG` param from `sync_hello_world_ng` (it obviously can't be different than NG) - Remove some `if not app_type == AppType.NG:` checks from TnsLogs (I don't see why we need them) - Do not check of file name in logs in `__file_changed_messages()` when change is in `*.vue` file - Pack Template.VUE_BLANK before tests
1 parent ac950e5 commit 4a6b3f7

12 files changed

+331
-79
lines changed

data/changes.py

+11
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,14 @@ class SharedHelloWorld(object):
112112
HTML = ChangeSet(file_path=os.path.join('src', 'app', 'item', 'items.component.html'),
113113
old_value='"item.name"', new_value='"item.id"',
114114
old_text=None, new_text=None)
115+
116+
class BlankVue(object):
117+
VUE_SCRIPT = ChangeSet(file_path=os.path.join('app', 'components', 'Home.vue'),
118+
old_value='Blank {N}-Vue app', new_value='TEST APP',
119+
old_text='Blank {N}-Vue app', new_text='TEST APP')
120+
VUE_TEMPLATE = ChangeSet(file_path=os.path.join('app', 'components', 'Home.vue'),
121+
old_value='Home', new_value='TITLE',
122+
old_text='Home', new_text='TITLE')
123+
VUE_STYLE = ChangeSet(file_path=os.path.join('app', 'components', 'Home.vue'),
124+
old_value='font-size: 20;', new_value='font-size: 20; color: red;',
125+
old_color=Colors.DARK, new_color=Colors.RED)

data/sync/blank_vue.py

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
Sync changes on JS/TS project helper.
3+
"""
4+
import os
5+
6+
from core.enums.app_type import AppType
7+
from core.log.log import Log
8+
from core.settings import Settings
9+
from core.utils.wait import Wait
10+
from data.changes import Changes, Sync
11+
from data.const import Colors
12+
from products.nativescript.preview_helpers import Preview
13+
from products.nativescript.run_type import RunType
14+
from products.nativescript.tns import Tns
15+
from products.nativescript.tns_logs import TnsLogs
16+
17+
18+
def __run_vue(app_name, platform, bundle, hmr):
19+
# Execute `tns run` and wait until logs are OK
20+
return Tns.run(app_name=app_name, platform=platform, emulator=True, wait=False, bundle=bundle, hmr=hmr)
21+
22+
23+
def __preview_vue(app_name, platform, device, bundle, hmr):
24+
# Execute `tns run` and wait until logs are OK
25+
return Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, platform=platform, device=device)
26+
27+
28+
def __workflow(preview, app_name, platform, device, bundle=False, hmr=False):
29+
# Execute tns command
30+
if preview:
31+
result = __preview_vue(app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr)
32+
else:
33+
result = __run_vue(app_name=app_name, platform=platform, bundle=bundle, hmr=hmr)
34+
35+
if preview:
36+
Log.info('Skip logs checks.')
37+
else:
38+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.FULL, bundle=bundle,
39+
hmr=hmr, app_type=AppType.VUE)
40+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=240)
41+
42+
# Verify it looks properly
43+
device.wait_for_text(text=Changes.BlankVue.VUE_SCRIPT.old_text)
44+
device.wait_for_text(text=Changes.BlankVue.VUE_TEMPLATE.old_text)
45+
initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name, 'initial_state.png')
46+
device.get_screen(path=initial_state)
47+
48+
# Edit script in .vue file
49+
Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_SCRIPT)
50+
if preview:
51+
Log.info('Skip logs checks.')
52+
else:
53+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
54+
bundle=bundle, hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
55+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
56+
device.wait_for_text(text=Changes.BlankVue.VUE_SCRIPT.new_text)
57+
58+
# Edit template in .vue file
59+
Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_TEMPLATE)
60+
if preview:
61+
Log.info('Skip logs checks.')
62+
else:
63+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
64+
bundle=bundle, hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
65+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
66+
device.wait_for_text(text=Changes.BlankVue.VUE_TEMPLATE.new_text)
67+
68+
# Edit styling in .vue file
69+
Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_STYLE)
70+
if preview:
71+
Log.info('Skip logs checks.')
72+
else:
73+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
74+
bundle=bundle, hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
75+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
76+
style_applied = Wait.until(lambda: device.get_pixels_by_color(Colors.RED) > 100)
77+
assert style_applied, 'Failed to sync changes in style.'
78+
79+
# Revert script in .vue file
80+
Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_SCRIPT)
81+
if preview:
82+
Log.info('Skip logs checks.')
83+
else:
84+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
85+
bundle=bundle, hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
86+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
87+
device.wait_for_text(text=Changes.BlankVue.VUE_SCRIPT.old_text)
88+
89+
# Revert template in .vue file
90+
Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_TEMPLATE)
91+
if preview:
92+
Log.info('Skip logs checks.')
93+
else:
94+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
95+
bundle=bundle, hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
96+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
97+
device.wait_for_text(text=Changes.BlankVue.VUE_TEMPLATE.old_text)
98+
99+
# Revert styling in .vue file
100+
Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_STYLE)
101+
if preview:
102+
Log.info('Skip logs checks.')
103+
else:
104+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL,
105+
bundle=bundle,
106+
hmr=hmr, app_type=AppType.VUE, file_name='Home.vue')
107+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
108+
109+
if hmr:
110+
Log.info('Skip next steps because of https://github.com/nativescript-vue/nativescript-vue/issues/425')
111+
else:
112+
style_applied = Wait.until(lambda: device.get_pixels_by_color(Colors.RED) == 0)
113+
assert style_applied, 'Failed to sync changes in style.'
114+
115+
# Assert final and initial states are same
116+
device.screen_match(expected_image=initial_state, tolerance=1.0, timeout=30)
117+
118+
119+
def sync_blank_vue(app_name, platform, device, bundle=False, hmr=False):
120+
__workflow(preview=False, app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr)
121+
122+
123+
def preview_blank_vue(app_name, platform, device, bundle=False, hmr=False):
124+
__workflow(preview=True, app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr)

data/sync/hello_world_js.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from core.utils.wait import Wait
1111
from data.changes import Changes, Sync
1212
from data.const import Colors
13+
from products.nativescript.preview_helpers import Preview
1314
from products.nativescript.run_type import RunType
1415
from products.nativescript.tns import Tns
1516
from products.nativescript.tns_logs import TnsLogs
16-
from products.nativescript.preview_helpers import Preview
1717

1818

1919
def sync_hello_world_js(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
@@ -129,9 +129,8 @@ def __sync_hello_world_js_ts(app_type, app_name, platform, device,
129129
device.screen_match(expected_image=initial_state, tolerance=1.0, timeout=30)
130130

131131

132-
def preview_hello_world_js_ts(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
133-
instrumented=False):
134-
result = Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify, platform=platform,
132+
def preview_hello_world_js_ts(app_name, platform, device, bundle=False, hmr=False, instrumented=False):
133+
result = Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, platform=platform,
135134
device=device, instrumented=instrumented)
136135

137136
# Verify app looks properly
@@ -143,10 +142,9 @@ def preview_hello_world_js_ts(app_name, platform, device, bundle=False, hmr=Fals
143142
return result
144143

145144

146-
def preview_sync_hello_world_js_ts(app_type, app_name, platform, device, bundle=False, hmr=False, uglify=False,
147-
aot=False, instrumented=False):
145+
def preview_sync_hello_world_js_ts(app_type, app_name, platform, device, bundle=False, hmr=False, instrumented=False):
148146
result = preview_hello_world_js_ts(app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr,
149-
uglify=uglify, aot=aot, instrumented=instrumented)
147+
instrumented=instrumented)
150148

151149
blue_count = device.get_pixels_by_color(color=Colors.LIGHT_BLUE)
152150
# Set changes

data/sync/hello_world_ng.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
"""
44

55
import os
6+
67
from core.enums.app_type import AppType
78
from core.enums.platform_type import Platform
89
from core.settings import Settings
910
from data.changes import Changes, Sync
1011
from data.const import Colors
12+
from products.nativescript.preview_helpers import Preview
1113
from products.nativescript.run_type import RunType
1214
from products.nativescript.tns import Tns
1315
from products.nativescript.tns_logs import TnsLogs
14-
from products.nativescript.preview_helpers import Preview
1516

1617

1718
def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False, aot=False, hmr=False,
18-
instrumented=True, app_type=AppType.NG):
19+
instrumented=True):
1920
result = Tns.run(app_name=app_name, platform=platform, emulator=True, wait=False,
2021
bundle=bundle, aot=aot, uglify=uglify, hmr=hmr)
2122
# Check logs
22-
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, bundle=bundle,
23-
hmr=hmr, instrumented=instrumented, app_type=app_type)
23+
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.UNKNOWN, bundle=bundle,
24+
hmr=hmr, instrumented=instrumented, app_type=AppType.NG)
2425
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=300)
2526

2627
# Verify it looks properly
@@ -33,7 +34,7 @@ def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False,
3334
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.TS)
3435
device.wait_for_text(text=Changes.NGHelloWorld.TS.new_text)
3536
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
36-
file_name='item.service.ts', hmr=hmr, instrumented=instrumented, app_type=app_type)
37+
file_name='item.service.ts', hmr=hmr, instrumented=instrumented, app_type=AppType.NG)
3738
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
3839

3940
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.HTML)
@@ -46,7 +47,7 @@ def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False,
4647
assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
4748
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
4849
file_name='items.component.html', hmr=hmr, instrumented=instrumented,
49-
app_type=app_type, aot=aot)
50+
app_type=AppType.NG, aot=aot)
5051
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
5152

5253
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.CSS)
@@ -59,38 +60,37 @@ def sync_hello_world_ng(app_name, platform, device, bundle=False, uglify=False,
5960
device.wait_for_text(text=number)
6061
assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
6162
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
62-
file_name='app.css', hmr=hmr, instrumented=instrumented, app_type=app_type)
63+
file_name='app.css', hmr=hmr, instrumented=instrumented, app_type=AppType.NG)
6364
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
6465

6566
# Revert changes
6667
Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.HTML)
6768
device.wait_for_text(text=Changes.NGHelloWorld.TS.new_text)
6869
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
6970
file_name='items.component.html', hmr=hmr, instrumented=instrumented,
70-
app_type=app_type, aot=aot)
71+
app_type=AppType.NG, aot=aot)
7172
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
7273

7374
Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.TS)
7475
device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text)
7576
device.wait_for_main_color(color=Colors.DARK)
7677
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
77-
file_name='item.service.ts', hmr=hmr, instrumented=instrumented, app_type=app_type)
78+
file_name='item.service.ts', hmr=hmr, instrumented=instrumented, app_type=AppType.NG)
7879
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
7980

8081
Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.CSS)
8182
device.wait_for_main_color(color=Colors.WHITE)
8283
device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text)
8384
strings = TnsLogs.run_messages(app_name=app_name, platform=platform, run_type=RunType.INCREMENTAL, bundle=bundle,
84-
file_name='app.css', hmr=hmr, instrumented=instrumented, app_type=app_type)
85+
file_name='app.css', hmr=hmr, instrumented=instrumented, app_type=AppType.NG)
8586
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings, timeout=180)
8687

8788
# Assert final and initial states are same
8889
device.screen_match(expected_image=initial_state, tolerance=1.0, timeout=30)
8990

9091

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,
92+
def preview_hello_world_ng(app_name, platform, device, bundle=False, hmr=False, instrumented=False):
93+
result = Preview.run_app(app_name=app_name, bundle=bundle, hmr=hmr, platform=platform,
9494
device=device, instrumented=instrumented)
9595

9696
# Verify app looks properly
@@ -101,10 +101,9 @@ def preview_hello_world_ng(app_name, platform, device, bundle=False, hmr=False,
101101
return result
102102

103103

104-
def preview_sync_hello_world_ng(app_name, platform, device, bundle=False, hmr=False, uglify=False,
105-
aot=False, instrumented=False):
104+
def preview_sync_hello_world_ng(app_name, platform, device, bundle=False, hmr=False, instrumented=False):
106105
result = preview_hello_world_ng(app_name=app_name, platform=platform, device=device, bundle=bundle, hmr=hmr,
107-
uglify=uglify, aot=aot, instrumented=instrumented)
106+
instrumented=instrumented)
108107

109108
# Edit TS file and verify changes are applied
110109
Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.TS)

products/nativescript/preview_helpers.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,15 @@ def dismiss_simulator_alert():
108108
run(command)
109109

110110
@staticmethod
111-
def run_app(app_name, platform, device, bundle=False, hmr=False, uglify=False, aot=False,
112-
instrumented=False):
113-
result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify)
111+
def run_app(app_name, platform, device, bundle=False, hmr=False, instrumented=False):
112+
result = Tns.preview(app_name=app_name, bundle=bundle, hmr=hmr)
114113

115114
# Read the log and extract the url to load the app on emulator
116115
log = File.read(result.log_file)
117116
url = Preview.get_url(log)
118117
Preview.run_url(url=url, device=device)
119-
# When you run preview on ios simulator on first run confirmation dialog is showh. This script will dismiss it
120-
if platform == Platform.IOS:
118+
# When you run preview on ios simulator on first run confirmation dialog is shown.
119+
if device.type == DeviceType.SIM:
121120
time.sleep(2)
122121
Preview.dismiss_simulator_alert()
123122

products/nativescript/run_type.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class RunType(IntEnum):
1111
INCREMENTAL = 2, 'incremental' # Some js/xml/css file is changed and incremental prepare is triggered.
1212
FULL = 3, 'full' # Full prepare. Rebuild native frameworks.
1313
FIRST_TIME = 4, 'full (no platforms)' # When platforms are not added to project prepare should add them.
14+
UNKNOWN = 9, 'unknown' # When you are not sure what is the run type.
1415

1516
def __str__(self):
1617
return self.string

products/nativescript/tns.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,26 @@ def debug(app_name, platform, emulator=False, device=None, release=False, provis
299299
pass
300300
return result
301301

302+
@staticmethod
303+
def preview(app_name, bundle=False, hmr=False, log_trace=True, verify=True, timeout=120):
304+
"""
305+
Execute `tns preview` command.
306+
:param app_name: Pass --path <app_name>.
307+
:param bundle: If true pass --bundle.
308+
:param hmr: If true pass --hmr.
309+
:param log_trace: If true pass --log trace.
310+
:param verify: If true verify some logs.
311+
:param timeout: Timeout in seconds.
312+
:return: Result of `tns preview` command.
313+
"""
314+
result = Tns.exec_command(command='preview', path=app_name, bundle=bundle, hmr=hmr, wait=False,
315+
log_trace=log_trace, timeout=timeout)
316+
if verify:
317+
strings = [
318+
'Use NativeScript Playground app and scan the QR code above to preview the application on your device']
319+
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
320+
return result
321+
302322
@staticmethod
303323
def test_init(app_name, framework, verify=True):
304324
"""
@@ -381,15 +401,3 @@ def kill():
381401
else:
382402
Process.kill(proc_name='node', proc_cmdline=Settings.Executables.TNS)
383403
Process.kill_by_commandline(cmdline='webpack.js')
384-
385-
@staticmethod
386-
def preview(app_name, bundle=False, hmr=False, aot=False, uglify=False, wait=False,
387-
log_trace=True, verify=True, timeout=60):
388-
result = Tns.exec_command(command='preview', path=app_name, bundle=bundle, hmr=hmr, aot=aot, uglify=uglify,
389-
wait=wait, log_trace=log_trace, timeout=timeout)
390-
if verify:
391-
strings = [
392-
'Use NativeScript Playground app and scan the QR code above to preview the application on your device']
393-
TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
394-
395-
return result

0 commit comments

Comments
 (0)