|
1 |
| -""" |
| 1 | +''' |
2 | 2 | Helper for working with simulator
|
3 |
| -""" |
| 3 | +''' |
4 | 4 |
|
5 | 5 | import time
|
6 | 6 |
|
7 | 7 | from core.osutils.command import run
|
| 8 | +from core.osutils.command_log_level import CommandLogLevel |
8 | 9 | from core.osutils.process import Process
|
9 | 10 | from core.settings.settings import SIMULATOR_NAME
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | class Simulator(object):
|
13 | 14 | @staticmethod
|
14 | 15 | def create(name, device_type, ios_version):
|
15 |
| - """Create simulator""" |
16 |
| - |
17 |
| - print "~~~ Create simulator \"{0}\".".format(name) |
18 |
| - output = run( |
19 |
| - "xcrun simctl create \"{0}\" \"{1}\" \"{2}\"".format(name, device_type, ios_version)) |
20 |
| - print "~~~ Simulator \"{0}\" created successfully.".format(name) |
21 |
| - print "~~~ Simulator \"{0}\" id: ".format(name) + output |
| 16 | + """ |
| 17 | + Create iOS Simulator. |
| 18 | + :param name: Simulator name. |
| 19 | + :param device_type: Device type, example: 'iPhone 7' |
| 20 | + :param ios_version: iOS Version, example: '10.0' |
| 21 | + """ |
| 22 | + ios_version = ios_version.replace('.', '-') |
| 23 | + sdk = "com.apple.CoreSimulator.SimRuntime.iOS-{0}".format(ios_version) |
| 24 | + create_command = 'xcrun simctl create "{0}" "{1}" "{2}"'.format(name, device_type, sdk) |
| 25 | + output = run(command=create_command, log_level=CommandLogLevel.SILENT) |
| 26 | + assert 'Invalid' not in output, 'Failed to create simulator. \n ' + output |
| 27 | + assert 'error' not in output.lower(), 'Failed to create simulator. \n ' + output |
| 28 | + assert '-' in output, 'Failed to create simulator. Output is not GUID. \n' + output |
| 29 | + print 'iOS Simulator created: ' + name |
22 | 30 |
|
23 | 31 | @staticmethod
|
24 |
| - def start(name, sdk=None, timeout=300, wait_for=True): |
25 |
| - """Start iOS Simulator""" |
26 |
| - |
| 32 | + def start(name, sdk=None, timeout=300): |
| 33 | + """ |
| 34 | + Start iOS Simulator |
| 35 | + :param name: Simulator name. |
| 36 | + :param sdk: iOS Version, example '10.0' |
| 37 | + :param timeout: Timeout for starting simulator. |
| 38 | + """ |
| 39 | + |
| 40 | + # Generate simulator name based on version |
27 | 41 | if sdk is not None:
|
28 |
| - name = "{0} ({1})".format(name, sdk) |
29 |
| - print "~~~ Start simulator \"{0}\".".format(name) |
30 |
| - start_command = "instruments -w \"{0}\"".format(name) |
31 |
| - output = run(start_command, timeout) |
32 |
| - assert "Waiting for device to boot..." in output |
33 |
| - |
34 |
| - if wait_for: |
35 |
| - if Simulator.wait_for_simulator(timeout): |
36 |
| - print "~~~ Simulator \"{0}\" started successfully.".format(name) |
37 |
| - else: |
38 |
| - raise NameError("Waiting for simulator \"{0}\" failed!".format(name)) |
| 42 | + name = '{0} ({1})'.format(name, sdk) |
| 43 | + |
| 44 | + # Fire start command |
| 45 | + start_command = 'instruments -w "{0}"'.format(name) |
| 46 | + output = run(command=start_command, timeout=timeout, log_level=CommandLogLevel.SILENT) |
| 47 | + assert 'Unknown device' not in output, "Can not find simulator with name " + name |
| 48 | + assert 'Waiting for device to boot...' in output |
| 49 | + print 'Simulator {0} is booting now...'.format(name) |
| 50 | + |
| 51 | + # Wait until simulator boot |
| 52 | + if Simulator.wait_for_simulator(timeout): |
| 53 | + print 'Simulator {0} is up and running!'.format(name) |
| 54 | + else: |
| 55 | + raise NameError('Failed to boot {0}!'.format(name)) |
39 | 56 |
|
40 | 57 | @staticmethod
|
41 | 58 | def wait_for_simulator(timeout=300):
|
42 |
| - """Wait for simulator""" |
43 |
| - |
| 59 | + """ |
| 60 | + Wait until simulator boot. |
| 61 | + :param timeout: Timeout in seconds. |
| 62 | + :return: True if booted, False if it fails to boot. |
| 63 | + """ |
44 | 64 | found = False
|
45 | 65 | start_time = time.time()
|
46 | 66 | end_time = start_time + timeout
|
47 | 67 | while not found:
|
48 |
| - time.sleep(2) |
49 |
| - output = run("xcrun simctl list devices") |
50 |
| - if "Booted" in output: |
| 68 | + output = run(command='xcrun simctl list devices', log_level=CommandLogLevel.SILENT) |
| 69 | + if 'Booted' in output: |
51 | 70 | found = True
|
52 | 71 | break
|
53 | 72 | if time.time() > end_time:
|
54 | 73 | break
|
| 74 | + time.sleep(5) |
55 | 75 | return found
|
56 | 76 |
|
57 | 77 | @staticmethod
|
58 |
| - def stop_simulators(): |
59 |
| - """Stop running simulators""" |
60 |
| - Process.kill("Simulator") |
| 78 | + def stop(): |
| 79 | + """ |
| 80 | + Stop all running simulators. |
| 81 | + """ |
| 82 | + Process.kill('Simulator') |
61 | 83 | time.sleep(1)
|
62 | 84 |
|
63 | 85 | @staticmethod
|
64 |
| - def reset_simulators(): |
65 |
| - """Reset settings and storage of all simulators""" |
66 |
| - Simulator.stop_simulators() |
67 |
| - run("xcrun simctl erase all", timeout=60) |
| 86 | + def reset(): |
| 87 | + """ |
| 88 | + Reset settings and storage of all simulators. |
| 89 | + """ |
| 90 | + Simulator.stop() |
| 91 | + run(command='xcrun simctl erase all', timeout=60, log_level=CommandLogLevel.SILENT) |
| 92 | + print 'Reset settings and storage of all simulators.' |
68 | 93 |
|
69 | 94 | @staticmethod
|
70 | 95 | def delete(name):
|
71 |
| - """Delete simulator""" |
72 |
| - |
73 |
| - output = run("xcrun simctl list | grep \"{0}\"".format(name)) |
74 |
| - while (SIMULATOR_NAME in output) and ("Invalid" not in output): |
75 |
| - if "Booted" in output: |
76 |
| - run("xcrun simctl shutdown \"{0}\"".format(name)) |
77 |
| - Simulator.stop_simulators() |
78 |
| - run("xcrun simctl delete \"{0}\"".format(name)) |
79 |
| - print "~~~ Simulator \"{0}\" deleted.".format(name) |
80 |
| - output = run("xcrun simctl list | grep \"{0}\"".format(name)) |
81 |
| - |
82 |
| - @staticmethod |
83 |
| - def find_between(string, first, last): |
84 |
| - """Find string between two substrings""" |
85 |
| - try: |
86 |
| - start = string.index(first) + len(first) |
87 |
| - end = string.index(last, start) |
88 |
| - return string[start:end] |
89 |
| - except ValueError: |
90 |
| - return "ValueError!" |
| 96 | + """ |
| 97 | + Delete simulator. |
| 98 | + :param name: Simulator name. |
| 99 | + """ |
| 100 | + output = run(command='xcrun simctl list | grep \'{0}\''.format(name), log_level=CommandLogLevel.SILENT) |
| 101 | + while (SIMULATOR_NAME in output) and ('Invalid' not in output): |
| 102 | + if 'Booted' in output: |
| 103 | + run('xcrun simctl shutdown \'{0}\''.format(name), log_level=CommandLogLevel.SILENT) |
| 104 | + Simulator.stop() |
| 105 | + output = run('xcrun simctl delete \'{0}\''.format(name), log_level=CommandLogLevel.SILENT) |
| 106 | + assert "Unable to delete" not in output, "Failed to delete simulator {0}".format(name) |
| 107 | + print 'Simulator \'{0}\' deleted.'.format(name) |
| 108 | + output = run('xcrun simctl list | grep \'{0}\''.format(name), log_level=CommandLogLevel.SILENT) |
91 | 109 |
|
92 | 110 | @staticmethod
|
93 | 111 | def uninstall_app(app_name):
|
94 |
| - app_name = app_name.replace("_", "") |
95 |
| - app_name = app_name.replace(" ","") |
96 |
| - run("xcrun simctl uninstall booted org.nativescript.{0}".format(app_name)) |
| 112 | + app_name = app_name.replace('_', '') |
| 113 | + app_name = app_name.replace(' ', '') |
| 114 | + run('xcrun simctl uninstall booted org.nativescript.{0}'.format(app_name)) |
97 | 115 |
|
98 | 116 | @staticmethod
|
99 | 117 | def cat_app_file(app_name, file_path):
|
100 |
| - """Return content of file on booted simulator""" |
101 |
| - app_name = app_name.replace("_", "") |
102 |
| - app_name = app_name.replace(" ","") |
103 |
| - app_path = run("xcrun simctl get_app_container booted org.nativescript.{0}".format(app_name)) |
104 |
| - print "Get content of: " + app_path |
105 |
| - output = run("cat {0}/{1}".format(app_path, file_path)) |
| 118 | + app_name = app_name.replace('_', '') |
| 119 | + app_name = app_name.replace(' ', '') |
| 120 | + app_path = run('xcrun simctl get_app_container booted org.nativescript.{0}'.format(app_name)) |
| 121 | + print 'Get content of: ' + app_path |
| 122 | + output = run('cat {0}/{1}'.format(app_path, file_path)) |
106 | 123 | return output
|
107 | 124 |
|
108 | 125 | @staticmethod
|
109 | 126 | def file_contains(app_name, file_path, text):
|
110 |
| - """Assert file on booted simulator contains text""" |
111 | 127 | output = Simulator.cat_app_file(app_name, file_path)
|
112 | 128 | if text in output:
|
113 |
| - print("{0} exists in {1}".format(text, file_path)) |
| 129 | + print('{0} exists in {1}'.format(text, file_path)) |
114 | 130 | else:
|
115 |
| - print("{0} does not exists in {1}".format(text, file_path)) |
| 131 | + print('{0} does not exists in {1}'.format(text, file_path)) |
116 | 132 | assert text in output
|
0 commit comments