Skip to content

Commit af252ff

Browse files
committed
Fix iOS Simulator tests
1 parent 2279a18 commit af252ff

File tree

7 files changed

+103
-78
lines changed

7 files changed

+103
-78
lines changed

core/device/simulator.py

+83-67
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,132 @@
1-
"""
1+
'''
22
Helper for working with simulator
3-
"""
3+
'''
44

55
import time
66

77
from core.osutils.command import run
8+
from core.osutils.command_log_level import CommandLogLevel
89
from core.osutils.process import Process
910
from core.settings.settings import SIMULATOR_NAME
1011

1112

1213
class Simulator(object):
1314
@staticmethod
1415
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
2230

2331
@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
2741
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))
3956

4057
@staticmethod
4158
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+
"""
4464
found = False
4565
start_time = time.time()
4666
end_time = start_time + timeout
4767
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:
5170
found = True
5271
break
5372
if time.time() > end_time:
5473
break
74+
time.sleep(5)
5575
return found
5676

5777
@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')
6183
time.sleep(1)
6284

6385
@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.'
6893

6994
@staticmethod
7095
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)
91109

92110
@staticmethod
93111
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))
97115

98116
@staticmethod
99117
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))
106123
return output
107124

108125
@staticmethod
109126
def file_contains(app_name, file_path, text):
110-
"""Assert file on booted simulator contains text"""
111127
output = Simulator.cat_app_file(app_name, file_path)
112128
if text in output:
113-
print("{0} exists in {1}".format(text, file_path))
129+
print('{0} exists in {1}'.format(text, file_path))
114130
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))
116132
assert text in output

core/settings/settings.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
EMULATOR_NAME = "Emulator-Api19-Default"
6464
EMULATOR_PORT = "5554"
6565
EMULATOR_ID = "emulator-{0}".format(EMULATOR_PORT)
66-
SIMULATOR_NAME = "iPhone7100"
66+
SIMULATOR_NAME = "iPhone7N"
67+
SIMULATOR_TYPE = 'iPhone 7'
68+
SIMULATOR_SDK = '10.1'
6769

6870
# Android Build Settings
6971
ANDROID_KEYSTORE_PATH = os.environ.get("ANDROID_KEYSTORE_PATH")

runNose.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def get_repos():
102102
# Copy test packages and cleanup
103103
if CURRENT_OS == OSType.OSX:
104104
get_test_packages(platform=Platforms.BOTH)
105-
Simulator.stop_simulators() # Stop running simulators
105+
Simulator.stop() # Stop running simulators
106106
Xcode.cleanup_cache() # Clean Xcode cache folders
107107
else:
108108
get_test_packages(platform=Platforms.ANDROID)

tests/debug/DebugSimulator_Tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def setUpClass(cls):
2727

2828
def setUp(self):
2929
BaseClass.setUp(self)
30-
Simulator.stop_simulators()
30+
Simulator.stop()
3131
Process.kill("Safari")
3232
Process.kill("Inspector")
3333

3434
def tearDown(self):
3535
BaseClass.tearDown(self)
36-
Simulator.stop_simulators()
36+
Simulator.stop()
3737
Process.kill("Safari")
3838
Process.kill("Inspector")
3939

tests/debug/debug_ios.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DebugiOS(unittest.TestCase):
1717
@classmethod
1818
def setUpClass(cls):
1919
Emulator.stop()
20-
Simulator.stop_simulators()
20+
Simulator.stop()
2121
Folder.cleanup('./TNS_App')
2222
Tns.create_app_platform_add(app_name="TNS_App", platform="ios", framework_path=IOS_RUNTIME_SYMLINK_PATH)
2323

tests/run/simulator/EmulateiOS_Tests.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from core.device.simulator import Simulator
88
from core.osutils.folder import Folder
99
from core.osutils.process import Process
10-
from core.settings.settings import IOS_RUNTIME_PATH, SIMULATOR_NAME
10+
from core.settings.settings import IOS_RUNTIME_PATH, SIMULATOR_NAME, SIMULATOR_TYPE, SIMULATOR_SDK
1111
from core.tns.tns import Tns
1212
from core.settings.strings import *
1313

@@ -17,9 +17,9 @@ class EmulateiOSTests(BaseClass):
1717
def setUpClass(cls):
1818
logfile = os.path.join("out", cls.__name__ + ".txt")
1919
BaseClass.setUpClass(logfile)
20-
Simulator.stop_simulators()
20+
Simulator.stop()
2121
Simulator.delete(SIMULATOR_NAME)
22-
Simulator.create(SIMULATOR_NAME, 'iPhone 6', '9.1')
22+
Simulator.create(SIMULATOR_NAME, SIMULATOR_TYPE, SIMULATOR_SDK)
2323

2424
Tns.create_app(cls.app_name)
2525
Tns.platform_add_ios(attributes={"--path": cls.app_name,
@@ -36,13 +36,21 @@ def tearDownClass(cls):
3636
Folder.cleanup('./' + cls.app_name)
3737

3838
def test_001_emulate_list_devices(self):
39+
"""
40+
`tns emulate ios --availableDevices` should list all available iOS Simulators.
41+
"""
3942
output = Tns.run_tns_command("emulate ios", attributes={"--availableDevices": "",
4043
"--path": self.app_name,
4144
"--justlaunch": ""
4245
})
46+
assert "Available emulators" in output
4347
assert SIMULATOR_NAME in output
4448

4549
def test_002_emulate_ios(self):
50+
"""
51+
`tns emulate ios` should build the project and run it in simulator.
52+
If simulator is not running `tns` should start it.
53+
"""
4654
output = Tns.run_tns_command("emulate ios", attributes={"--path": self.app_name,
4755
"--device": SIMULATOR_NAME,
4856
"--justlaunch": ""
@@ -84,4 +92,3 @@ def test_400_emulate_invalid_device(self):
8492
"--justlaunch": ""
8593
})
8694
assert "Cannot resolve the specified connected device by the provided index or identifier." in output
87-
assert "Cannot find device with name: " + invalid in output

tests/unittests/UnittestsSimulator_Tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setUpClass(cls):
1818
logfile = os.path.join("out", cls.__name__ + ".txt")
1919
BaseClass.setUpClass(logfile)
2020
Emulator.stop()
21-
Simulator.stop_simulators()
21+
Simulator.stop()
2222
Simulator.start(SIMULATOR_NAME, '9.1')
2323

2424
def setUp(self):
@@ -32,7 +32,7 @@ def tearDown(self):
3232

3333
@classmethod
3434
def tearDownClass(cls):
35-
Simulator.stop_simulators()
35+
Simulator.stop()
3636

3737
@timed(360)
3838
def test_010_test_jasmine_ios_simulator(self):

0 commit comments

Comments
 (0)