Example #1
0
    def revert_changes(app_name, log, platform):
        # Clean old logs
        if CURRENT_OS is not OSType.WINDOWS:
            File.write(file_path=log, text="")

        # Revert XML changes
        ReplaceHelper.rollback(app_name, HelpersHMR.xml_change, sleep=10)
        strings = ['Refreshing application on device', './main-page.xml',
                   'HMR: Checking for updates to the bundle with hmr hash']
        Tns.wait_for_log(log_file=log, string_list=strings)
        if platform == Platform.ANDROID:
            text_changed = Device.wait_for_text(device_id=EMULATOR_ID, text='TAP')
            assert text_changed, 'Changes in XML file not applied (UI is not refreshed).'

        # Revert JS changes
        ReplaceHelper.rollback(app_name, HelpersHMR.js_change, sleep=10)
        strings = ['Refreshing application on device', 'HMR: The following modules were updated:', './main-view-model.js', './main-page.js',
                   'Successfully transferred bundle.', 'HMR: Successfully applied update with hmr hash ']
        Tns.wait_for_log(log_file=log, string_list=strings)
        if platform == Platform.ANDROID:
            text_changed = Device.wait_for_text(device_id=EMULATOR_ID, text='42 taps left', timeout=20)
            assert text_changed, 'HMR: The following modules were updated:'

        # Revert CSS changes
        ReplaceHelper.rollback(app_name, HelpersHMR.css_change, sleep=10)
        Tns.wait_for_log(log_file=log, string_list=['app.css'], clean_log=False)

        # Verify application looks correct
        Tns.wait_for_log(log_file=log, string_list=HelpersHMR.wp_sync, not_existing_string_list=HelpersHMR.wp_errors,
                         timeout=60)
        if platform == Platform.ANDROID:
            Helpers.android_screen_match(image=HelpersHMR.image_original, timeout=120)
    def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=45, check_interval=3,
                     clean_log=True):
        """
        Wait until log file contains list of string.
        :param log_file: Path to log file.
        :param string_list: List of strings.
        :param not_existing_string_list: List of string that should not be in logs.
        :param timeout: Timeout.
        :param check_interval: Check interval.
        :param clean_log: Specify if content of log file should be delete after check.
        """
        t_end = time.time() + timeout
        all_items_found = False
        not_found_list = []
        log = ""
        while time.time() < t_end:
            not_found_list = []
            log = File.read(log_file)
            log = str(log.decode('utf8').encode('utf8')).strip()
            for item in string_list:
                if item in log:
                    print "'{0}' found.".format(item)
                else:
                    not_found_list.append(item)
            if not_found_list == []:
                all_items_found = True
                print "Log contains: {0}".format(string_list)
                break
            else:
                print "'{0}' NOT found. Wait...".format(not_found_list)
                time.sleep(check_interval)
            if 'BUILD FAILED' in log:
                print 'BUILD FAILED. No need to wait more time!'
                break
            if 'Unable to sync files' in log:
                print 'Sync process failed. No need to wait more time!'
                break
            if '????????????????????????????' in log:
                print 'Log seems to be corrupted. No need to wait more time!'
                break
            if 'errors were thrown' in log:
                print 'Multiple errors were thrown. No need to wait more time!'
                break

        if clean_log and (CURRENT_OS is not OSType.WINDOWS) and all_items_found:
            File.write(file_path=log_file, text="")

        if all_items_found:
            if not_existing_string_list is None:
                pass
            else:
                for item in not_existing_string_list:
                    assert item not in log, "{0} found! It should not be in logs.\nLog:\n{1}".format(item, log)
        else:
            print "##### OUTPUT BEGIN #####\n"
            print log
            print "##### OUTPUT END #####\n"
            print ""
            assert False, "Output does not contain {0}".format(not_found_list)
    def setUpClass(cls):
        BaseClass.setUpClass(cls.__name__)

        Folder.cleanup(cls.app_no_platform)

        File.remove(cls.debug_apk)
        File.remove(cls.release_apk)
        Folder.cleanup('temp')

        Tns.create_app(cls.app_name)
        Tns.platform_add_android(attributes={"--path": cls.app_name, "--frameworkPath": ANDROID_PACKAGE})

        # Add release and debug configs
        debug = os.path.join(cls.app_name, 'app', 'config.debug.json')
        release = os.path.join(cls.app_name, 'app', 'config.release.json')
        File.write(file_path=debug, text='{"config":"debug"}')
        File.write(file_path=release, text='{"config":"release"}')
Example #4
0
    def setUpClass(cls):
        BaseClass.setUpClass(cls.__name__)

        Folder.cleanup(cls.app_no_platform)

        File.remove(cls.debug_apk)
        File.remove(cls.release_apk)
        Folder.cleanup('temp')

        Tns.create_app(cls.app_name)
        Tns.platform_add_android(attributes={
            "--path": cls.app_name,
            "--frameworkPath": ANDROID_PACKAGE
        })

        # Add release and debug configs
        debug = os.path.join(cls.app_name, 'app', 'config.debug.json')
        release = os.path.join(cls.app_name, 'app', 'config.release.json')
        File.write(file_path=debug, text='{"config":"debug"}')
        File.write(file_path=release, text='{"config":"release"}')
Example #5
0
    def wait_for_log(log_file,
                     string_list,
                     not_existing_string_list=None,
                     timeout=45,
                     check_interval=3,
                     clean_log=True):
        """
        Wait until log file contains list of string.
        :param log_file: Path to log file.
        :param string_list: List of strings.
        :param not_existing_string_list: List of string that should not be in logs.
        :param timeout: Timeout.
        :param check_interval: Check interval.
        :param clean_log: Specify if content of log file should be delete after check.
        """
        t_end = time.time() + timeout
        all_items_found = False
        not_found_list = []
        log = ""
        while time.time() < t_end:
            not_found_list = []
            log = File.read(log_file)
            log = str(log.decode('utf8').encode('utf8')).strip()
            for item in string_list:
                if item in log:
                    print "'{0}' found.".format(item)
                else:
                    not_found_list.append(item)
            if not_found_list == []:
                all_items_found = True
                print "Log contains: {0}".format(string_list)
                break
            else:
                print "'{0}' NOT found. Wait...".format(not_found_list)
                time.sleep(check_interval)
            if 'BUILD FAILED' in log:
                print 'BUILD FAILED. No need to wait more time!'
                break
            if 'Unable to sync files' in log:
                print 'Sync process failed. No need to wait more time!'
                break
            if '????????????????????????????' in log:
                print 'Log seems to be corrupted. No need to wait more time!'
                break
            if 'errors were thrown' in log:
                print 'Multiple errors were thrown. No need to wait more time!'
                break

        if clean_log and (CURRENT_OS
                          is not OSType.WINDOWS) and all_items_found:
            File.write(file_path=log_file, text="")

        if all_items_found:
            if not_existing_string_list is None:
                pass
            else:
                for item in not_existing_string_list:
                    assert item not in log, "{0} found! It should not be in logs.\nLog:\n{1}".format(
                        item, log)
        else:
            print "##### OUTPUT BEGIN #####\n"
            print log
            print "##### OUTPUT END #####\n"
            print ""
            assert False, "Output does not contain {0}".format(not_found_list)
    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!"
Example #7
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!"