def created(app_name, output=None, full_check=True): """ Assert application is created properly. :param app_name: App name :param output: Output of `tns create command` :param full_check: If true everything will be checked. If false only console output will be checked. """ # Assert console output is ok if output is not None: app = app_name.rsplit('/')[-1] if USE_YARN != "True": if Npm.version() < 5: assert 'nativescript-theme-core' in output assert 'Project {0} was successfully created'.format(app) in output, 'Failed to create {0}'.format(app) if full_check: # Assert files are ok assert File.exists(app_name) assert File.exists(app_name + '/node_modules/tns-core-modules/package.json') assert File.exists(app_name + '/node_modules/tns-core-modules/LICENSE') assert File.exists(app_name + '/node_modules/tns-core-modules/xml/xml.js') assert File.exists(app_name + '/node_modules/nativescript-theme-core') # Assert content of package.json app_id = app_name.replace(' ', '').replace('_', '').replace('-', '').rsplit('/')[-1] strings = ['org.nativescript.{0}'.format(app_id), 'tns-core-modules', 'nativescript-theme-core'] TnsAsserts.package_json_contains(app_name, string_list=strings)
def test_300_prepare_ios_preserve_case(self): File.copy( self.app_name + "/node_modules/tns-core-modules/application/application-common.js", self.app_name + "/node_modules/tns-core-modules/application/New-application-common.js" ) File.copy( self.app_name + "/node_modules/tns-core-modules/application/application.android.js", self.app_name + "/node_modules/tns-core-modules/application/New-application.android.js" ) File.copy( self.app_name + "/node_modules/tns-core-modules/application/application.ios.js", self.app_name + "/node_modules/tns-core-modules/application/New-application.ios.js" ) output = Tns.prepare_ios(attributes={"--path": self.app_name}) TnsAsserts.prepared(self.app_name, platform=Platform.IOS, output=output, prepare=Prepare.FULL) # Verify case is preserved path = TnsAsserts._get_ios_modules_path(self.app_name) assert File.exists(path + 'application/New-application-common.js') assert File.exists(path + 'application/New-application.js') assert not File.exists(path + 'application/New-application.ios.js')
def platform_clean(platform=Platform.NONE, attributes={}, assert_success=True, log_trace=False, tns_path=None): platform_string = Tns.__get_platform_string(platform) output = Tns.run_tns_command("platform clean " + platform_string, attributes=attributes, log_trace=log_trace, tns_path=tns_path) app_name = Tns.__get_app_name_from_attributes(attributes) if assert_success: assert "Platform {0} successfully removed".format( platform_string) in output assert "error" not in output if platform is Platform.ANDROID: assert File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID)) if platform is Platform.IOS: assert File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_IOS)) assert "Platform {0} successfully added".format( platform_string) in output return output
def test_200_build_android_app_bundle(self): """Build app with android app bundle option. Verify the output(app.aab) and use bundletool to deploy on device""" path_to_aab = os.path.join(self.app_name, "platforms", "android", "app", "build", "outputs", "bundle", "debug", "app.aab") output = Tns.build_android(attributes={ "--path": self.app_name, "--aab": "" }, assert_success=False) assert "The build result is located at:" in output assert path_to_aab in output assert File.exists(path_to_aab) #Verify app can be deployed on emulator via bundletool # Use bundletool to create the .apks file self.bundletool_build(self.bundletool_path, path_to_aab, self.path_to_apks) assert File.exists(self.path_to_apks) # Deploy on device self.bundletool_deploy(self.bundletool_path, self.path_to_apks, device_id=EMULATOR_ID) # Start the app on device Adb.start_app(EMULATOR_ID, "org.nativescript.TestApp") # Verify app looks correct inside emulator app_started = Device.wait_for_text(device_id=EMULATOR_ID, text='TAP') assert app_started, 'App is not started on device'
def platform_added(app_name, platform=Platform.NONE, output=None): """ Assert platform is added. :param app_name: Application name (folder where app is located). :param platform: Platforms that should be available. :param output: output of `tns platform add` command """ # Verify console output is correct if output is not None: if platform is Platform.ANDROID: assert 'Platform android successfully added' in output if platform is Platform.IOS and CURRENT_OS == OSType.OSX: assert 'Platform ios successfully added' in output else: assert 'Applications for platform ios can not be built on this OS' assert 'Project successfully created.' not in output # This is to handle test for app with space. # In this case we put app name inside ''. app_name = app_name.replace('\'', '') app_name = app_name.replace('\"', '') # Verify file and folder content if platform is Platform.NONE: assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS)) assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID)) if platform is Platform.ANDROID or platform is Platform.BOTH: assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID)) if platform is Platform.IOS or platform is Platform.BOTH: if CURRENT_OS == OSType.OSX: assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS)) else: assert not File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_IOS))
def test_205_build_android_app_bundle_env_snapshot(self): """Build app with android app bundle option with --bundle and optimisations for snapshot. Verify the output(app.aab) and use bundletool to deploy on device.""" # This test will not run on windows because env.snapshot option is not available on that OS path_to_aab = os.path.join(self.app_name, "platforms", "android", "app", "build", "outputs", "bundle", "release", "app.aab") #Configure app with snapshot optimisations source = os.path.join('data', 'abdoid-app-bundle', 'app.gradle') target = os.path.join(self.app_name, 'app', 'App_Resources', 'Android', 'app.gradle' ) File.copy(src=source, dest=target) source = os.path.join('data', 'abdoid-app-bundle', 'webpack.config.js') target = os.path.join(self.app_name, 'webpack.config.js' ) File.copy(src=source, dest=target) #env.snapshot is applicable only in release build output = Tns.build_android(attributes={"--path": self.app_name, "--keyStorePath": ANDROID_KEYSTORE_PATH, "--keyStorePassword": ANDROID_KEYSTORE_PASS, "--keyStoreAlias": ANDROID_KEYSTORE_ALIAS, "--keyStoreAliasPassword": ANDROID_KEYSTORE_ALIAS_PASS, "--release": "", "--aab": "", "--env.uglify": "", "--env.snapshot": "", "--bundle": "" }, assert_success=False) assert "The build result is located at:" in output assert path_to_aab in output assert File.exists(path_to_aab) #Verify app can be deployed on emulator via bundletool # Use bundletool to create the .apks file self.bundletool_build(self.bundletool_path, path_to_aab, self.path_to_apks) assert File.exists(self.path_to_apks) # Verify that the correct .so file is included in the package File.unzip(self.path_to_apks, os.path.join(self.app_name, 'apks')) File.unzip(os.path.join(self.app_name, 'apks', 'splits', 'base-x86.apk'), os.path.join(self.app_name,'base_apk')) assert File.exists(os.path.join(self.app_name, 'base_apk', 'lib', 'x86', 'libNativeScript.so')) # Deploy on device self.bundletool_deploy(self.bundletool_path, self.path_to_apks, device_id=EMULATOR_ID) # Start the app on device Adb.start_app(EMULATOR_ID, "org.nativescript.TestApp") # Verify app looks correct inside emulator app_started = Device.wait_for_text(device_id=EMULATOR_ID, text='TAP') assert app_started, 'App is not started on device'
def plugin_create(name, attributes={}, log_trace=False, assert_success=True, tns_path=None, force_clean=True): # Detect folder where plugin should be created path = attributes.get("--path") # noinspection PyUnusedLocal folder = path if path is None: path = name folder = name else: folder = path + os.sep + name # Clean target location if force_clean: Folder.cleanup(folder=path) # Execute plugin create command output = Tns.run_tns_command( "plugin create --includeTypeScriptDemo=y " + name, attributes=attributes, log_trace=log_trace, tns_path=tns_path) if assert_success: # Verify command output assert "Will now rename some files" in output, "Post clone script not executed." assert "Screenshots removed" in output, "Post clone script not executed." assert "Solution for {0}".format( name ) + " was successfully created" in output, 'Missing message in output.' assert "https://docs.nativescript.org/plugins/building-plugins" in output, 'Link to docs is missing.' # Verify created files and folders plugin_root = os.path.join(TEST_RUN_HOME, folder) readme = os.path.join(plugin_root, "README.md") src = os.path.join(plugin_root, "src") demo = os.path.join(plugin_root, "demo") post_clone_script = os.path.join(src, "scripts", "postclone.js") assert File.exists(readme), 'README.md do not exists.' assert not Folder.is_empty( src), 'src folder should exists and should not be empty.' assert not Folder.is_empty( demo), 'demo folder should exists and should not be empty.' assert not File.exists( post_clone_script ), 'Post clone script should not exists in plugin src folder.' return output
def test_301_prepare_android_platform_specific_files(self): Tns.prepare_android(attributes={"--path": self.app_name}) # Add set of platform specific files File.copy(self.app_name + "/app/app.js", self.app_name + "/app/app.ios.js") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/app.android.js") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/appios.js") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/appandroid.js") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/ios.js") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/android.js") File.copy(self.app_name + "/app/app.css", self.app_name + "/app/app.ios.css") File.copy(self.app_name + "/app/app.css", self.app_name + "/app/app.android.css") File.copy(self.app_name + "/app/app.js", self.app_name + "/app/appNew.js") File.copy(self.app_name + "/app/app.css", self.app_name + "/app/appNew.css") File.remove(self.app_name + "/app/app.js") File.remove(self.app_name + "/app/app.css") Tns.prepare_android(attributes={"--path": self.app_name}) # Verify new files are in available in platforms folder app_path = os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH) assert File.exists(os.path.join(app_path, 'app.css')) assert File.exists(os.path.join(app_path, 'app.js')) assert File.exists(os.path.join(app_path, 'appandroid.js')) assert File.exists(os.path.join(app_path, 'appios.js')) assert File.exists(os.path.join(app_path, 'android.js')) assert File.exists(os.path.join(app_path, 'ios.js')) assert not File.exists(os.path.join(app_path, 'app.ios.css')) assert not File.exists(os.path.join(app_path, 'app.android.css'))
def platform_remove(platform=Platform.NONE, attributes={}, assert_success=True, log_trace=False, tns_path=None): platform_string = Tns.__get_platform_string(platform) output = Tns.run_tns_command("platform remove " + platform_string, attributes=attributes, log_trace=log_trace, tns_path=tns_path) app_name = Tns.__get_app_name_from_attributes(attributes) if assert_success: assert "Platform {0} successfully removed".format(platform_string) in output assert "error" not in output if platform is Platform.ANDROID: assert not File.exists(app_name + TnsAsserts.PLATFORM_ANDROID) if platform is Platform.IOS: assert not File.exists(app_name + TnsAsserts.PLATFORM_IOS) return output
def get_screen_text(): """ Get text of current screen of host machine. :return: All the text visible on screen as string """ base_path = os.path.join(OUTPUT_FOLDER, "images", "host") if not File.exists(base_path): Folder.create(base_path) actual_image_path = os.path.join(base_path, "host_{0}.png".format(time.time())) if File.exists(actual_image_path): File.remove(actual_image_path) Screen.save_screen(path=actual_image_path) image = Image.open(actual_image_path) text = pytesseract.image_to_string(image.convert('L')) return text
def get_screen(device_id, file_path): """ Save screen of mobile device. :param device_id: Device identifier (example: `emulator-5554`). :param file_path: Path to image that will be saved. """ File.remove(file_path) base_path, file_name = os.path.split(file_path) Folder.create(base_path) device_type = Device.__get_device_type(device_id) if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID): Adb.get_screen(device_id=device_id, file_path=file_path) if device_type == DeviceType.SIMULATOR: Simulator.get_screen(device_id=device_id, file_path=file_path) if device_type == DeviceType.IOS: IDevice.get_screen(device_id=device_id, file_path=file_path) image_saved = False if File.exists(file_path): size = os.path.getsize(file_path) if size > 10: image_saved = True return image_saved
def test_300_prepare_ios_preserve_case(self): File.copy(self.app_name + "/node_modules/tns-core-modules/application/application-common.js", self.app_name + "/node_modules/tns-core-modules/application/New-application-common.js") File.copy(self.app_name + "/node_modules/tns-core-modules/application/application.android.js", self.app_name + "/node_modules/tns-core-modules/application/New-application.android.js") File.copy(self.app_name + "/node_modules/tns-core-modules/application/application.ios.js", self.app_name + "/node_modules/tns-core-modules/application/New-application.ios.js") output = Tns.prepare_ios(attributes={"--path": self.app_name}) TnsAsserts.prepared(self.app_name, platform=Platform.IOS, output=output, prepare=Prepare.FULL) # Verify case is preserved path = TnsAsserts._get_ios_modules_path(self.app_name) assert File.exists(path + 'application/New-application-common.js') assert File.exists(path + 'application/New-application.js') assert not File.exists(path + 'application/New-application.ios.js')
def tearDown(self): # Logic executed only on test failure test_name = self._testMethodName artifacts_folder = os.path.join(OUTPUT_FOLDER, self.__class__.__name__ + "_" + test_name) outcome = "PASSED" if self.IsFailed(self._resultForDoCleanups) is True: outcome = "FAILED" # Ensure `artifacts_folder` exists and it is clean if File.exists(artifacts_folder): Folder.cleanup(artifacts_folder) else: Folder.create(artifacts_folder) # Collect artifacts on test failure self.__copy_images(artifacts_folder=artifacts_folder) self.__copy_project_folder(artifacts_folder=artifacts_folder) self.__save_host_screen(artifacts_folder=artifacts_folder, test_method_name=test_name) print "" print "Test Method: {0}".format(self._testMethodName) print "End Time: {0}".format(time.strftime("%X")) print "Outcome: {0}".format(outcome) print "_________________________________TEST END_______________________________________" print ""
def create_app(app_name, attributes={}, log_trace=False, assert_success=True, update_modules=True, force_clean=True, measureTime=False): if force_clean: if File.exists(app_name): Folder.cleanup(app_name) path = app_name attributes_to_string = "" for k, v in attributes.iteritems(): if "--path" in k: path = v attributes_to_string = "".join("{0} {1}".format(k, v)) attr = {} if not any(s in attributes_to_string for s in ("--ng", "--template", "--tsc", "--vue")): if BRANCH == "master": attr = {"--template": SUT_FOLDER + os.path.sep + "tns-template-hello-world.tgz"} else: attr = {"--template": "tns-template-hello-world"} attr.update(attributes) if app_name is None: output = Tns.run_tns_command("create ", attributes=attr, log_trace=log_trace, measureTime=measureTime) else: output = Tns.run_tns_command("create \"" + app_name + "\"", attributes=attr, log_trace=log_trace, measureTime=measureTime) if assert_success: TnsAsserts.created(app_name=app_name, output=output) if update_modules: Tns.update_modules(path) # Tns.ensure_app_resources(path) return output
def tearDown(self): # Logic executed only on test failure test_name = self._testMethodName artifacts_folder = os.path.join( OUTPUT_FOLDER, self.__class__.__name__ + "_" + test_name) outcome = "PASSED" if self.IsFailed(self._resultForDoCleanups) is True: outcome = "FAILED" # Ensure `artifacts_folder` exists and it is clean if File.exists(artifacts_folder): Folder.cleanup(artifacts_folder) else: Folder.create(artifacts_folder) # Collect artifacts on test failure self.__copy_images(artifacts_folder=artifacts_folder) self.__copy_project_folder(artifacts_folder=artifacts_folder) self.__save_host_screen(artifacts_folder=artifacts_folder, test_method_name=test_name) print "" print "Test Method: {0}".format(self._testMethodName) print "End Time: {0}".format(time.strftime("%X")) print "Outcome: {0}".format(outcome) print "_________________________________TEST END_______________________________________" print ""
def run_android(attributes={}, assert_success=True, log_trace=False, timeout=COMMAND_TIMEOUT, tns_path=None, wait=True): output = Tns.run_tns_command("run android", attributes=attributes, log_trace=log_trace, timeout=timeout, tns_path=tns_path, wait=wait) if assert_success: assert "Project successfully built" in output assert "Successfully installed on device with identifier" in output app_name = Tns.__get_app_name_from_attributes( attributes=attributes) # apk_name = Tns.__get_final_package_name(app_name, platform=Platform.ANDROID) apk_name = "app" debug_app_path = os.path.join( app_name, TnsAsserts.PLATFORM_ANDROID_APK_DEBUG_PATH, apk_name) release_app_path = os.path.join( app_name, TnsAsserts.PLATFORM_ANDROID_APK_RELEASE_PATH, apk_name) if "--release" in attributes.keys(): apk_path = release_app_path + "-release.apk" else: apk_path = debug_app_path + "-debug.apk" apk_path = apk_path.replace("\"", "") # Handle projects with space assert File.exists( apk_path), "Apk file does not exist at " + apk_path return output
def create_app_ng(app_name, attributes={}, log_trace=False, assert_success=True, update_modules=True, template_version=None): if template_version is not None: template = "tns-template-hello-world-ng@" + template_version attr = {"--template": template} else: if BRANCH is "master": attr = {"--template": SUT_FOLDER + os.path.sep + "tns-template-hello-world-ng.tgz"} else: attr = {"--template": "tns-template-hello-world-ng"} attributes.update(attr) output = Tns.create_app(app_name=app_name, attributes=attributes, log_trace=log_trace, assert_success=assert_success, update_modules=update_modules) if update_modules: Tns.update_angular(path=app_name) Tns.update_typescript(path=app_name) if assert_success: if USE_YARN != "True": if Npm.version() < 5: assert "nativescript-angular" in output assert File.exists(os.path.join(app_name, 'node_modules', 'nativescript-theme-core')) package_json = File.read(os.path.join(app_name, 'package.json')) assert "tns-core-modules" in package_json assert "nativescript-angular" in package_json assert "nativescript-dev-typescript" in package_json return output
def get_screen(device_id, file_path): """ Save screen of iOS Simulator. :param device_id: Device identifier (Simualtor GUID) :param file_path: Name of image that will be saved. """ run(command="xcrun simctl io {0} screenshot {1}".format(device_id, file_path), log_level=CommandLogLevel.SILENT) assert File.exists(file_path), "Failed to get screenshot at " + file_path
def build_android(attributes={}, assert_success=True, tns_path=None, log_trace=False, measureTime=False): output = Tns.run_tns_command("build android", attributes=attributes, tns_path=tns_path, log_trace=log_trace, measureTime=measureTime) if assert_success: # Verify output of build command assert "Project successfully built" in output, "Build failed!" + os.linesep + output assert "FAILURE" not in output assert "NOT FOUND" not in output # Test for https://github.com/NativeScript/android-runtime/issues/390 if log_trace: assert "BUILD SUCCESSFUL" in output, "Build failed!" + os.linesep + output else: assert "BUILD SUCCESSFUL" not in output, "Native build out is displayed even without --log trace" # Verify apk packages app_name = Tns.__get_app_name_from_attributes(attributes=attributes) Tns.__get_platform_string(platform=Platform.ANDROID) android_version_string = str(TnsAsserts.get_platform_version(app_name=app_name, platform='android')) android_major_version = int(android_version_string.split('-')[0].split('.')[0]) if android_major_version > 3: apk_name = "app" debug_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_DEBUG_PATH, apk_name) release_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_RELEASE_PATH, apk_name) else: apk_name = Tns.__get_final_package_name(app_name, platform=Platform.ANDROID) debug_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_PATH, apk_name) release_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_PATH, apk_name) if "--release" in attributes.keys(): apk_path = release_app_path + "-release.apk" else: apk_path = debug_app_path + "-debug.apk" apk_path = apk_path.replace("\"", "") # Handle projects with space assert File.exists(apk_path), "Apk file does not exist at " + apk_path # Verify final package contains right modules (or verify bundle when it is used) if "--bundle" not in attributes.keys(): assert "Webpack compilation complete" not in output else: assert "Webpack compilation complete" in output assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH, "bundle.js")) assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH, "package.json")) assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH, "starter.js")) assert File.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH, "vendor.js")) assert not Folder.exists(os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_NPM_MODULES_PATH)) return output
def test_200_build_android_inside_project_folder(self): Folder.navigate_to(self.app_name) output = Tns.build_android(tns_path=os.path.join("..", TNS_PATH), attributes={"--path": self.app_name}, assert_success=False, log_trace=True) Folder.navigate_to(TEST_RUN_HOME, relative_from_current_folder=False) assert successfully_prepared in output assert build_successful in output assert successfully_built in output assert File.exists(os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APK_DEBUG_PATH, self.debug_apk))
def test_310_prepare_should_flatten_scoped_dependencies(self): Folder.cleanup(self.app_name) Tns.create_app_ng(self.app_name) Tns.platform_add_android(attributes={"--path": self.app_name, "--frameworkPath": ANDROID_PACKAGE}) Tns.prepare_android(attributes={"--path": self.app_name}) # Verify scoped dependencies are flattened (verify #1783 is fixed) ng_path = os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_NPM_MODULES_PATH, '@angular', 'core') assert File.exists(ng_path), "Scoped dependencies are flattened, please see #1783!"
def ensure_app_resources(path): app_resources_path = os.path.join(path, "app", "App_Resources") if File.exists(app_resources_path): pass else: print "AppResources not found. Will copy from default template..." src = os.path.join(TEST_RUN_HOME, "sut", "template-hello-world", "app", "App_Resources") dest = os.path.join(TEST_RUN_HOME, path, "app", "App_Resources") Folder.copy(src, dest)
def tearDown(self): # Verify application state at the end of the test is correct if File.exists(self.app_name): data = TnsAsserts.get_package_json(self.app_name) assert "tns-android" in data["nativescript"], "'tns-android' not found under `nativescript` in package.json" assert "tns-android" not in data["dependencies"], "'tns-android' found under `dependencies` in package.json" BaseClass.tearDown(self) Folder.cleanup(self.platforms_android + '/build/outputs') Folder.cleanup("with space")
def test_200_platform_add_ios_framework_path(self): Tns.platform_add_ios(attributes={ "--path": self.app_name, "--frameworkPath": IOS_PACKAGE }) # If project.xcworkspace is there Xcode project name is wrong assert not File.exists( self.app_name + "/platforms/ios/TestApp.xcodeproj/project.xcworkspace")
def install(): package = File.find(base_path=SUT_FOLDER, file_name="nativescript") output = Npm.install(package=package, folder=TEST_RUN_HOME) message = "NativeScript CLI installation failed - \"{e}\" found in output." assert "dev-post-install" not in output, message.format( e="dev-post-install") cli_path = os.path.join(TEST_RUN_HOME, "node_modules", ".bin", "tns") assert File.exists( cli_path ), "NativeScript CLI installation failed - tns does not exist."
def get_screen(device_id, file_path): """ Save screen of iOS Simulator. :param device_id: Device identifier (Simualtor GUID) :param file_path: Name of image that will be saved. """ run(command="xcrun simctl io {0} screenshot {1}".format( device_id, file_path), log_level=CommandLogLevel.SILENT) assert File.exists( file_path), "Failed to get screenshot at " + file_path
def test_200_build_ios_inside_project(self): Folder.navigate_to(self.app_name) output = Tns.build_ios(tns_path=os.path.join("..", TNS_PATH), attributes={"--path": self.app_name}, assert_success=False, log_trace=True) Folder.navigate_to(TEST_RUN_HOME, relative_from_current_folder=False) assert "build/Debug-iphonesimulator/TestApp.app" in output assert File.exists( self.app_name + "/platforms/ios/build/Debug-iphonesimulator/TestApp.app")
def test_202_plugin_add_xcconfig_after_platform_add_ios(self): Tns.platform_add_ios(attributes={"--path": self.app_name, "--frameworkPath": IOS_PACKAGE}) plugin_path = TEST_RUN_HOME + "/data/CocoaPods/xcconfig-plugin.tgz" output = Tns.plugin_add(plugin_path, attributes={"--path": self.app_name}, assert_success=False) assert "Successfully installed plugin xcconfig-plugin." in output assert File.exists(self.app_name + "/node_modules/xcconfig-plugin/package.json") assert File.exists(self.app_name + "/node_modules/xcconfig-plugin/platforms/ios/build.xcconfig") assert File.exists(self.app_name + "/node_modules/xcconfig-plugin/platforms/ios/module.modulemap") assert File.exists(self.app_name + "/node_modules/xcconfig-plugin/platforms/ios/XcconfigPlugin.h") assert "xcconfig-plugin" in File.read(self.app_name + "/package.json") output = Tns.build_ios(attributes={"--path": self.app_name}) assert "Successfully prepared plugin xcconfig-plugin for ios." in output output = File.read(self.app_name + "/platforms/ios/plugins-debug.xcconfig") assert "OTHER_LDFLAGS = $(inherited) -l\"sqlite3\"" in output output = File.read(self.app_name + "/platforms/ios/TestApp/build-debug.xcconfig") assert "#include \"../plugins-debug.xcconfig\"" in output
def test_321_build_with_copy_to_option(self): # TODO: Remove those lines after https://github.com/NativeScript/nativescript-cli/issues/2547 is fixed. # This is required when build with different SDK Folder.cleanup(self.app_name) Tns.create_app(self.app_name) Tns.platform_add_android(attributes={"--path": self.app_name, "--frameworkPath": ANDROID_PACKAGE}) File.remove(self.debug_apk) Tns.build_android(attributes={"--path": self.app_name, "--copy-to": "./"}) assert File.exists(self.debug_apk) File.remove(self.debug_apk)
def test_399_build_project_with_gz_file(self): # This is required when build with different SDK Folder.cleanup(self.app_name) Tns.create_app(self.app_name) Tns.platform_add_android(attributes={"--path": self.app_name, "--frameworkPath": ANDROID_PACKAGE}) # Create zip run("tar -czf " + self.app_name + "/app/app.tar.gz " + self.app_name + "/app/app.js") assert File.exists(self.app_name + "/app/app.tar.gz") # Build the project Tns.build_android(attributes={"--path": self.app_name})
def tearDown(self): # Verify application state at the end of the test is correct if File.exists(self.app_name): data = TnsAsserts.get_package_json(self.app_name) assert "tns-android" in data[ "nativescript"], "'tns-android' not found under `nativescript` in package.json" assert "tns-android" not in data[ "dependencies"], "'tns-android' found under `dependencies` in package.json" BaseClass.tearDown(self) Folder.cleanup(self.platforms_android + '/build/outputs') Folder.cleanup("with space")
def test_406_build_release_without_key_options(self): output = Tns.build_android(attributes={ "--release": "", "--path": self.app_name }, assert_success=False) assert "When producing a release build, you need to specify all --key-store-* options." in output assert "# tns build android" in output assert not File.exists( os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APK_RELEASE_PATH, self.release_apk))
def test_002_build_android_release(self): Tns.build_android(attributes={"--path": self.app_name, "--keyStorePath": ANDROID_KEYSTORE_PATH, "--keyStorePassword": ANDROID_KEYSTORE_PASS, "--keyStoreAlias": ANDROID_KEYSTORE_ALIAS, "--keyStoreAliasPassword": ANDROID_KEYSTORE_ALIAS_PASS, "--release": "" }, log_trace=True) # Configs are respected assert 'release' in File.read(os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APP_PATH, 'config.json')) assert File.exists(os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APK_RELEASE_PATH, self.release_apk))
def test_310_build_ios_with_copy_to(self): Folder.cleanup("TestApp.app") File.remove("TestApp.ipa") # Tns.platform_add_ios(attributes={"--path": self.app_name, "--frameworkPath": IOS_RUNTIME_PATH}) Tns.build_ios(attributes={ "--path": self.app_name, "--copy-to": "./" }, log_trace=True) assert File.exists("TestApp.app") Tns.build_ios(attributes={ "--path": self.app_name, "--forDevice": "", "--release": "", "--copy-to": "./" }, log_trace=True) assert File.exists("TestApp.ipa")
def test_451_resources_update(self): target_app = os.path.join(TEST_RUN_HOME, BaseClass.app_name) source_app = os.path.join(TEST_RUN_HOME, 'data', 'apps', 'test-app-js-41') Folder.cleanup(target_app) Folder.copy(source_app, target_app) output = Tns.run_tns_command("resources update", attributes={"--path": self.app_name}) assert "Successfully updated your project's application resources '/Android' directory structure" in output assert "The previous version of your Android application resources has been renamed to '/Android-Pre-v4'" in output assert File.exists(self.app_name + "/app/App_Resources/Android-Pre-v4/app.gradle") assert File.exists(self.app_name + "/app/App_Resources/Android/app.gradle") assert File.exists( self.app_name + "/app/App_Resources/Android/src/main/AndroidManifest.xml") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/assets") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/java") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/res/values") Tns.build_android(attributes={"--path": self.app_name})
def created_ts(app_name, output=None): """ Assert TypeScript application is created properly. :param app_name: App name :param output: Output of `tns create command` """ # First make sure base app is created TnsAsserts.created(app_name=app_name, output=output) # Assert output contains TypeScript plugin if USE_YARN != "True": if Npm.version() < 5: assert 'nativescript-dev-typescript' in output # Assert files added with TypeScript plugin ts_config = os.path.join(app_name, 'tsconfig.json') ref_dts = os.path.join(app_name, 'references.d.ts') dts = os.path.join(app_name, TnsAsserts.TNS_MODULES, 'tns-core-modules.d.ts') # Assert content of files added with TypeScript plugin. assert File.exists(ts_config) assert not File.exists(ref_dts) assert File.exists(dts) ts_config_json = TnsAsserts.get_tsconfig_json(app_name=app_name) paths = ts_config_json.get('compilerOptions').get('paths') assert paths is not None, 'Paths missing in tsconfig.json' assert not Folder.is_empty(os.path.join(app_name, TnsAsserts.NODE_MODULES, 'nativescript-dev-typescript')) assert File.exists(os.path.join(app_name, TnsAsserts.HOOKS, 'before-prepare', 'nativescript-dev-typescript.js')) assert File.exists(os.path.join(app_name, TnsAsserts.HOOKS, 'before-watch', 'nativescript-dev-typescript.js')) assert File.exists(os.path.join(app_name, TnsAsserts.NODE_MODULES, 'typescript', 'bin', 'tsc'))
def platform_added(app_name, platform=Platform.NONE, output=None): """ Assert platform is added. :param app_name: Application name (folder where app is located). :param platform: Platforms that should be available. :param output: output of `tns platform add` command """ # Verify console output is correct if output is not None: if platform is Platform.ANDROID: assert 'Platform android successfully added' in output if platform is Platform.IOS and CURRENT_OS == OSType.OSX: assert 'Platform ios successfully added' in output else: assert 'Applications for platform ios can not be built on this OS' assert 'Project successfully created.' not in output # This is to handle test for app with space. # In this case we put app name inside ''. app_name = app_name.replace('\'', '') app_name = app_name.replace('\"', '') # Verify file and folder content if platform is Platform.NONE: assert not File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_IOS)) assert not File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID)) if platform is Platform.ANDROID or platform is Platform.BOTH: assert File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID)) if platform is Platform.IOS or platform is Platform.BOTH: if CURRENT_OS == OSType.OSX: assert File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_IOS)) else: assert not File.exists( os.path.join(app_name, TnsAsserts.PLATFORM_IOS))
def test_200_build_android_app_bundle(self): """Build app with android app bundle option. Verify the output(app.aab) and use bundletool to deploy on device""" path_to_aab = os.path.join(self.app_name, "platforms", "android", "app", "build", "outputs", "bundle", "debug", "app.aab") output = Tns.build_android(attributes={"--path": self.app_name, "--aab":""}, assert_success=False) assert "The build result is located at:" in output assert path_to_aab in output assert File.exists(path_to_aab) #Verify app can be deployed on emulator via bundletool # Use bundletool to create the .apks file self.bundletool_build(self.bundletool_path, path_to_aab, self.path_to_apks) assert File.exists(self.path_to_apks) # Deploy on device self.bundletool_deploy(self.bundletool_path, self.path_to_apks, device_id=EMULATOR_ID) # Start the app on device Adb.start_app(EMULATOR_ID, "org.nativescript.TestApp") # Verify app looks correct inside emulator app_started = Device.wait_for_text(device_id=EMULATOR_ID, text='TAP') assert app_started, 'App is not started on device'
def get_screen_text(device_id): """ Get text of current screen on mobile device. :param device_id: Device identifier (example: `emulator-5554`). :return: All the text visible on screen as string """ img_name = "actual_{0}_{1}.png".format(device_id, time.time()) actual_image_path = os.path.join(OUTPUT_FOLDER, "images", device_id, img_name) if File.exists(actual_image_path): File.remove(actual_image_path) Device.get_screen(device_id=device_id, file_path=actual_image_path) image = Image.open(actual_image_path).convert('LA') text = pytesseract.image_to_string(image) return text
def read(file_path): """ Read content of json file. :param file_path: Path to file. :return: Content of file as json object. """ # Check if file exists assert File.exists(file_path), 'Failed to find file: ' + file_path # Read it... with open(file_path) as json_file: data = json.load(json_file) return data
def test_200_build_android_inside_project_folder(self): Folder.navigate_to(self.app_name) output = Tns.build_android(tns_path=os.path.join("..", TNS_PATH), attributes={"--path": self.app_name}, assert_success=False, log_trace=True) Folder.navigate_to(TEST_RUN_HOME, relative_from_current_folder=False) assert successfully_prepared in output assert build_successful in output assert successfully_built in output assert File.exists( os.path.join(self.app_name, TnsAsserts.PLATFORM_ANDROID_APK_DEBUG_PATH, self.debug_apk))
def created(app_name, output=None, full_check=True): """ Assert application is created properly. :param app_name: App name :param output: Output of `tns create command` :param full_check: If true everything will be checked. If false only console output will be checked. """ # Assert console output is ok if output is not None: app = app_name.rsplit('/')[-1] if USE_YARN != "True": if Npm.version() < 5: assert 'nativescript-theme-core' in output assert 'Project {0} was successfully created'.format( app) in output, 'Failed to create {0}'.format(app) if full_check: # Assert files are ok assert File.exists(app_name) assert File.exists(app_name + '/node_modules/tns-core-modules/package.json') assert File.exists(app_name + '/node_modules/tns-core-modules/LICENSE') assert File.exists(app_name + '/node_modules/tns-core-modules/xml/xml.js') assert File.exists(app_name + '/node_modules/nativescript-theme-core') # Assert content of package.json app_id = app_name.replace(' ', '').replace('_', '').replace( '-', '').rsplit('/')[-1] strings = [ 'org.nativescript.{0}'.format(app_id), 'tns-core-modules', 'nativescript-theme-core' ] TnsAsserts.package_json_contains(app_name, string_list=strings)
def plugin_create(name, attributes={}, log_trace=False, assert_success=True, tns_path=None, force_clean=True): # Detect folder where plugin should be created path = attributes.get("--path") # noinspection PyUnusedLocal folder = path if path is None: path = name folder = name else: folder = path + os.sep + name # Clean target location if force_clean: Folder.cleanup(folder=path) # Execute plugin create command output = Tns.run_tns_command("plugin create --includeTypeScriptDemo=y " + name, attributes=attributes, log_trace=log_trace, tns_path=tns_path) if assert_success: # Verify command output assert "Will now rename some files" in output, "Post clone script not executed." assert "Screenshots removed" in output, "Post clone script not executed." assert "Solution for {0}".format(name) + " was successfully created" in output, 'Missing message in output.' assert "https://docs.nativescript.org/plugins/building-plugins" in output, 'Link to docs is missing.' # Verify created files and folders plugin_root = os.path.join(TEST_RUN_HOME, folder) readme = os.path.join(plugin_root, "README.md") src = os.path.join(plugin_root, "src") demo = os.path.join(plugin_root, "demo") post_clone_script = os.path.join(src, "scripts", "postclone.js") assert File.exists(readme), 'README.md do not exists.' assert not Folder.is_empty(src), 'src folder should exists and should not be empty.' assert not Folder.is_empty(demo), 'demo folder should exists and should not be empty.' assert not File.exists(post_clone_script), 'Post clone script should not exists in plugin src folder.' return output
def test_399_build_project_with_gz_file(self): # This is required when build with different SDK Folder.cleanup(self.app_name) Tns.create_app(self.app_name) Tns.platform_add_android(attributes={ "--path": self.app_name, "--frameworkPath": ANDROID_PACKAGE }) # Create zip run("tar -czf " + self.app_name + "/app/app.tar.gz " + self.app_name + "/app/app.js") assert File.exists(self.app_name + "/app/app.tar.gz") # Build the project Tns.build_android(attributes={"--path": self.app_name})
def test_400_plugin_add_sandbox_pod_can_write_outside_app_folder_by_default(self): Tns.create_app(self.app_name) Tns.platform_add_ios(attributes={"--path": self.app_name, "--frameworkPath": IOS_PACKAGE}) plugin = os.path.join(TEST_RUN_HOME, "data", "CocoaPods", "nativescript-ios-fail-with-sandbox-plugin.tgz") output = Tns.plugin_add(plugin, attributes={"--path": self.app_name}, assert_success=False) assert "Successfully installed plugin nativescript-ios-fail-with-sandbox-plugin." in output assert "nativescript-ios-fail-with-sandbox-plugin" in File.read(self.app_name + "/package.json") output = Tns.prepare_ios(attributes={"--path": self.app_name}, assert_success=False) assert "Successfully prepared " + \ "plugin nativescript-ios-fail-with-sandbox-plugin for ios." in output assert "sh: ../I_MADE_THIS_FILE.txt: Operation not permitted" not in output assert File.exists(self.app_name + "/platforms/I_MADE_THIS_FILE.txt")
def test_321_build_with_copy_to_option(self): # TODO: Remove those lines after https://github.com/NativeScript/nativescript-cli/issues/2547 is fixed. # This is required when build with different SDK Folder.cleanup(self.app_name) Tns.create_app(self.app_name) Tns.platform_add_android(attributes={ "--path": self.app_name, "--frameworkPath": ANDROID_PACKAGE }) File.remove(self.debug_apk) Tns.build_android(attributes={ "--path": self.app_name, "--copy-to": "./" }) assert File.exists(self.debug_apk) File.remove(self.debug_apk)
def test_401_plugin_add_sandbox_pod_can_not_write_outside_app_folder_if_use_pod_sandbox_is_true(self): File.replace("node_modules/nativescript/config/config.json", '"USE_POD_SANDBOX": false', '"USE_POD_SANDBOX": true') Tns.create_app(self.app_name) Tns.platform_add_ios(attributes={"--path": self.app_name, "--frameworkPath": IOS_PACKAGE}) plugin = os.path.join(TEST_RUN_HOME, "data", "CocoaPods", "nativescript-ios-fail-with-sandbox-plugin.tgz") output = Tns.plugin_add(plugin, attributes={"--path": self.app_name}, assert_success=False) assert "Successfully installed plugin nativescript-ios-fail-with-sandbox-plugin." in output assert "nativescript-ios-fail-with-sandbox-plugin" in File.read(self.app_name + "/package.json") output = Tns.prepare_ios(attributes={"--path": self.app_name}, assert_success=False) assert "Successfully prepared " + \ "plugin nativescript-ios-fail-with-sandbox-plugin for ios." in output assert "sh: ../I_MADE_THIS_FILE.txt: Operation not permitted" in output assert not File.exists(self.app_name + "/platforms/I_MADE_THIS_FILE.txt")
def create_app(app_name, attributes={}, log_trace=False, assert_success=True, update_modules=True, force_clean=True, measureTime=False): if force_clean: if File.exists(app_name): Folder.cleanup(app_name) path = app_name attributes_to_string = "" for k, v in attributes.iteritems(): if "--path" in k: path = v attributes_to_string = "".join("{0} {1}".format(k, v)) attr = {} if not any(s in attributes_to_string for s in ("--ng", "--template", "--tsc", "--vue")): if BRANCH == "master": attr = { "--template": SUT_FOLDER + os.path.sep + "tns-template-hello-world.tgz" } else: attr = {"--template": "tns-template-hello-world"} attr.update(attributes) if app_name is None: output = Tns.run_tns_command("create ", attributes=attr, log_trace=log_trace, measureTime=measureTime) else: output = Tns.run_tns_command("create \"" + app_name + "\"", attributes=attr, log_trace=log_trace, measureTime=measureTime) if assert_success: TnsAsserts.created(app_name=app_name, output=output) if update_modules: Tns.update_modules(path) # Tns.ensure_app_resources(path) return output
def run_android(attributes={}, assert_success=True, log_trace=False, timeout=COMMAND_TIMEOUT, tns_path=None, wait=True): output = Tns.run_tns_command("run android", attributes=attributes, log_trace=log_trace, timeout=timeout, tns_path=tns_path, wait=wait) if assert_success: assert "Project successfully built" in output assert "Successfully installed on device with identifier" in output app_name = Tns.__get_app_name_from_attributes(attributes=attributes) # apk_name = Tns.__get_final_package_name(app_name, platform=Platform.ANDROID) apk_name = "app" debug_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_DEBUG_PATH, apk_name) release_app_path = os.path.join(app_name, TnsAsserts.PLATFORM_ANDROID_APK_RELEASE_PATH, apk_name) if "--release" in attributes.keys(): apk_path = release_app_path + "-release.apk" else: apk_path = debug_app_path + "-debug.apk" apk_path = apk_path.replace("\"", "") # Handle projects with space assert File.exists(apk_path), "Apk file does not exist at " + apk_path return output
def test_451_resources_update(self): target_app = os.path.join(TEST_RUN_HOME, BaseClass.app_name) source_app = os.path.join(TEST_RUN_HOME, 'data', 'apps', 'test-app-js-41') Folder.cleanup(target_app) Folder.copy(source_app, target_app) output = Tns.run_tns_command("resources update", attributes={"--path": self.app_name}) assert "Successfully updated your project's application resources '/Android' directory structure" in output assert "The previous version of your Android application resources has been renamed to '/Android-Pre-v4'" in output assert File.exists(self.app_name + "/app/App_Resources/Android-Pre-v4/app.gradle") assert File.exists(self.app_name + "/app/App_Resources/Android/app.gradle") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/AndroidManifest.xml") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/assets") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/java") assert File.exists(self.app_name + "/app/App_Resources/Android/src/main/res/values") Tns.build_android(attributes={"--path": self.app_name})