Ejemplo n.º 1
0
 def setUp(self):
     TestContext.TEST_NAME = self._testMethodName
     Log.test_start(test_name=TestContext.TEST_NAME)
     Tns.kill()
     Gradle.kill()
     TnsTest.__clean_backup_folder_and_dictionary()
Ejemplo n.º 2
0
def open_log(fname, was_appended=False):
    log = db.services.io.open_log_from_xml(fname, was_appended)
    Log.convert(log)
    return log
Ejemplo n.º 3
0
def sync_hello_world_ng(app_name,
                        platform,
                        device,
                        bundle=True,
                        uglify=False,
                        aot=False,
                        hmr=True,
                        instrumented=True):
    result = run_hello_world_ng(app_name=app_name,
                                platform=platform,
                                device=device,
                                uglify=uglify,
                                aot=aot,
                                hmr=hmr)

    # Verify that application is not restarted on file changes when hmr=true
    if hmr and Settings.HOST_OS != OSType.WINDOWS:
        not_existing_string_list = ['Restarting application']
    else:
        not_existing_string_list = None

    # Apply changes
    Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.TS)
    device.wait_for_text(text=Changes.NGHelloWorld.TS.new_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='item.service.ts',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)

    Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.HTML)
    if platform == Platform.IOS:
        for number in ["10", "11"]:
            device.wait_for_text(text=number)
    else:
        for number in ["8", "9"]:
            device.wait_for_text(text=number)
    assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='items.component.html',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   aot=aot,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)

    Sync.replace(app_name=app_name, change_set=Changes.NGHelloWorld.CSS)
    if platform == Platform.IOS:
        for number in ["10", "1"]:
            device.wait_for_text(text=number)
    else:
        for number in ["8", "9"]:
            device.wait_for_text(text=number)
    assert not device.is_text_visible(text=Changes.NGHelloWorld.TS.new_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='app.css',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)
    assert Wait.until(lambda: device.get_pixels_by_color(color=Changes.NGHelloWorld.CSS.new_color) > 100), \
        'CSS on root level not applied!'
    Log.info('CSS on root level applied successfully!')

    # Revert changes
    Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.HTML)
    device.wait_for_text(text=Changes.NGHelloWorld.TS.new_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='items.component.html',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   aot=aot,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)

    Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.TS)
    device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='item.service.ts',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)

    Sync.revert(app_name=app_name, change_set=Changes.NGHelloWorld.CSS)
    device.wait_for_text(text=Changes.NGHelloWorld.TS.old_text)
    strings = TnsLogs.run_messages(app_name=app_name,
                                   platform=platform,
                                   run_type=RunType.INCREMENTAL,
                                   bundle=bundle,
                                   file_name='app.css',
                                   hmr=hmr,
                                   instrumented=instrumented,
                                   app_type=AppType.NG,
                                   device=device)
    TnsLogs.wait_for_log(log_file=result.log_file,
                         string_list=strings,
                         timeout=180,
                         not_existing_string_list=not_existing_string_list)
    assert Wait.until(lambda: device.get_pixels_by_color(color=Changes.NGHelloWorld.CSS.new_color) < 100), \
        'CSS on root level not applied!'
    Log.info('CSS on root level applied successfully!')

    # Assert final and initial states are same
    initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name,
                                 'initial_state.png')
    device.screen_match(expected_image=initial_state,
                        tolerance=1.0,
                        timeout=30)
Ejemplo n.º 4
0
 def restart():
     Log.info("Restart adb.")
     Adb.run_adb_command('kill-server')
     Process.kill(proc_name='adb')
     Adb.run_adb_command('start-server')
Ejemplo n.º 5
0
 def erase(simulator_info):
     result = Simctl.run_simctl_command('erase {0}'.format(
         simulator_info.id))
     assert result.exit_code == 0, 'Failed to erase {0}'.format(
         simulator_info.name)
     Log.info('Erase {0}.'.format(simulator_info.name))
Ejemplo n.º 6
0
    def run_messages(app_name, platform, run_type=RunType.FULL, bundle=True, hmr=True, uglify=False, app_type=None,
                     file_name=None, instrumented=False, plugins=None, aot=False, device=None, just_launch=False,
                     transfer_all=False):
        """
        Get log messages that should be present when running a project.
        :param app_name: Name of the app (for example TestApp).
        :param platform: Platform.ANDROID or Platform.IOS.
        :param run_type: RunType enum value (None when it can not be defined strictly).
        :param bundle: True if `--bundle is specified.`
        :param hmr: True if `--hmr is specified.`
        :param uglify: True if `--env.uglify is specified.`
        :param app_type: AppType enum value.
        :param file_name: Name of changed file.
        :param instrumented: If true it will return logs we place inside app (see files in assets/logs).
        :param plugins: List of plugins.
        :param aot: True if `--env.aot is specified.`
        :param device: Device object.
        :param transfer_all: different strings for transfer of files depends on their count - 10 or more
        :return: Array of strings.
        """
        if plugins is None:
            plugins = []
        logs = []
        if hmr:
            bundle = True

        # Generate prepare messages
        if run_type in [RunType.FIRST_TIME, RunType.FULL]:
            logs.extend(TnsLogs.prepare_messages(platform=platform, plugins=plugins))

        # Generate build messages
        # TODO: Check if file is in app resources and require native build
        logs.extend(TnsLogs.build_messages(platform=platform, run_type=run_type))

        # App install messages
        if run_type in [RunType.FIRST_TIME, RunType.FULL]:
            logs.append('Installing on device')
            logs.append('Successfully installed')

        # File transfer messages
        logs.extend(TnsLogs.__file_changed_messages(run_type=run_type, file_name=file_name, platform=platform,
                                                    bundle=bundle, hmr=hmr, uglify=uglify, aot=aot, app_type=app_type,
                                                    transfer_all=transfer_all))

        if run_type == RunType.JUST_LAUNCH:
            logs.append('Preparing project...')
            logs.append('Webpack compilation complete.')
            logs.append('Project successfully prepared')
            logs.append('Restarting application on device')

        # App restart messages:
        if run_type in [RunType.UNKNOWN, RunType.JUST_LAUNCH]:
            Log.info('Can not define if app should be restarted or not.')
        else:
            if TnsLogs.__should_restart(run_type=run_type, bundle=bundle, hmr=hmr, file_name=file_name):
                logs.extend(TnsLogs.__app_restart_messages(app_name=app_name, platform=platform,
                                                           instrumented=instrumented, app_type=app_type,
                                                           device=device, just_launch=just_launch))
            else:
                logs.extend(TnsLogs.__app_refresh_messages(instrumented=instrumented, app_type=app_type,
                                                           file_name=file_name, hmr=hmr))

        # Add message for successful sync
        app_id = TnsPaths.get_bundle_id(app_name)
        logs.append('Successfully synced application {0} on device'.format(app_id))

        if app_type == AppType.NG:
            logs.append('Angular is running in the development mode. Call enableProdMode() to enable '
                        'the production mode.')
            # If you are in NG with hmr project changes of app.css should not cause angular reload
            if file_name is not None:
                if hmr and 'app.css' in file_name:
                    logs.remove('Angular is running in the development mode. Call enableProdMode() to enable '
                                'the production mode.')

        # Return logs
        return logs
import sys

import nose

import run_common
from core.log.log import Log

if __name__ == '__main__':
    run_common.prepare(clone_templates=False, install_ng_cli=True)
    Log.info("Running tests...")
    arguments = [
        'nosetests', '-v', '-s', '--nologcapture', '--logging-filter=nose',
        '--with-xunit', '--with-flaky'
    ]
    for i in sys.argv:
        arguments.append(str(i))
    nose.run(argv=arguments)
 def replace(path,
             old_string,
             new_string,
             fail_safe=False,
             backup_files=False):
     if backup_files:
         File.__back_up_files(path)
     content = File.read(path=path)
     old_text_exists = old_string in content
     if not fail_safe:
         assert old_text_exists, 'Can not find "{0}" in {1}'.format(
             old_string, path)
     if old_text_exists:
         new_content = content.replace(old_string, new_string)
         File.write(path=path, text=new_content)
         Log.info("")
         Log.info("##### REPLACE FILE CONTENT #####")
         Log.info("File: {0}".format(path))
         Log.info("Old String: {0}".format(old_string))
         Log.info("New String: {0}".format(new_string))
         Log.info("")
     else:
         Log.debug('Skip replace. Text "{0}" do not exists in {1}.'.format(
             old_string, path))
 def copy(source, target, backup_files=False):
     if backup_files:
         File.__back_up_files(target, source)
     shutil.copy(source, target)
     Log.info('Copy {0} to {1}'.format(os.path.abspath(source),
                                       os.path.abspath(target)))
Ejemplo n.º 10
0
 def destroy(self):
     Log.log("Destroy called")
Ejemplo n.º 11
0
 def __init__(self, parent_frame, ws_controller):
     Log.log("WsStart init called")
     WorkspaceBase.__init__(self, parent_frame, ws_controller)
     ws_header = ttk.Label(self, text = "This is the start workspace")
     ws_header.grid(row = 0, column = 0)
Ejemplo n.º 12
0
 def deactivate(self):
     Log.log("Deactivate called")
Ejemplo n.º 13
0
 def pause(self):
     Log.log("Pause called")
Ejemplo n.º 14
0
 def activate(self):
     Log.log("Activate called")
Ejemplo n.º 15
0
 def cache_clean():
     Log.info("Clean gradle cache.")
     if Settings.HOST_OS is OSType.WINDOWS:
         run(cmd="rmdir /s /q {USERPROFILE}\\.gradle".format(**os.environ))
     else:
         run(cmd="rm -rf ~/.gradle")
 def unpack_tar(file_path, dest_dir):
     try:
         tar_file = tarfile.open(file_path, 'r:gz')
         tar_file.extractall(dest_dir)
     except Exception:
         Log.debug('Failed to unpack .tar file {0}'.format(file_path))
Ejemplo n.º 17
0
    def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=60, check_interval=3):
        """
        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.
        """
        end_time = time.time() + timeout
        all_items_found = False
        not_found_list = []
        log = ""
        verified_flag = '[VERIFIED]'
        while time.time() < end_time:
            not_found_list = []
            log = File.read(log_file)
            # Extract the part of the log that hasn't been previously verified
            if verified_flag in log:
                log = File.extract_part_of_text(log, verified_flag)
            for item in string_list:
                if item in log:
                    Log.info("'{0}' found.".format(item))
                else:
                    not_found_list.append(item)
            string_list = not_found_list
            if not not_found_list:
                all_items_found = True
                Log.info("All items found")
                break
            else:
                Log.debug("'{0}' NOT found. Wait...".format(not_found_list))
                time.sleep(check_interval)
            if 'BUILD FAILED' in log:
                Log.error('BUILD FAILED. No need to wait more time!')
                break
            if 'Unable to sync files' in log:
                Log.error('Sync process failed. No need to wait more time!')
                break
            if 'errors were thrown' in log:
                Log.error('Multiple errors were thrown. No need to wait more time!')
                break

        # Mark that part of the log as verified by appending a flag at the end.
        # The second time we read the file we will verify only the text after that flag
        if Settings.HOST_OS != OSType.WINDOWS:
            File.append(log_file, verified_flag)

        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:
            Log.info("NOT FOUND: {0}".format(not_found_list))
            Log.info('##### ACTUAL LOG #####\n')
            Log.info(log)
            Log.info('######################\n')
            assert False, "Output does not contain {0}".format(not_found_list)
class PlaygroundMarketSamples(TnsRunTest):
    chrome = None
    app_name = Settings.AppName.DEFAULT
    is_ios_fail = None
    test_data = Market.get_data_market()
    range = sys.argv[-1]
    if "samples=" in range:
        range_array = str(range).replace("samples=", "").split("-")
        start = int(range_array[0])
        end = int(range_array[1])
        test_data = test_data[start:end]
        Log.info(
            "====== Running test for samples in the range {}-{} ======".format(
                start, end))
    # test_data = [x for x in test_data1 if u'Getting_a_User\'s_Current_Location' in x]
    # print test_data
    # test_data = [
    #     ['getting_started_ng', 'https://play.nativescript.org/?template=play-ng&id=2OkUP6', 'Play', 'ng'],
    #     ['getting_started_ng', 'https://play.nativescript.org/?template=play-tsc&id=TcHeUQ&v=10', 'Play', 'ng']
    # ]

    @classmethod
    def setUpClass(cls):
        Market.remove_results_file()
        Settings.Emulators.DEFAULT = Settings.Emulators.EMU_API_28
        TnsRunTest.setUpClass()
        Preview.install_preview_app(cls.emu, Platform.ANDROID)
        if Settings.HOST_OS is OSType.OSX:
            Preview.install_preview_app(cls.sim, Platform.IOS)
            Preview.install_playground_app(cls.sim, Platform.IOS)

    def setUp(self):
        TnsRunTest.setUp(self)
        self.is_ios_fail = os.environ.get('is_ios_fail') == 'True'

    def tearDown(self):
        self.chrome.kill()
        TnsRunTest.tearDown(self)

    @classmethod
    def tearDownClass(cls):
        TnsRunTest.tearDownClass()

    @parameterized.expand(test_data)
    def test(self, name, url, text, flavor):
        Log.info(text)
        retries = 1
        original_name = name.replace("_", " ").encode("utf8")
        data = {
            "name": original_name,
            "ios": "False",
            "android": "False",
            "flavor": str(flavor),
            "timeout": "False",
            "slow": "False"
        }
        is_android_fail = True
        is_ios_fail = True

        while retries >= 0:
            # =====================Android RUN========================
            if is_android_fail:
                self.chrome = Chrome()
                link, is_slow = PlaygroundMarketSamples.get_link(
                    self.chrome, url)
                if link == "":
                    Log.info('No Playground URL found in Android stage !!!')
                    data["timeout"] = "True"
                    data["slow"] = "True"
                    retries -= 1
                    continue

                Log.info('Testing Android !!!')
                image_name = '{0}_{1}.png'.format(name.encode("utf8"),
                                                  str(Platform.ANDROID))
                Preview.run_url(url=link, device=self.emu)
                Log.info(' Waiting Android app to load...')
                time.sleep(10)
                PlaygroundMarketSamples.verify_device_is_connected(
                    self.chrome, "Android SDK built")
                emulator_result = PlaygroundMarketSamples.get_error(
                    self.chrome)
                is_android_fail = emulator_result > 0
                android = str(not is_android_fail)
                data["android"] = android
                data["slow"] = str(is_slow)

                if is_android_fail:
                    self.emu.get_screen(
                        os.path.join(Settings.TEST_OUT_IMAGES, image_name))
                self.chrome.kill()

            # =====================iOS RUN========================
            if Settings.HOST_OS == OSType.OSX and is_ios_fail:
                image_name = '{0}_{1}.png'.format(name.encode("utf8"),
                                                  str(Platform.IOS))

                if self.is_ios_fail:
                    Log.info(' Installing Preview app on iOS ...')
                    Preview.install_preview_app_no_unpack(
                        self.sim, Platform.IOS)

                self.chrome = Chrome()
                link, is_slow = PlaygroundMarketSamples.get_link(
                    self.chrome, url)
                if link == "":
                    Log.info('No Playground URL found in iOS stage !!!')
                    data["timeout"] = "True"
                    data["slow"] = "True"
                    retries -= 1
                    continue

                Log.info('Testing iOS !!!')
                Preview.run_url(url=link, device=self.sim)
                if "test_0_" in self._testMethodName or "test_000_" in self._testMethodName:
                    time.sleep(10)
                    PlaygroundMarketSamples.close_popup(self.sim)
                Log.info(' Waiting iOS app to load...')
                Preview.dismiss_simulator_alert()
                time.sleep(10)
                PlaygroundMarketSamples.verify_device_is_connected(
                    self.chrome, self.sim.name)
                Preview.dismiss_simulator_alert()

                if "test_0_" in self._testMethodName or "test_000_" in self._testMethodName:
                    PlaygroundMarketSamples.close_popup(self.sim)

                PlaygroundMarketSamples.close_permissions_windows_ios(
                    name, self.sim)
                simulator_result = PlaygroundMarketSamples.get_error(
                    self.chrome)
                is_app_active = Preview.is_running_on_ios(
                    self.sim, Settings.Packages.PREVIEW_APP_ID)
                self.is_ios_fail = simulator_result > 0 or not is_app_active
                is_ios_fail = self.is_ios_fail
                os.environ["is_ios_fail"] = str(self.is_ios_fail)

                if self.is_ios_fail:
                    self.sim.get_screen(
                        os.path.join(Settings.TEST_OUT_IMAGES, image_name))

                ios = str(not self.is_ios_fail)
                data["ios"] = ios
                data["slow"] = str(is_slow)

            if self.is_ios_fail is False and is_android_fail is False:
                break
            retries -= 1
            self.tearDown()

        Market.preserve_data(data)

    # noinspection PyBroadException
    @staticmethod
    def get_link(chrome, url):
        url = '{0}&debug=true'.format(url)
        chrome.open(url)
        link = ""
        timeout = 60  # In seconds
        slow = False
        end_time = time.time() + timeout
        while end_time > time.time():
            try:
                Log.info('Searching for QR url ...')
                link = chrome.driver.find_element_by_xpath(
                    "//span[contains(.,'nsplay://boot')]").text
            except NoSuchElementException:
                Log.info('No Playground URL element found ...')
            if "nsplay" in link:
                Log.info('Url link found ...')
                if (end_time - time.time()) < 35:
                    slow = True
                break
        return link, slow

    @staticmethod
    def get_error(chrome, previous_errors=0):
        PlaygroundMarketSamples.open_device_logs(chrome)
        exceptions = 0
        timeout = 10  # In seconds
        end_time = time.time() + timeout
        while end_time > time.time():
            Log.info(' Searching for exception ...')
            elements = chrome.driver.find_elements_by_xpath(
                "//span[contains(.,'Exception')]")
            exceptions = len(elements) - previous_errors
            if exceptions > 0:
                error = elements[previous_errors].text
                print error
                break
        return exceptions

    @staticmethod
    def wait_for_text(emu, text, timeout=20, retry_delay=1):
        t_end = time.time() + timeout
        found = False
        error_msg = '{0} NOT found on {1}.'.format(text, emu.name)
        found_msg = '{0} found on {1}.'.format(text, emu.name)
        while time.time() < t_end:
            if emu.is_text_visible(text=text):
                found = True
                Log.info(found_msg)
                break
            else:
                Log.info(error_msg + ' Waiting ...')
                time.sleep(retry_delay)
        if not found:
            text = emu.get_text()
            Log.info('Current text: {0}{1}'.format(os.linesep, text))
        return found

    @staticmethod
    def close_permissions_windows_ios(name, sim):
        samples = ['Instagram_Clone_with_Image_Filters']
        iterations = 1
        if name in samples:
            is_ok_button_visible = PlaygroundMarketSamples.wait_for_text(
                sim, "OK", 10)
            while is_ok_button_visible:
                iterations = iterations + 1
                Preview.dismiss_simulator_alert()
                time.sleep(1)
                is_ok_button_visible = PlaygroundMarketSamples.wait_for_text(
                    sim, "OK", 5)
                if iterations == 5:
                    break

    @staticmethod
    def verify_device_is_connected(chrome, device, timeout=15):
        PlaygroundMarketSamples.close_cookie_alert(chrome)
        Log.info("Check device in Playground")
        t_end = time.time() + timeout
        while time.time() < t_end:
            try:
                chrome.driver.find_elements_by_xpath(
                    "//span[contains(.,'Devices')]")[0].click()
                Log.info('Devices button clicked.')
                break
            except ElementClickInterceptedException:
                Log.info('Unable to click Devices button!')
                time.sleep(3)
        devices = chrome.driver.find_elements_by_class_name('device-name')
        try:
            if devices:
                device_name = devices[0].text
                if device not in device_name:
                    Log.info("Searched device not found !!! Actual: " +
                             str(device_name) + " Expected: " + device)
            else:
                Log.info("No device has been found to be attached !!!")
        except StaleElementReferenceException:
            Log.info('Device name element has been removed from the DOM!!!')

    @staticmethod
    def open_device_logs(chrome, timeout=5):
        Log.info("Open device Logs")
        t_end = time.time() + timeout
        while time.time() < t_end:
            try:
                chrome.driver.find_elements_by_xpath(
                    "//span[contains(.,'Device Logs')]")[0].click()
                Log.info('Device Log opened.')
                break
            except ElementClickInterceptedException:
                Log.info('Unable to click Device Log!')
                time.sleep(2)

    @staticmethod
    def close_popup(device, timeout=10, button_text="Open"):
        if PlaygroundMarketSamples.wait_for_text(device, button_text, timeout):
            device.click(text=button_text)
            # Preview.dismiss_simulator_alert()

    @staticmethod
    def close_cookie_alert(chrome):
        Log.info("Close cookie alert.")
        accept_cookies = chrome.driver.find_elements_by_xpath(
            "//button[contains(.,'Accept Cookies')]")
        if accept_cookies:
            accept_cookies[0].click()
Ejemplo n.º 19
0
def run(cmd,
        cwd=Settings.TEST_RUN_HOME,
        wait=True,
        timeout=600,
        fail_safe=False,
        register=True,
        log_level=logging.DEBUG):
    # Init result values
    time_string = datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f')
    log_file = os.path.join(Settings.TEST_OUT_LOGS,
                            'command_{0}.txt'.format(time_string))
    complete = False
    duration = None
    output = ''

    # Ensure logs folder exists
    dir_path = os.path.dirname(os.path.realpath(log_file))
    Folder.create(dir_path)

    # Command settings
    if not wait:
        # Redirect output to file
        File.write(path=log_file, text=cmd + os.linesep + '====>' + os.linesep)
        cmd = cmd + ' >> ' + log_file + ' 2>&1 &'

    # Log command that will be executed:
    Log.log(level=log_level, msg='Execute command: ' + cmd)
    Log.log(level=logging.DEBUG, msg='CWD: ' + cwd)

    # Execute command:
    if wait:
        start = time.time()
        with open(log_file, mode='w') as log:
            if Settings.HOST_OS == OSType.WINDOWS:
                process = subprocess.Popen(
                    cmd, cwd=cwd, shell=True, stdout=log, stderr=log)
            else:
                process = subprocess.Popen(
                    cmd,
                    cwd=cwd,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stderr=log)

        # Wait until command complete
        try:
            process.wait(timeout=timeout)
            complete = True
            out, err = process.communicate()
            if out is not None:
                if Settings.PYTHON_VERSION < 3:
                    output = str(out.decode('utf8').encode('utf8')).strip()
                else:
                    output = out.decode("utf-8").strip()
        except subprocess.TimeoutExpired:
            process.kill()
            if fail_safe:
                Log.error('Command "{0}" timeout after {1} seconds.'.format(
                    cmd, timeout))
            else:
                raise

        # Append stderr to output
        stderr = File.read(path=log_file)
        if stderr:
            output = output + os.linesep + File.read(path=log_file)

        # noinspection PyBroadException
        try:
            File.delete(path=log_file)
        except Exception:
            Log.debug('Failed to clean log file: {0}'.format(log_file))
        log_file = None
        end = time.time()
        duration = end - start
    else:
        process = psutil.Popen(
            cmd,
            cwd=cwd,
            shell=True,
            stdin=None,
            stdout=None,
            stderr=None,
            close_fds=True)

    # Get result
    pid = process.pid
    exit_code = process.returncode

    # Log output of the process
    if wait:
        Log.log(
            level=log_level, msg='OUTPUT: ' + os.linesep + output + os.linesep)
    else:
        Log.log(
            level=log_level, msg='OUTPUT REDIRECTED: ' + log_file + os.linesep)

    # Construct result
    result = ProcessInfo(
        cmd=cmd,
        pid=pid,
        exit_code=exit_code,
        output=output,
        log_file=log_file,
        complete=complete,
        duration=duration)

    # Register in TestContext
    if psutil.pid_exists(result.pid) and register:
        TestContext.STARTED_PROCESSES.append(result)

    # Return the result
    return result
 def verify_device_is_connected(chrome, device, timeout=15):
     PlaygroundMarketSamples.close_cookie_alert(chrome)
     Log.info("Check device in Playground")
     t_end = time.time() + timeout
     while time.time() < t_end:
         try:
             chrome.driver.find_elements_by_xpath(
                 "//span[contains(.,'Devices')]")[0].click()
             Log.info('Devices button clicked.')
             break
         except ElementClickInterceptedException:
             Log.info('Unable to click Devices button!')
             time.sleep(3)
     devices = chrome.driver.find_elements_by_class_name('device-name')
     try:
         if devices:
             device_name = devices[0].text
             if device not in device_name:
                 Log.info("Searched device not found !!! Actual: " +
                          str(device_name) + " Expected: " + device)
         else:
             Log.info("No device has been found to be attached !!!")
     except StaleElementReferenceException:
         Log.info('Device name element has been removed from the DOM!!!')
Ejemplo n.º 21
0
 def open_home(device_id):
     cmd = 'shell am start -a android.intent.action.MAIN -c android.intent.category.HOME'
     Adb.run_adb_command(command=cmd, device_id=device_id)
     Log.info('Open home screen of {0}.'.format(str(device_id)))
 def close_cookie_alert(chrome):
     Log.info("Close cookie alert.")
     accept_cookies = chrome.driver.find_elements_by_xpath(
         "//button[contains(.,'Accept Cookies')]")
     if accept_cookies:
         accept_cookies[0].click()
Ejemplo n.º 23
0
def __workflow(preview, app_name, platform, device, bundle=True, hmr=True):
    # Execute tns command
    if preview:
        result = __preview_vue(
            app_name=app_name,
            platform=platform,
            device=device,
            bundle=bundle,
            hmr=hmr)
    else:
        result = __run_vue(
            app_name=app_name, platform=platform, bundle=bundle, hmr=hmr)

    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.FULL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE)
        TnsLogs.wait_for_log(
            log_file=result.log_file, string_list=strings, timeout=240)

    # Verify it looks properly
    device.wait_for_text(
        text=Changes.BlankVue.VUE_SCRIPT.old_text, timeout=120)
    device.wait_for_text(
        text=Changes.BlankVue.VUE_TEMPLATE.old_text, timeout=120)
    initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name,
                                 'initial_state.png')
    device.get_screen(path=initial_state)

    # Edit script in .vue file
    Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_SCRIPT)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
    device.wait_for_text(text=Changes.BlankVue.VUE_SCRIPT.new_text)

    # Edit template in .vue file
    Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_TEMPLATE)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
    device.wait_for_text(text=Changes.BlankVue.VUE_TEMPLATE.new_text)

    # Edit styling in .vue file
    Sync.replace(app_name=app_name, change_set=Changes.BlankVue.VUE_STYLE)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
    style_applied = Wait.until(lambda: device.get_pixels_by_color(Colors.RED) >
                               100)
    assert style_applied, 'Failed to sync changes in style.'

    # Revert script in .vue file
    Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_SCRIPT)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
    device.wait_for_text(text=Changes.BlankVue.VUE_SCRIPT.old_text)

    # Revert template in .vue file
    Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_TEMPLATE)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)
    device.wait_for_text(text=Changes.BlankVue.VUE_TEMPLATE.old_text)

    # Revert styling in .vue file
    Sync.revert(app_name=app_name, change_set=Changes.BlankVue.VUE_STYLE)
    if preview:
        Log.info('Skip logs checks.')
    else:
        strings = TnsLogs.run_messages(
            app_name=app_name,
            platform=platform,
            run_type=RunType.INCREMENTAL,
            bundle=bundle,
            hmr=hmr,
            app_type=AppType.VUE,
            file_name='Home.vue')
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=strings)

    if hmr:
        Log.info(
            'Skip next steps because of https://github.com/nativescript-vue/nativescript-vue/issues/425'
        )
    else:
        style_applied = Wait.until(lambda: device.get_pixels_by_color(
            Colors.RED) == 0)
        assert style_applied, 'Failed to sync changes in style.'

    # Assert final and initial states are same
    device.screen_match(
        expected_image=initial_state, tolerance=1.0, timeout=30)
    def test(self, name, url, text, flavor):
        Log.info(text)
        retries = 1
        original_name = name.replace("_", " ").encode("utf8")
        data = {
            "name": original_name,
            "ios": "False",
            "android": "False",
            "flavor": str(flavor),
            "timeout": "False",
            "slow": "False"
        }
        is_android_fail = True
        is_ios_fail = True

        while retries >= 0:
            # =====================Android RUN========================
            if is_android_fail:
                self.chrome = Chrome()
                link, is_slow = PlaygroundMarketSamples.get_link(
                    self.chrome, url)
                if link == "":
                    Log.info('No Playground URL found in Android stage !!!')
                    data["timeout"] = "True"
                    data["slow"] = "True"
                    retries -= 1
                    continue

                Log.info('Testing Android !!!')
                image_name = '{0}_{1}.png'.format(name.encode("utf8"),
                                                  str(Platform.ANDROID))
                Preview.run_url(url=link, device=self.emu)
                Log.info(' Waiting Android app to load...')
                time.sleep(10)
                PlaygroundMarketSamples.verify_device_is_connected(
                    self.chrome, "Android SDK built")
                emulator_result = PlaygroundMarketSamples.get_error(
                    self.chrome)
                is_android_fail = emulator_result > 0
                android = str(not is_android_fail)
                data["android"] = android
                data["slow"] = str(is_slow)

                if is_android_fail:
                    self.emu.get_screen(
                        os.path.join(Settings.TEST_OUT_IMAGES, image_name))
                self.chrome.kill()

            # =====================iOS RUN========================
            if Settings.HOST_OS == OSType.OSX and is_ios_fail:
                image_name = '{0}_{1}.png'.format(name.encode("utf8"),
                                                  str(Platform.IOS))

                if self.is_ios_fail:
                    Log.info(' Installing Preview app on iOS ...')
                    Preview.install_preview_app_no_unpack(
                        self.sim, Platform.IOS)

                self.chrome = Chrome()
                link, is_slow = PlaygroundMarketSamples.get_link(
                    self.chrome, url)
                if link == "":
                    Log.info('No Playground URL found in iOS stage !!!')
                    data["timeout"] = "True"
                    data["slow"] = "True"
                    retries -= 1
                    continue

                Log.info('Testing iOS !!!')
                Preview.run_url(url=link, device=self.sim)
                if "test_0_" in self._testMethodName or "test_000_" in self._testMethodName:
                    time.sleep(10)
                    PlaygroundMarketSamples.close_popup(self.sim)
                Log.info(' Waiting iOS app to load...')
                Preview.dismiss_simulator_alert()
                time.sleep(10)
                PlaygroundMarketSamples.verify_device_is_connected(
                    self.chrome, self.sim.name)
                Preview.dismiss_simulator_alert()

                if "test_0_" in self._testMethodName or "test_000_" in self._testMethodName:
                    PlaygroundMarketSamples.close_popup(self.sim)

                PlaygroundMarketSamples.close_permissions_windows_ios(
                    name, self.sim)
                simulator_result = PlaygroundMarketSamples.get_error(
                    self.chrome)
                is_app_active = Preview.is_running_on_ios(
                    self.sim, Settings.Packages.PREVIEW_APP_ID)
                self.is_ios_fail = simulator_result > 0 or not is_app_active
                is_ios_fail = self.is_ios_fail
                os.environ["is_ios_fail"] = str(self.is_ios_fail)

                if self.is_ios_fail:
                    self.sim.get_screen(
                        os.path.join(Settings.TEST_OUT_IMAGES, image_name))

                ios = str(not self.is_ios_fail)
                data["ios"] = ios
                data["slow"] = str(is_slow)

            if self.is_ios_fail is False and is_android_fail is False:
                break
            retries -= 1
            self.tearDown()

        Market.preserve_data(data)
Ejemplo n.º 25
0
 def erase_all():
     result = Simctl.run_simctl_command('erase all')
     assert result.exit_code == 0, 'Failed to erase all iOS Simulators.'
     Log.info('Erase all iOS Simulators.')
Ejemplo n.º 26
0
    def exec_command(command,
                     cwd=Settings.TEST_RUN_HOME,
                     platform=Platform.NONE,
                     emulator=False,
                     path=None,
                     device=None,
                     release=False,
                     for_device=False,
                     provision=None,
                     bundle=True,
                     hmr=True,
                     aot=False,
                     uglify=False,
                     source_map=False,
                     snapshot=False,
                     log_trace=False,
                     just_launch=False,
                     sync_all_files=False,
                     clean=False,
                     aab=False,
                     options=None,
                     wait=True,
                     timeout=600):
        """
        Execute tns command.
        :param command: Tns command.
        :param cwd: Working directory.
        :param platform: Pass `platform <value>` to command.
        :param emulator: If true pass `--emulator` flag.
        :param path: Pass `--path <value>` to command.
        :param device: Pass `--device <value>` to command.
        :param release: If true pass `--release <all signing options>` to command.
        :param for_device: If true pass `--for-device` to command.
        :param provision: Pass `--provision <value>` to command.
        :param bundle: If true pass `--bundle` to command.
        :param hmr: If true pass `--hmr` to command.
        :param aot: If true pass `--env.aot` to command.
        :param uglify: If true pass `--env.uglify` to command.
        :param source_map: If true pass `--env.sourceMap` to command.
        :param snapshot: If true pass `--env.snapshot` to command.
        :param log_trace: If not None pass `--log <level>` to command.
        :param just_launch: If true pass `--justlaunch` to command.
        :param sync_all_files:  If true pass `--syncAllFiles` to command.
        :param clean:  If true pass `--clean` to command.
        :param options: Pass additional options as string.
        :param wait: If true it will wait until command is complete.
        :param timeout: Timeout for CLI command (respected only if wait=True).
        :return: ProcessInfo object.
        :rtype: core.utils.process_info.ProcessInfo
        """
        cmd = '{0} {1}'.format(Settings.Executables.TNS, command)
        if platform is not None:
            cmd = cmd + ' ' + str(platform)
        if path is not None:
            cmd = cmd + ' --path ' + path
        if emulator:
            cmd += ' --emulator'
        if device is not None:
            cmd = cmd + ' --device ' + device
        if release:
            cmd += ' --release'
            if platform is Platform.ANDROID:
                cmd += ' --key-store-path {0} --key-store-password {1} --key-store-alias {2} ' \
                       '--key-store-alias-password {3}'.format(Settings.Android.ANDROID_KEYSTORE_PATH,
                                                               Settings.Android.ANDROID_KEYSTORE_PASS,
                                                               Settings.Android.ANDROID_KEYSTORE_ALIAS,
                                                               Settings.Android.ANDROID_KEYSTORE_ALIAS_PASS)
        if provision is not None and platform != Platform.ANDROID and Settings.HOST_OS == OSType.OSX and not emulator:
            cmd = cmd + ' --provision ' + provision
        if for_device:
            cmd += ' --for-device'
        if not bundle:
            cmd += ' --no-bundle'
        if not hmr:
            cmd += ' --no-hmr'
        if aot:
            cmd += ' --env.aot'
        if uglify:
            cmd += ' --env.uglify'
        if snapshot:
            cmd += ' --env.snapshot'
        if source_map:
            cmd += ' --env.sourceMap'
        if just_launch:
            cmd += ' --justlaunch'
        if clean:
            cmd += ' --clean'
        if sync_all_files:
            cmd += ' --syncAllFiles'
        if aab:
            cmd += ' --aab'
        if log_trace:
            cmd += ' --log trace'
        if options:
            cmd += ' ' + options

        result = run(cmd=cmd,
                     cwd=cwd,
                     wait=wait,
                     log_level=logging.INFO,
                     timeout=timeout)

        # Retry in case of connectivity issues
        if result.output is not None and 'Bad Gateway' in result.output:
            Log.info(
                '"Bad Gateway" issue detected! Will retry the command ...')
            result = run(cmd=cmd,
                         cwd=cwd,
                         wait=wait,
                         log_level=logging.INFO,
                         timeout=timeout)

        return result
Ejemplo n.º 27
0
def merge_logs(new_log, log_fname):
    log = db.services.io.merge_logs(new_log, log_fname)
    Log.convert(log)
    return log
def sync_master_detail_ng(app_name,
                          platform,
                          device,
                          bundle=True,
                          hmr=True,
                          uglify=False,
                          aot=False):
    run_master_detail_ng(app_name=app_name,
                         platform=platform,
                         device=device,
                         bundle=bundle,
                         hmr=hmr,
                         uglify=uglify,
                         aot=aot)

    # Edit TS file and verify changes are applied
    Sync.replace(app_name=app_name, change_set=Changes.MasterDetailNG.TS)
    device.wait_for_text(text=Changes.MasterDetailNG.TS.new_text)

    # Edit HTML file and verify changes are applied
    Sync.replace(app_name=app_name, change_set=Changes.MasterDetailNG.HTML)
    device.wait_for_text(text=Changes.MasterDetailNG.HTML.new_text)
    device.wait_for_text(text=Changes.MasterDetailNG.TS.new_text)

    # Edit common SCSS on root level
    change = Changes.MasterDetailNG.SCSS_ROOT_COMMON
    Sync.replace(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) > 100), \
        'Common SCSS on root level not applied!'
    Log.info('Common SCSS on root level applied successfully!')

    # Edit platform specific SCSS on root level
    if platform == Platform.ANDROID:
        change = Changes.MasterDetailNG.SCSS_ROOT_ANDROID
    elif platform == Platform.IOS:
        change = Changes.MasterDetailNG.SCSS_ROOT_IOS
    else:
        raise ValueError('Invalid platform value!')
    Sync.replace(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) > 100), \
        'Platform specific SCSS on root level not applied!'
    Log.info('Platform specific SCSS on root level applied successfully!')

    # Edit nested common SCSS
    change = Changes.MasterDetailNG.SCSS_NESTED_COMMON
    Sync.replace(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) > 100), \
        'Common nested SCSS not applied!'
    Log.info('Common nested SCSS applied successfully!')

    # Edit nested platform specific SCSS
    if platform == Platform.ANDROID:
        change = Changes.MasterDetailNG.SCSS_NESTED_ANDROID
    elif platform == Platform.IOS:
        change = Changes.MasterDetailNG.SCSS_NESTED_IOS
    else:
        raise ValueError('Invalid platform value!')
    Sync.replace(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) > 100), \
        'Platform specific nested SCSS not applied!'
    Log.info('Platform specific nested SCSS applied successfully!')

    # Revert TS file and verify changes are applied
    Sync.revert(app_name=app_name, change_set=Changes.MasterDetailNG.TS)
    device.wait_for_text(text=Changes.MasterDetailNG.TS.old_text)
    device.wait_for_text(text=Changes.MasterDetailNG.HTML.new_text)

    # Revert HTML file and verify changes are applied
    Sync.revert(app_name=app_name, change_set=Changes.MasterDetailNG.HTML)
    device.wait_for_text(text=Changes.MasterDetailNG.HTML.old_text)
    device.wait_for_text(text=Changes.MasterDetailNG.TS.old_text)

    # Revert common SCSS on root level
    change = Changes.MasterDetailNG.SCSS_ROOT_COMMON
    Sync.revert(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) < 100), \
        'Common SCSS on root level not reverted!'
    Log.info('Common SCSS on root level reverted successfully!')

    # Revert platform specific SCSS on root level
    if platform == Platform.ANDROID:
        change = Changes.MasterDetailNG.SCSS_ROOT_ANDROID
    elif platform == Platform.IOS:
        change = Changes.MasterDetailNG.SCSS_ROOT_IOS
    else:
        raise ValueError('Invalid platform value!')
    Sync.revert(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) < 100), \
        'Platform specific SCSS on root level not reverted!'
    Log.info('Platform specific SCSS on root level reverted successfully!')

    # Revert nested common SCSS
    change = Changes.MasterDetailNG.SCSS_NESTED_COMMON
    Sync.revert(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) < 100), \
        'Common SCSS on root level not applied!'
    Log.info('Platform specific SCSS on root level reverted successfully!')

    # Revert nested platform specific SCSS
    if platform == Platform.ANDROID:
        change = Changes.MasterDetailNG.SCSS_NESTED_ANDROID
    elif platform == Platform.IOS:
        change = Changes.MasterDetailNG.SCSS_NESTED_IOS
    else:
        raise ValueError('Invalid platform value!')
    Sync.revert(app_name=app_name, change_set=change)
    assert Wait.until(lambda: device.get_pixels_by_color(color=change.new_color) < 100), \
        'Platform specific SCSS on root level not applied!'

    # Assert final and initial states are same
    initial_state = os.path.join(Settings.TEST_OUT_IMAGES, device.name,
                                 'initial_state.png')
    device.screen_match(expected_image=initial_state,
                        tolerance=1.0,
                        timeout=30)