Пример #1
0
    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 wait(device_id, timeout=300):
        """
        Wait until emulator is up and running.
        :param device_id: Device name
        :param timeout: Timeout until device is ready (in seconds).
        :return: True if device is ready before timeout, otherwise - False.
        """
        booted = False
        start_time = time.time()
        end_time = start_time + timeout
        while not booted:
            time.sleep(5)
            booted = Emulator.is_running(device_id=device_id)
            if (booted is True) or (time.time() > end_time):
                break

        # If booted, make sure screen will not lock
        if booted:
            Adb.run(command='shell settings put system screen_off_timeout -1', device_id=device_id)

        # If booted, make sure screen will not lock
        if booted:
            text = Adb.get_page_source(device_id=device_id)
            if "android.process.acore" in text:
                print "Error dialog detected! Try to kill it..."
                Device.click(device_id=device_id, text="OK", timeout=10)
        return booted
Пример #3
0
    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'
Пример #4
0
    def wait(device_id, timeout=300):
        """
        Wait until emulator is up and running.
        :param device_id: Device name
        :param timeout: Timeout until device is ready (in seconds).
        :return: True if device is ready before timeout, otherwise - False.
        """
        booted = False
        start_time = time.time()
        end_time = start_time + timeout
        while not booted:
            time.sleep(5)
            booted = Emulator.is_running(device_id=device_id)
            if (booted is True) or (time.time() > end_time):
                break

        # If booted, make sure screen will not lock
        if booted:
            Adb.run(command='shell settings put system screen_off_timeout -1',
                    device_id=device_id)

        # If booted, make sure screen will not lock
        if booted:
            text = Adb.get_page_source(device_id=device_id)
            if "android.process.acore" in text:
                print "Error dialog detected! Try to kill it..."
                Device.click(device_id=device_id, text="OK", timeout=10)
        return booted
Пример #5
0
    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_210_tns_run_android_add_remove_files_and_folders(self):
        """
        New files and folders should be synced properly.
        """

        log = Tns.run_android(attributes={'--path': self.app_name, '--device': self.DEVICE_ID}, wait=False,
                              assert_success=False)
        strings = ['Successfully synced application', self.DEVICE_ID]
        Tns.wait_for_log(log_file=log, string_list=strings, timeout=120, check_interval=10)

        # Add new files
        new_file_name = 'main-page2.xml'
        source_file = os.path.join(self.app_name, 'app', 'main-page.xml')
        destination_file = os.path.join(self.app_name, 'app', new_file_name)
        File.copy(source_file, destination_file)
        strings = ['Successfully transferred main-page2.xml', 'Successfully synced application', self.DEVICE_ID]
        Tns.wait_for_log(log_file=log, string_list=strings)

        # Verify new file is synced and available on device.
        error_message = 'Newly created file {0} not found on {1}'.format(new_file_name, self.DEVICE_ID)
        app_id = Tns.get_app_id(app_name=self.app_name)
        path = 'app/{0}'.format(new_file_name)
        assert Adb.path_exists(device_id=self.DEVICE_ID, package_id=app_id, path=path), error_message

        # Revert changes(rename file and delete file)
        File.copy(destination_file, source_file)
        File.remove(destination_file)
        strings = ['Successfully transferred main-page.xml', 'Successfully synced application', self.DEVICE_ID]
        Tns.wait_for_log(log_file=log, string_list=strings)

        # Verify new file is synced and available on device.
        error_message = '{0} was deleted, but still available on {1}'.format(new_file_name, self.DEVICE_ID)
        assert Adb.path_does_not_exist(device_id=self.DEVICE_ID, package_id=app_id, path=path), error_message

        # Add folder
        new_folder_name = 'feature2'
        source_file = os.path.join(self.app_name, 'app', 'feature1')
        destination_file = os.path.join(self.app_name, 'app', new_folder_name)
        Folder.copy(source_file, destination_file)
        strings = ['Successfully transferred', 'Successfully transferred', 'feature1.js', self.DEVICE_ID]
        Tns.wait_for_log(log_file=log, string_list=strings)

        # Verify new folder is synced and available on device.
        error_message = 'Newly created folder {0} not found on {1}'.format(new_folder_name, self.DEVICE_ID)
        path = 'app/{0}'.format(new_folder_name)
        assert Adb.path_exists(device_id=self.DEVICE_ID, package_id=app_id, path=path, timeout=20), error_message
        path = 'app/{0}/{1}'.format(new_folder_name, 'feature1.js')
        assert Adb.path_exists(device_id=self.DEVICE_ID, package_id=app_id, path=path, timeout=20), error_message

        # Delete folder
        Folder.cleanup(destination_file)
        strings = ['Successfully synced application', self.DEVICE_ID]
        Tns.wait_for_log(log_file=log, string_list=strings)

        # Verify new folder is deleted from device.
        error_message = 'Deleted folder {0} is still available on {1}'.format(new_folder_name, self.DEVICE_ID)
        assert Adb.path_does_not_exist(device_id=self.DEVICE_ID, package_id=app_id, path=path), error_message
Пример #7
0
 def turn_on_screen(device_id):
     """
     Turn on screen.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
         Adb.turn_on_screen(device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #8
0
 def clear_log(device_id):
     """
     Flush the entire log.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
         Adb.clear_logcat(device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #9
0
 def install_preview_app(device_id, platform=Platform.BOTH):
     """Installs Preview App on emulator and simulator"""
     package_android = os.path.join(SUT_FOLDER, 'app-universal-release.apk')
     package_ios = os.path.join(SUT_FOLDER, 'nsplaydev.app')
     if platform is Platform.IOS:
         Simulator.install(package_ios)
     elif platform is Platform.ANDROID:
         Adb.install(package_android, device_id)
     elif platform is Platform.BOTH:
         Adb.install(package_android, device_id)
         Simulator.install(package_ios)
Пример #10
0
 def install_playground_app(device_id, platform=Platform.BOTH):
     """Installs Playground App on emulator and simulator"""
     package_android = os.path.join(SUT_FOLDER, "app-release.apk")
     package_ios = os.path.join(SUT_FOLDER, 'nsplay.app')
     if platform is Platform.IOS:
         Simulator.install(package_ios)
     elif platform is Platform.ANDROID: 
         Adb.install(package_android, device_id)
     elif platform is Platform.BOTH: 
         Adb.install(package_android, device_id)
         Simulator.install(package_ios)
Пример #11
0
 def install_playground_app(device_id, platform=Platform.BOTH):
     """Installs Playground App on emulator and simulator"""
     package_android = os.path.join(SUT_FOLDER, "app-release.apk")
     package_ios = os.path.join(SUT_FOLDER, 'nsplay.app')
     if platform is Platform.IOS:
         Simulator.install(package_ios)
     elif platform is Platform.ANDROID:
         Adb.install(package_android, device_id)
     elif platform is Platform.BOTH:
         Adb.install(package_android, device_id)
         Simulator.install(package_ios)
Пример #12
0
 def start_app(device_id, app_id):
     """
     Start application.
     :param device_id: Device id.
     :param app_id: App id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
         Adb.start_app(device_id, app_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #13
0
 def turn_on_screen(device_id):
     """
     Turn on screen.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type
                                                 == DeviceType.ANDROID):
         Adb.turn_on_screen(device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #14
0
 def install_preview_app(device_id, platform=Platform.BOTH):
     """Installs Preview App on emulator and simulator"""
     package_android = os.path.join(SUT_FOLDER, 'app-universal-release.apk')
     package_ios = os.path.join(SUT_FOLDER, 'nsplaydev.app')
     if platform is Platform.IOS:
         Simulator.install(package_ios)
     elif platform is Platform.ANDROID: 
         Adb.install(package_android, device_id)
     elif platform is Platform.BOTH: 
         Adb.install(package_android, device_id)
         Simulator.install(package_ios)
Пример #15
0
 def clear_log(device_id):
     """
     Flush the entire log.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type
                                                 == DeviceType.ANDROID):
         Adb.clear_logcat(device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #16
0
 def install_app(app_file_path, device_id):
     """
     Install application.
     :param app_file_path: File path to app.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
         Adb.install(app_file_path, device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #17
0
 def install_app(app_file_path, device_id):
     """
     Install application.
     :param app_file_path: File path to app.
     :param device_id: Device id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type
                                                 == DeviceType.ANDROID):
         Adb.install(app_file_path, device_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #18
0
 def start_app(device_id, app_id):
     """
     Start application.
     :param device_id: Device id.
     :param app_id: App id.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type
                                                 == DeviceType.ANDROID):
         Adb.start_app(device_id, app_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #19
0
 def stop_application(device_id, app_id):
     """
     Stop application
     :param device_id: Device identifier
     :param app_id: Bundle identifier (example: org.nativescript.TestApp)
     """
     device_type = Device.__get_device_type(device_id=device_id)
     if device_type is DeviceType.SIMULATOR:
         Simulator.stop_application(app_id=app_id)
     elif device_type is DeviceType.IOS:
         raise NotImplementedError
     else:
         Adb.stop_application(device_id=device_id, app_id=app_id)
Пример #20
0
 def stop_application(device_id, app_id):
     """
     Stop application
     :param device_id: Device identifier
     :param app_id: Bundle identifier (example: org.nativescript.TestApp)
     """
     device_type = Device.__get_device_type(device_id=device_id)
     if device_type is DeviceType.SIMULATOR:
         Simulator.stop_application(app_id=app_id)
     elif device_type is DeviceType.IOS:
         raise NotImplementedError
     else:
         Adb.stop_application(device_id=device_id, app_id=app_id)
    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'
Пример #22
0
 def is_running(device_id, app_id):
     """
     Check if app is running.
     :param app_id: Bundle identifier (example: org.nativescript.TestApp)
     :param device_id: Device identifier
     :return: True if application is running
     """
     device_type = Device.__get_device_type(device_id=device_id)
     if device_type is DeviceType.SIMULATOR:
         raise NotImplementedError
     elif device_type is DeviceType.IOS:
         raise NotImplementedError
     else:
         Adb.is_application_running(device_id=device_id, app_id=app_id)
Пример #23
0
 def is_running(device_id, app_id):
     """
     Check if app is running.
     :param app_id: Bundle identifier (example: org.nativescript.TestApp)
     :param device_id: Device identifier
     :return: True if application is running
     """
     device_type = Device.__get_device_type(device_id=device_id)
     if device_type is DeviceType.SIMULATOR:
         raise NotImplementedError
     elif device_type is DeviceType.IOS:
         raise NotImplementedError
     else:
         Adb.is_application_running(device_id=device_id, app_id=app_id)
Пример #24
0
 def uninstall_app(app_prefix, platform):
     """
     Uninstall all apps on all connected physical devices.
     :param app_prefix: App prefix, for example: org.nativescript.
     :param platform: Platform enum value (Platform.ANDROID or Platform.IOS)
     """
     device_ids = Device.get_ids(platform=platform, include_emulators=True)
     if platform == Platform.ANDROID:
         for device_id in device_ids:
             Adb.uninstall_all_apps(device_id=device_id)
             Adb.run(command="shell rm -rf /data/local/tmp/*", device_id=device_id, log_level=CommandLogLevel.FULL)
     elif platform == Platform.IOS:
         for device_id in device_ids:
             IDevice.uninstall_all_app(device_id=device_id, app_prefix=app_prefix)
Пример #25
0
    def ensure_available(platform):
        """
        Ensure device is available.
        :param platform: `Platform.ANDROID` or `Platform.IOS`
        """
        count = Device.get_count(platform)
        if count > 0:
            print "{0} {1} device(s) attached.".format(count, platform)
        else:
            raise TypeError("No real devices attached.")

        # If device is Android, make sure /data/local/tmp is clean
        if platform == Platform.ANDROID:
            for device_id in Device.get_ids(platform=Platform.ANDROID, include_emulators=True):
                Adb.run(command="shell rm -rf /data/local/tmp/*", device_id=device_id, log_level=CommandLogLevel.FULL)
Пример #26
0
    def wait_for_text(device_id, text="", timeout=60):
        """
        Wait for text to be visible on screen of device.
        :param device_id: Device identifier (example: `emulator-5554`).
        :param text: Text that should be visible on the screen.
        :param timeout: Timeout in seconds.
        :return: True if text found, False if not found.
        """

        # IMPORTANT NOTE !!!!
        # UIAuto.wait_for_text() does not work well with cases when you specify partial text of element.
        # Example: Element text is "42 taps left" and you try to wait for "taps" string only
        # TODO: Think about some fix (may be xpath on view hierarchy)

        device_type = Device.__get_device_type(device_id)
        if device_type == DeviceType.ANDROID or device_type == DeviceType.EMULATOR:
            return Adb.wait_for_text(device_id=device_id, text=text, timeout=timeout)
        else:
            t_end = time.time() + timeout
            found = False
            actual_text = ""
            while time.time() < t_end:
                actual_text = Device.get_screen_text(device_id=device_id)
                if text in actual_text:
                    print text + " found on screen of " + device_id
                    found = True
                    break
                else:
                    print text + " NOT found on screen of " + device_id
                    time.sleep(5)
            if not found:
                print "ACTUAL TEXT:"
                print actual_text
            return found
    def test_003_android_run_hmr_wrong_xml(self):
        log = Tns.run_android(attributes={'--path': self.app_name, '--device': EMULATOR_ID, '--hmr': ''}, wait=False,
                              assert_success=False)
        Tns.wait_for_log(log_file=log, string_list=HelpersHMR.run_hmr,
                         not_existing_string_list=HelpersHMR.errors_hmr, timeout=240)

        # Verify app looks correct inside emulator
        Device.screen_match(device_name=EMULATOR_NAME, device_id=EMULATOR_ID,
                            expected_image=HelpersHMR.image_original)

        # Break the app with invalid xml changes
        ReplaceHelper.replace(self.app_name, ReplaceHelper.CHANGE_XML_INVALID_SYNTAX)

        # Verify console notify user for broken xml
        # strings = ['for activity org.nativescript.TestApp / com.tns.ErrorReportActivity']
        strings = ['com.tns.NativeScriptException', 'Parsing XML at', 'Successfully synced application', EMULATOR_ID]
        Tns.wait_for_log(log_file=log, string_list=strings, timeout=120, check_interval=10)
        assert Adb.wait_for_text(device_id=EMULATOR_ID, text="Exception", timeout=30), "Error activity not found!"

        # Revert changes
        ReplaceHelper.rollback(self.app_name, ReplaceHelper.CHANGE_XML_INVALID_SYNTAX)
        strings = ['JS: HMR: Hot Module Replacement Enabled. Waiting for signal.',
                   'Successfully synced application', EMULATOR_ID]
        Tns.wait_for_log(log_file=log, string_list=strings, timeout=120, check_interval=10)

        # Verify app looks correct inside emulator
        Device.screen_match(device_name=EMULATOR_NAME, device_id=EMULATOR_ID,
                            expected_image=HelpersHMR.image_original)
Пример #28
0
 def uninstall_app(app_prefix, platform):
     """
     Uninstall all apps on all connected physical devices.
     :param app_prefix: App prefix, for example: org.nativescript.
     :param platform: Platform enum value (Platform.ANDROID or Platform.IOS)
     """
     device_ids = Device.get_ids(platform=platform, include_emulators=True)
     if platform == Platform.ANDROID:
         for device_id in device_ids:
             Adb.uninstall_all_apps(device_id=device_id)
             Adb.run(command="shell rm -rf /data/local/tmp/*",
                     device_id=device_id,
                     log_level=CommandLogLevel.FULL)
     elif platform == Platform.IOS:
         for device_id in device_ids:
             IDevice.uninstall_all_app(device_id=device_id,
                                       app_prefix=app_prefix)
Пример #29
0
    def ensure_available(platform):
        """
        Ensure device is available.
        :param platform: `Platform.ANDROID` or `Platform.IOS`
        """
        count = Device.get_count(platform)
        if count > 0:
            print "{0} {1} device(s) attached.".format(count, platform)
        else:
            raise TypeError("No real devices attached.")

        # If device is Android, make sure /data/local/tmp is clean
        if platform == Platform.ANDROID:
            for device_id in Device.get_ids(platform=Platform.ANDROID,
                                            include_emulators=True):
                Adb.run(command="shell rm -rf /data/local/tmp/*",
                        device_id=device_id,
                        log_level=CommandLogLevel.FULL)
Пример #30
0
 def get_ids(platform, include_emulators=False):
     """
     Get IDs of all connected physical devices.
     :param platform: `Platform.ANDROID` or `Platform.IOS`
     :return: List of device identifiers.
     """
     if platform is Platform.IOS:
         return IDevice.get_devices()
     elif platform is Platform.ANDROID:
         return Adb.get_devices(include_emulators=include_emulators)
     else:
         raise NameError('Invalid platform')
Пример #31
0
 def get_ids(platform, include_emulators=False):
     """
     Get IDs of all connected physical devices.
     :param platform: `Platform.ANDROID` or `Platform.IOS`
     :return: List of device identifiers.
     """
     if platform is Platform.IOS:
         return IDevice.get_devices()
     elif platform is Platform.ANDROID:
         return Adb.get_devices(include_emulators=include_emulators)
     else:
         raise NameError('Invalid platform')
Пример #32
0
    def test_003_android_run_hmr_wrong_xml(self):
        log = Tns.run_android(attributes={
            '--path': self.app_name,
            '--device': EMULATOR_ID,
            '--hmr': ''
        },
                              wait=False,
                              assert_success=False)
        Tns.wait_for_log(log_file=log,
                         string_list=HelpersHMR.run_hmr,
                         not_existing_string_list=HelpersHMR.errors_hmr,
                         timeout=240)

        # Verify app looks correct inside emulator
        Device.screen_match(device_name=EMULATOR_NAME,
                            device_id=EMULATOR_ID,
                            expected_image=HelpersHMR.image_original)

        # Break the app with invalid xml changes
        ReplaceHelper.replace(self.app_name,
                              ReplaceHelper.CHANGE_XML_INVALID_SYNTAX)

        # Verify console notify user for broken xml
        # strings = ['for activity org.nativescript.TestApp / com.tns.ErrorReportActivity']
        strings = [
            'com.tns.NativeScriptException', 'Parsing XML at',
            'Successfully synced application', EMULATOR_ID
        ]
        Tns.wait_for_log(log_file=log,
                         string_list=strings,
                         timeout=120,
                         check_interval=10)
        assert Adb.wait_for_text(device_id=EMULATOR_ID,
                                 text="Exception",
                                 timeout=30), "Error activity not found!"

        # Revert changes
        ReplaceHelper.rollback(self.app_name,
                               ReplaceHelper.CHANGE_XML_INVALID_SYNTAX)
        strings = [
            'JS: HMR: Hot Module Replacement Enabled. Waiting for signal.',
            'Successfully synced application', EMULATOR_ID
        ]
        Tns.wait_for_log(log_file=log,
                         string_list=strings,
                         timeout=120,
                         check_interval=10)

        # Verify app looks correct inside emulator
        Device.screen_match(device_name=EMULATOR_NAME,
                            device_id=EMULATOR_ID,
                            expected_image=HelpersHMR.image_original)
Пример #33
0
 def get_start_time(device_id, app_id):
     """
     Get start time of application. Examples:
     - Android - I/ActivityManager(19531): Displayed org.nativescript.TestApp/com.tns.NativeScriptActivity: +3s452ms
     - iOS - TODO(vchimev)
     :param device_id: Device id.
     :param app_id: App id.
     :return: Start time.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type == DeviceType.ANDROID):
         return Adb.get_start_time(device_id, app_id)
     else:
         raise NotImplementedError('Not Implemented for 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 is_running(device_id):
     """
     Check if device is is currently available.
     :param device_id: Device id.
     :return: True if running, False if not running.
     """
     if CURRENT_OS == OSType.WINDOWS:
         command = "shell dumpsys window windows | findstr mFocusedApp"
     else:
         command = "shell dumpsys window windows | grep -E 'mFocusedApp'"
     output = Adb.run(command=command, device_id=device_id, log_level=CommandLogLevel.SILENT)
     if 'ActivityRecord' in output:
         return True
     else:
         return False
Пример #36
0
 def get_start_time(device_id, app_id):
     """
     Get start time of application. Examples:
     - Android - I/ActivityManager(19531): Displayed org.nativescript.TestApp/com.tns.NativeScriptActivity: +3s452ms
     - iOS - TODO(vchimev)
     :param device_id: Device id.
     :param app_id: App id.
     :return: Start time.
     """
     device_type = Device.__get_device_type(device_id)
     if (device_type == DeviceType.EMULATOR) or (device_type
                                                 == DeviceType.ANDROID):
         return Adb.get_start_time(device_id, app_id)
     else:
         raise NotImplementedError('Not Implemented for iOS!')
Пример #37
0
 def is_running(device_id):
     """
     Check if device is is currently available.
     :param device_id: Device id.
     :return: True if running, False if not running.
     """
     if CURRENT_OS == OSType.WINDOWS:
         command = "shell dumpsys window windows | findstr mFocusedApp"
     else:
         command = "shell dumpsys window windows | grep -E 'mFocusedApp'"
     output = Adb.run(command=command,
                      device_id=device_id,
                      log_level=CommandLogLevel.SILENT)
     if 'ActivityRecord' in output:
         return True
     else:
         return False
 def ensure_available(emulator_name=EMULATOR_NAME):
     """
     Ensure Android Emulator is running.
     """
     found = Emulator.is_running(device_id=EMULATOR_ID)
     if found:
         print 'Emulator already running, reboot it...'
         Adb.run(command="shell rm -rf /data/local/tmp/*", device_id=EMULATOR_ID, log_level=CommandLogLevel.FULL)
         Adb.uninstall_all_apps(device_id=EMULATOR_ID)
         Adb.run(command="reboot", device_id=EMULATOR_ID, log_level=CommandLogLevel.FULL)
         Emulator.wait(device_id=EMULATOR_ID)
     else:
         Emulator.stop()
         Emulator.start(emulator_name=emulator_name, port=EMULATOR_PORT)
     return found
Пример #39
0
    def wait_for_text(device_id, text="", timeout=60):
        """
        Wait for text to be visible on screen of device.
        :param device_id: Device identifier (example: `emulator-5554`).
        :param text: Text that should be visible on the screen.
        :param timeout: Timeout in seconds.
        :return: True if text found, False if not found.
        """

        # IMPORTANT NOTE !!!!
        # UIAuto.wait_for_text() does not work well with cases when you specify partial text of element.
        # Example: Element text is "42 taps left" and you try to wait for "taps" string only
        # TODO: Think about some fix (may be xpath on view hierarchy)

        device_type = Device.__get_device_type(device_id)
        if device_type == DeviceType.ANDROID or device_type == DeviceType.EMULATOR:
            return Adb.wait_for_text(device_id=device_id,
                                     text=text,
                                     timeout=timeout)
        else:
            t_end = time.time() + timeout
            found = False
            actual_text = ""
            while time.time() < t_end:
                actual_text = Device.get_screen_text(device_id=device_id)
                if text in actual_text:
                    print text + " found on screen of " + device_id
                    found = True
                    break
                else:
                    print text + " NOT found on screen of " + device_id
                    time.sleep(5)
            if not found:
                print "ACTUAL TEXT:"
                print actual_text
            return found
Пример #40
0
 def ensure_available(emulator_name=EMULATOR_NAME):
     """
     Ensure Android Emulator is running.
     """
     found = Emulator.is_running(device_id=EMULATOR_ID)
     if found:
         print 'Emulator already running, reboot it...'
         Adb.run(command="shell rm -rf /data/local/tmp/*",
                 device_id=EMULATOR_ID,
                 log_level=CommandLogLevel.FULL)
         Adb.uninstall_all_apps(device_id=EMULATOR_ID)
         Adb.run(command="reboot",
                 device_id=EMULATOR_ID,
                 log_level=CommandLogLevel.FULL)
         Emulator.wait(device_id=EMULATOR_ID)
     else:
         Emulator.stop()
         Emulator.start(emulator_name=emulator_name, port=EMULATOR_PORT)
     return found
Пример #41
0
    def screen_match(device_name,
                     device_id,
                     expected_image,
                     tolerance=0.1,
                     timeout=30):
        """
        Verify screen match expected image.
        :param device_name: Name of device (name of Android avd image, or name or iOS Simulator).
        :param device_id: Device identifier (example: `emulator-5554`).
        :param expected_image: Name of expected image.
        :param tolerance: Tolerance in percents.
        :param timeout: Timeout in seconds.
        """

        device_type = Device.__get_device_type(device_id)
        if device_type == DeviceType.IOS:
            type = run(command="ideviceinfo | grep ProductType",
                       log_level=CommandLogLevel.SILENT)
            type = type.replace(',', '')
            type = type.replace('ProductType:', '').strip(' ')
            device_name = type

        print "Verify {0} looks correct...".format(expected_image)
        expected_image_original_path = os.path.join(
            "data", "images", device_name, "{0}.png".format(expected_image))
        actual_image_path = os.path.join(
            OUTPUT_FOLDER, "images", device_name,
            "{0}_actual.png".format(expected_image))
        diff_image_path = os.path.join(OUTPUT_FOLDER, "images", device_name,
                                       "{0}_diff.png".format(expected_image))
        expected_image_path = os.path.join(
            OUTPUT_FOLDER, "images", device_name,
            "{0}_expected.png".format(expected_image))

        if File.exists(expected_image_original_path):
            t_end = time.time() + timeout
            diff = 100.0
            are_equal = False
            comparison_result = None
            while time.time() < t_end:
                time.sleep(1)
                if Device.get_screen(device_id=device_id,
                                     file_path=actual_image_path):
                    comparison_result = ImageUtils.image_match(
                        actual_image_path=actual_image_path,
                        expected_image_path=expected_image_original_path,
                        tolerance=tolerance)
                    are_equal = comparison_result[0]
                    diff = comparison_result[1]
                    if are_equal:
                        print "{0} looks OK.".format(expected_image)
                        break  # Exist if images look OK.
                    else:
                        time.sleep(2)
                        print "{0} does not match. Diff is {1} %. Wait...".format(
                            expected_image, diff)
                else:
                    print "Failed to get image from {0}".format(device_id)

            # Report results after timeout is over
            if not are_equal:
                # Save expected and diff images (actual is already there)
                diff_image_final_path = os.path.join("out", diff_image_path)
                print "Diff image will be saved at " + diff_image_final_path
                File.copy(src=expected_image_original_path,
                          dest=expected_image_path)
                comparison_result[2].save(diff_image_final_path)
                # Get logs (from android devices).
                if device_type == DeviceType.EMULATOR or device_type == DeviceType.ANDROID:
                    log_path = diff_image_final_path.replace(
                        '_diff.png', '.log')
                    print "Console logs will be saved at " + log_path
                    log = Adb.get_logcat(device_id=device_id)
                    if len(log) < 10000:
                        print log
                    File.write(file_path=log_path, text=log)
            assert are_equal, "Current image on {0} does not match expected image {1}. Diff is {2}%". \
                format(device_name, expected_image, diff)
        else:
            # If expected image is not found actual will be saved as expected.
            print "Expected image not found. Actual image will be saved as expected: " + expected_image_original_path
            time.sleep(timeout)
            Device.get_screen(device_id, expected_image_original_path)
            assert False, "Expected image not found!"
Пример #42
0
    def screen_match(device_name, device_id, expected_image, tolerance=0.1, timeout=30):
        """
        Verify screen match expected image.
        :param device_name: Name of device (name of Android avd image, or name or iOS Simulator).
        :param device_id: Device identifier (example: `emulator-5554`).
        :param expected_image: Name of expected image.
        :param tolerance: Tolerance in percents.
        :param timeout: Timeout in seconds.
        """

        device_type = Device.__get_device_type(device_id)
        if device_type == DeviceType.IOS:
            type = run(command="ideviceinfo | grep ProductType", log_level=CommandLogLevel.SILENT)
            type = type.replace(',', '')
            type = type.replace('ProductType:', '').strip(' ')
            device_name = type

        print "Verify {0} looks correct...".format(expected_image)
        expected_image_original_path = os.path.join("data", "images", device_name, "{0}.png".format(expected_image))
        actual_image_path = os.path.join(OUTPUT_FOLDER, "images", device_name, "{0}_actual.png".format(expected_image))
        diff_image_path = os.path.join(OUTPUT_FOLDER, "images", device_name, "{0}_diff.png".format(expected_image))
        expected_image_path = os.path.join(OUTPUT_FOLDER, "images", device_name,
                                           "{0}_expected.png".format(expected_image))

        if File.exists(expected_image_original_path):
            t_end = time.time() + timeout
            diff = 100.0
            are_equal = False
            comparison_result = None
            while time.time() < t_end:
                time.sleep(1)
                if Device.get_screen(device_id=device_id, file_path=actual_image_path):
                    comparison_result = ImageUtils.image_match(actual_image_path=actual_image_path,
                                                               expected_image_path=expected_image_original_path,
                                                               tolerance=tolerance)
                    are_equal = comparison_result[0]
                    diff = comparison_result[1]
                    if are_equal:
                        print "{0} looks OK.".format(expected_image)
                        break  # Exist if images look OK.
                    else:
                        time.sleep(2)
                        print "{0} does not match. Diff is {1} %. Wait...".format(expected_image, diff)
                else:
                    print "Failed to get image from {0}".format(device_id)

            # Report results after timeout is over
            if not are_equal:
                # Save expected and diff images (actual is already there)
                diff_image_final_path = os.path.join("out", diff_image_path)
                print "Diff image will be saved at " + diff_image_final_path
                File.copy(src=expected_image_original_path, dest=expected_image_path)
                comparison_result[2].save(diff_image_final_path)
                # Get logs (from android devices).
                if device_type == DeviceType.EMULATOR or device_type == DeviceType.ANDROID:
                    log_path = diff_image_final_path.replace('_diff.png', '.log')
                    print "Console logs will be saved at " + log_path
                    log = Adb.get_logcat(device_id=device_id)
                    if len(log) < 10000:
                        print log
                    File.write(file_path=log_path, text=log)
            assert are_equal, "Current image on {0} does not match expected image {1}. Diff is {2}%". \
                format(device_name, expected_image, diff)
        else:
            # If expected image is not found actual will be saved as expected.
            print "Expected image not found. Actual image will be saved as expected: " + expected_image_original_path
            time.sleep(timeout)
            Device.get_screen(device_id, expected_image_original_path)
            assert False, "Expected image not found!"
Пример #43
0
    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'