Exemplo n.º 1
0
 def setUp(self):
     TnsRunTest.setUp(self)
     Sync.revert(app_name=self.app_name,
                 change_set=self.ts_change,
                 fail_safe=True)
     Sync.revert(app_name=self.app_name,
                 change_set=self.xml_change,
                 fail_safe=True)
     self.chrome = Chrome()
Exemplo n.º 2
0
class ChromeTests(unittest.TestCase):

    def setUp(self):
        self.chrome = Chrome()

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

    def test_01_smoke(self):
        self.chrome.open(url='https://www.google.com/ncr')
        search_box = self.chrome.driver.find_element_by_name('q')
        search_box.send_keys('NativeScript')
        search_box.send_keys(Keys.ENTER)
        web_site_link = self.chrome.driver.find_element_by_partial_link_text('nativescript.org')
        assert web_site_link is not None
Exemplo n.º 3
0
class DebugNGTests(TnsRunTest):
    ts_change = Changes.NGHelloWorld.TS
    xml_change = Changes.NGHelloWorld.XML_ACTION_BAR
    app_name = Settings.AppName.DEFAULT
    chrome = None
    dev_tools = None

    @classmethod
    def setUpClass(cls):
        TnsRunTest.setUpClass()
        Tns.create(app_name=cls.app_name,
                   template=Template.HELLO_WORLD_NG.local_package,
                   update=True)
        Tns.platform_add_android(
            app_name=cls.app_name,
            framework_path=Settings.Android.FRAMEWORK_PATH)
        if Settings.HOST_OS == OSType.OSX:
            Tns.platform_add_ios(app_name=cls.app_name,
                                 framework_path=Settings.IOS.FRAMEWORK_PATH)

    def setUp(self):
        TnsRunTest.setUp(self)
        Sync.revert(app_name=self.app_name,
                    change_set=self.ts_change,
                    fail_safe=True)
        Sync.revert(app_name=self.app_name,
                    change_set=self.xml_change,
                    fail_safe=True)
        self.chrome = Chrome()

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

    def test_001_debug_android_elements(self):
        self.__debug_elements(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_001_debug_ios_elements(self):
        self.__debug_elements(platform=Platform.IOS, device=self.sim)

    def test_020_debug_android_sources(self):
        self.__debug_sources(platform=Platform.ANDROID, device=self.emu)

    @unittest.skip(
        'Do not work because we fail to click on "Ter Stegen". Wait until click is implemented via Appium.'
    )
    def test_020_debug_ios_sources(self):
        self.__debug_sources(platform=Platform.IOS, device=self.sim)

    def __debug_elements(self, platform, device):
        # Run `tns debug` wait until app is visible on device
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text=self.ts_change.old_text, timeout=240)

        # Start debug session and verify elements tab
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.ELEMENTS)
        assert self.dev_tools.wait_element_by_text(
            text=self.xml_change.old_text
        ) is not None, 'Elements tab is empty.'

        # Sync changes and verify elements tab is updated
        Sync.replace(app_name=self.app_name, change_set=self.xml_change)
        device.wait_for_text(text=self.xml_change.new_text)

        # Elements tab do not auto-update for NG apps and need to be refreshed manually
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.ELEMENTS)
        assert self.dev_tools.wait_element_by_text(
            text=self.xml_change.new_text
        ) is not None, 'Elements tab not updated.'

        # Update label in CDT and verify it is updated on device
        self.dev_tools.edit_text(old_text=self.xml_change.new_text,
                                 new_text=self.xml_change.old_text)
        element = self.dev_tools.wait_element_by_text(
            text=self.xml_change.old_text)
        assert element is not None, 'Failed to change text in elements tab.'
        device.wait_for_text(text=self.xml_change.old_text)

        # Expand items
        self.dev_tools.doubleclick_line(text='ProxyViewContainer')
        self.dev_tools.doubleclick_line(text='GridLayout')
        time.sleep(3)
        self.dev_tools.doubleclick_line(text='ListView')
        if platform == Platform.ANDROID:
            self.dev_tools.doubleclick_line(text='StackLayout')
            self.dev_tools.wait_element_by_text(text='Label', timeout=10)
        if platform == Platform.IOS:
            self.dev_tools.wait_element_by_text(text='StackLayout', timeout=10)

    def __debug_sources(self, platform, device):
        # Start debug and wait until app is deployed
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text=self.ts_change.old_text)

        # Open sources tab and verify content is loaded
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)

        # Open TS file and place breakpoint on line 21
        self.dev_tools.load_source_file('item-detail.component.ts')
        self.dev_tools.breakpoint(21)

        # Navigate to details page and check breakpoint is hit
        device.click(text=self.ts_change.old_text)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=10)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

        # Resume execution
        self.dev_tools.continue_debug()
        device.wait_for_text(text='Goalkeeper', timeout=30)

        # Sync changes and verify sources tab is updated
        Sync.replace(app_name=self.app_name, change_set=self.ts_change)
        device.wait_for_text(text=self.ts_change.new_text)
        self.dev_tools.load_source_file('item.service.ts')
        self.dev_tools.wait_element_by_text(text=self.ts_change.new_text,
                                            timeout=10)

        # Navigate to details page and check breakpoint is hit
        device.click(text=self.ts_change.new_text)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=10)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

        # Resume execution
        self.dev_tools.continue_debug()
        device.wait_for_text(text='Goalkeeper', timeout=30)
Exemplo n.º 4
0
 def setUp(self):
     TnsRunTest.setUp(self)
     self.chrome = Chrome()
Exemplo n.º 5
0
class PlaygroundDocSamples(TnsRunTest):
    chrome = None
    app_name = Settings.AppName.DEFAULT

    test_data = [
        [
            'getting_started_ng',
            'template=play-ng&tutorial=getting-started-ng',
            'Play with NativeScript!'
        ],
        [
            'getting_started_js',
            'template=groceries-js&tutorial=groceries-js', 'hello world'
        ],
        [
            'animate_background_color', 'template=play-tsc&id=aLjBQg',
            'Tap to animate'
        ],
        ['animate_position', 'template=play-tsc&id=egSanf', 'Tap to animate'],
        ['hub_modal', 'template=play-ng&id=MN31oP', 'Search'],
        ['hub', 'template=play-ng&id=lpCc2k', 'Search'],
        ['animation', 'template=play-tsc&id=h6g8J8', 'Run animation'],
        ['keyframes_animation', 'template=play-tsc&id=tQRe9Q', 'Home'],
        ['navigation', 'template=play-tsc&id=o41kGU', 'Navigate To Item'],
        ['navigate_item_page', 'template=play-tsc&id=qk6ACL', 'Featured'],
        ['layouts', 'template=play-tsc&id=obk2gB', 'Button'],
        [
            'stack_layout', 'template=play-tsc&id=JY218G',
            'Play with NativeScript!'
        ], ['dialogs', 'template=play-ng&id=zJ51uY', 'Confirm'],
        ['share_this', 'template=play-js&id=RTWLSH', 'Share This!'],
        ['action_bar', 'template=play-tsc&id=IrIZ5I', 'Home Alone?'],
        ['events-js', 'template=play-js&id=kIs7uK', 'Events'],
        ['events-ts', 'template=play-tsc&id=8Rhm07', 'Events'],
        ['is_user_iteraction_enabled', 'template=play-tsc&id=6c9GA0', 'TAP']
    ]

    @classmethod
    def setUpClass(cls):
        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.chrome = Chrome()

    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):
        link = PlaygroundDocSamples.get_link(self.chrome, url)
        image_name = '{0}_{1}.png'.format(name, str(Platform.ANDROID))
        Preview.run_url(url=link, device=self.emu)
        self.emu.wait_for_text(text=text)
        self.emu.get_screen(os.path.join(Settings.TEST_OUT_IMAGES, image_name))
        if Settings.HOST_OS == OSType.OSX:
            image_name = '{0}_{1}.png'.format(name, str(Platform.IOS))
            Preview.run_url(url=link, device=self.sim)
            time.sleep(2)
            Preview.dismiss_simulator_alert()
            self.sim.wait_for_text(text=text)
            self.sim.get_screen(
                os.path.join(Settings.TEST_OUT_IMAGES, image_name))

    # noinspection PyBroadException
    @staticmethod
    def get_link(chrome, url):
        url = 'https://play.nativescript.org/?{0}&debug=true'.format(url)
        chrome.open(url)
        link = chrome.driver.find_element_by_xpath(
            "//span[contains(.,'nsplay://boot')]").text
        return link
class DebugJSTests(TnsRunTest):
    app_name = Settings.AppName.DEFAULT
    xml_change = Changes.JSHelloWord.XML_ACTION_BAR
    js_change = Changes.JSHelloWord.JS
    chrome = None
    dev_tools = None

    @classmethod
    def setUpClass(cls):
        TnsRunTest.setUpClass()
        Tns.create(app_name=cls.app_name,
                   template=Template.HELLO_WORLD_JS.local_package,
                   update=True)

        # Instrument the app so it console log events.
        source_js = os.path.join(Settings.TEST_RUN_HOME, 'assets', 'runtime',
                                 'debug', 'files', "console_log",
                                 'main-view-model.js')
        target_js = os.path.join(Settings.TEST_RUN_HOME, cls.app_name, 'app',
                                 'main-view-model.js')
        File.copy(source=source_js, target=target_js)

        Tns.platform_add_android(
            app_name=cls.app_name,
            framework_path=Settings.Android.FRAMEWORK_PATH)
        if Settings.HOST_OS == OSType.OSX:
            Tns.platform_add_ios(app_name=cls.app_name,
                                 framework_path=Settings.IOS.FRAMEWORK_PATH)

    def setUp(self):
        TnsRunTest.setUp(self)
        Sync.revert(app_name=self.app_name,
                    change_set=self.js_change,
                    fail_safe=True)
        Sync.revert(app_name=self.app_name,
                    change_set=self.xml_change,
                    fail_safe=True)
        self.chrome = Chrome()

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

    def test_001_debug_android_elements(self):
        self.__debug_elements(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_001_debug_ios_elements(self):
        self.__debug_elements(platform=Platform.IOS, device=self.sim)

    def test_010_debug_android_console_log(self):
        self.__console_log(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_010_debug_ios_console_log(self):
        self.__console_log(platform=Platform.IOS, device=self.sim)

    def test_011_debug_android_console_eval(self):
        self.__console_eval(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_011_debug_ios_console_eval(self):
        self.__console_eval(platform=Platform.IOS, device=self.sim)

    def test_020_debug_android_sources(self):
        self.__debug_sources(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_020_debug_ios_sources(self):
        self.__debug_sources(platform=Platform.IOS, device=self.sim)

    def test_021_debug_android_synch_changes(self):
        self.__debug_sync_changes(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_021_debug_ios_synch_changes(self):
        self.__debug_sync_changes(platform=Platform.IOS, device=self.sim)

    def test_022_debug_android_watch_expression(self):
        self.__debug_watch_expression(platform=Platform.ANDROID,
                                      device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_022_debug_ios_watch_expression(self):
        self.__debug_watch_expression(platform=Platform.IOS, device=self.sim)

    def test_030_debug_android_brk(self):
        self.__debug_brk(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_030_debug_ios_brk(self):
        self.__debug_brk(platform=Platform.IOS, device=self.sim)

    def test_040_debug_android_start(self):
        self.__debug_start(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_040_debug_ios_start(self):
        self.__debug_start(platform=Platform.IOS, device=self.sim)

    def test_100_reload_chrome_page_android(self):
        self.__reload_page(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_100_reload_chrome_page_ios(self):
        self.__reload_page(platform=Platform.IOS, device=self.sim)

    def __debug_elements(self, platform, device):
        # Run `tns debug` wait until app is visible on device
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text='TAP')

        # Start debug session and verify elements tab
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.ELEMENTS)
        assert self.dev_tools.wait_element_by_text(
            text=self.xml_change.old_text
        ) is not None, 'Elements tab is empty.'

        # Sync changes and verify elements tab is updated
        Sync.replace(app_name=self.app_name, change_set=self.xml_change)
        device.wait_for_text(text=self.xml_change.new_text)
        assert self.dev_tools.wait_element_by_text(
            text=self.xml_change.new_text
        ) is not None, 'Elements tab not updated.'

        # Update label in CDT and verify it is updated on device
        self.dev_tools.edit_text(old_text=self.xml_change.new_text,
                                 new_text=self.xml_change.old_text)
        element = self.dev_tools.wait_element_by_text(
            text=self.xml_change.old_text, timeout=90)
        assert element is not None, 'Failed to change text in elements tab.'
        device.wait_for_text(text=self.xml_change.old_text)

    def __console_log(self, platform, device):
        # Run `tns debug` wait until debug url is in the console and app is loaded
        result = Tns.debug(app_name=self.app_name,
                           platform=platform,
                           emulator=True)
        device.wait_for_text(text='TAP')

        # Open Chrome Dev Tools -> Console
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.CONSOLE)

        # TAP the button to trigger console log and ensure it is in device logs
        device.click(text='TAP', case_sensitive=True)
        assert device.wait_for_log(
            text='Test Debug!',
            timeout=90), 'Console logs not available in device logs.'

        # Ensure logs are available in tns logs
        tns_logs = File.read(result.log_file)
        assert "Test Debug!" in tns_logs, 'Console log messages not available in CLI output.' + os.linesep + tns_logs
        message = 'Console log messages(Arabic and Kurdish characters) not available in CLI output.'
        # add test for issue https://github.com/NativeScript/android-runtime/issues/1302
        assert 'العربییە' in tns_logs, message + os.linesep + tns_logs

        # Verify console logs are available in Chrome dev tools
        log = self.dev_tools.wait_element_by_text(text='Test Debug!')
        assert log is not None, 'Console logs not displayed in Chrome Dev Tools.'
        if Settings.PYTHON_VERSION < 3:
            text = 'العربییە'.decode('utf-8')
        else:
            text = 'العربییە'
        log = self.dev_tools.wait_element_by_text(text=text)
        # add test for issue https://github.com/NativeScript/android-runtime/issues/1302
        assert log is not None, 'Console logs(Arabic and Kurdish characters) not displayed in Chrome Dev Tools.'

    def __console_eval(self, platform, device):
        # Run `tns debug` wait until debug url is in the console and app is loaded
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text='TAP')

        # Open Chrome Dev Tools -> Console
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.CONSOLE)

        # Evaluate on console
        self.dev_tools.type_on_console("1024+1024")
        self.dev_tools.wait_element_by_text(text='2048', timeout=10)

        # TODO: Add tests for eval something {N} specific

    def __debug_sources(self, platform, device):
        # Start debug and wait until app is deployed
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text='TAP')

        # Open sources tab and verify content is loaded
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)

        # Open JS file and place breakpoint on line 18
        self.dev_tools.load_source_file("main-view-model.js")
        self.dev_tools.breakpoint(18)

        # Tap on TAP button in emulator and check it is hit
        device.click(text="TAP", case_sensitive=True)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=40)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

        # Resume execution
        self.dev_tools.continue_debug()
        device.wait_for_text(text='41 taps left', timeout=30)

    def __debug_sync_changes(self, platform, device):
        # Start debug and wait until app is deployed
        result = Tns.debug(app_name=self.app_name,
                           platform=platform,
                           emulator=True)
        device.wait_for_text(text='TAP')

        # Open sources tab and verify content is loaded
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)

        # Open JS file and place breakpoint on line 18
        self.dev_tools.load_source_file("main-view-model.js")
        self.dev_tools.breakpoint(18)

        # Sync JS changes works fine until breakpoint is hit
        Sync.replace(app_name=self.app_name, change_set=Changes.JSHelloWord.JS)
        js_file_name = os.path.basename(Changes.JSHelloWord.JS.file_path)
        logs = [
            js_file_name, 'Webpack build done!', 'Refreshing application',
            'Successfully synced application'
        ]
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs)
        device.wait_for_text(text=Changes.JSHelloWord.JS.new_text, timeout=30)

        # Revert changes
        Sync.revert(app_name=self.app_name, change_set=Changes.JSHelloWord.JS)
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs)
        device.wait_for_text(text=Changes.JSHelloWord.JS.old_text, timeout=30)

        # Tap on TAP button in emulator and check it is hit
        device.click(text="TAP", case_sensitive=True)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=90)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

        # Test for https://github.com/NativeScript/nativescript-cli/issues/4227
        Sync.replace(app_name=self.app_name, change_set=self.xml_change)
        xml_file_name = os.path.basename(self.xml_change.file_path)
        logs = [
            xml_file_name, 'Webpack build done!', 'Refreshing application',
            'Successfully synced application'
        ]
        TnsLogs.wait_for_log(log_file=result.log_file, string_list=logs)
        sleep(10)  # Give it some more time to crash
        logs = File.read(result.log_file)
        assert 'Unable to apply changes' not in logs
        assert 'Stopping webpack watch' not in logs
        assert 'closed' not in logs
        assert 'detached' not in logs
        assert "did not start in time" not in logs

        # Resume execution
        self.dev_tools.continue_debug()
        device.wait_for_text(
            text='42 taps left',
            timeout=30)  # We tapped but changes synced so number is restarted
        device.wait_for_text(text=self.xml_change.new_text,
                             timeout=10)  # Verify change applied during debug

    def __debug_watch_expression(self, platform, device):
        # Start debug and wait until app is deployed
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text='TAP')

        # Open sources tab and place breakpoint on line 18 of main-view-model.js
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)
        self.dev_tools.load_source_file("main-view-model.js")
        self.dev_tools.breakpoint(18)

        # Tap on TAP button in emulator and check it is hit
        device.click(text="TAP", case_sensitive=True)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=60)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

        # Add watch expression
        if platform == Platform.ANDROID:
            self.dev_tools.add_watch_expression(
                expression='console', expected_result='console: Object')
        elif platform == Platform.IOS:
            self.dev_tools.add_watch_expression(
                expression='console', expected_result='console: Console')
        self.dev_tools.add_watch_expression(
            expression='viewModel', expected_result='viewModel: Observable')

    def __debug_brk(self, platform, device):
        # Hack to workaround https://github.com/NativeScript/nativescript-cli/issues/4567
        Tns.run(app_name=self.app_name,
                platform=platform,
                emulator=True,
                source_map=True,
                just_launch=True)
        device.wait_for_text(text='TAP')

        Tns.debug(app_name=self.app_name,
                  platform=platform,
                  emulator=True,
                  debug_brk=True)
        self.dev_tools = ChromeDevTools(self.chrome, platform=platform)
        self.dev_tools.open_tab(tab=ChromeDevToolsTabs.SOURCES, verify=False)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Debugger paused", timeout=30)
        assert pause_element is not None, 'Failed to stop on first line of code.'
        assert self.dev_tools.find_element_by_text(
            text="function"
        ) is not None, 'Failed to stop on first line of code.'
        assert 'NativeScript' in device.get_text(
        ), 'Failed to stop on first line of code.'

        # Resume execution
        self.dev_tools.continue_debug()
        device.wait_for_text(text='TAP')

    def __debug_start(self, platform, device):
        # Run the app and verify it is deployed
        Tns.run(app_name=self.app_name,
                platform=platform,
                emulator=True,
                source_map=True,
                just_launch=True)
        device.wait_for_text(text='TAP')

        # Attach debug session
        result = Tns.debug(app_name=self.app_name,
                           platform=platform,
                           emulator=True,
                           start=True)

        # Verify sources tab is loaded
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)
        device.wait_for_text(text='TAP')

        # Verify console log is working (covers https://github.com/NativeScript/nativescript-cli/issues/3629)
        self.dev_tools.open_tab(ChromeDevToolsTabs.CONSOLE)
        device.click(text='TAP', case_sensitive=True)
        assert self.dev_tools.wait_element_by_text(
            text='Test Debug!'
        ) is not None, 'Console logs not displayed in CDT.'
        tns_logs = File.read(result.log_file)
        assert "Test Debug!" in tns_logs, 'Console log messages not available in CLI output.' + os.linesep + tns_logs

        # Verify debug is working (covers https://github.com/NativeScript/nativescript-cli/issues/2831)
        self.dev_tools.open_tab(ChromeDevToolsTabs.SOURCES)
        self.dev_tools.load_source_file("main-view-model.js")
        self.dev_tools.breakpoint(18)
        device.click(text="TAP", case_sensitive=True)
        pause_element = self.dev_tools.wait_element_by_text(
            text="Paused on breakpoint", timeout=10)
        assert pause_element is not None, 'Failed to pause on breakpoint.'

    def __reload_page(self, platform, device):
        # Start debug and wait until app is deployed
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        device.wait_for_text(text='TAP')

        # Open CDT with debug url twice
        self.dev_tools = ChromeDevTools(self.chrome, platform=platform)
        sleep(1)
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)
        device.wait_for_text(text='TAP')

        # Open another site and then back to CDT
        self.chrome.open(url='https://www.nativescript.org')
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.SOURCES)
        device.wait_for_text(text='TAP')
Exemplo n.º 7
0
 def setUpClass(cls):
     TnsRunTest.setUpClass()
     NG.kill()
     NG.new(collection=None, project=cls.app_name)
     cls.chrome = Chrome()
Exemplo n.º 8
0
 def setUpClass(cls):
     cls.chrome = Chrome()
Exemplo n.º 9
0
class DebugNetworkTests(TnsRunTest):
    app_name = Settings.AppName.DEFAULT
    chrome = None
    dev_tools = None

    @classmethod
    def setUpClass(cls):
        TnsRunTest.setUpClass()
        app_folder = os.path.join(Settings.TEST_RUN_HOME, cls.app_name)
        Folder.clean(app_folder)
        Git.clone(
            repo_url='https://github.com/NativeScript/chrome-devtools-test-app',
            local_folder=app_folder)
        Tns.platform_add_android(
            app_name=cls.app_name,
            framework_path=Settings.Android.FRAMEWORK_PATH)
        if Settings.HOST_OS == OSType.OSX:
            Tns.platform_add_ios(app_name=cls.app_name,
                                 framework_path=Settings.IOS.FRAMEWORK_PATH)
        App.update(app_name=cls.app_name)

    def setUp(self):
        TnsRunTest.setUp(self)
        self.chrome = Chrome()

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

    def test_010_debug_android_elements(self):
        self.__debug_elements(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_010_debug_ios_elements(self):
        self.__debug_elements(platform=Platform.IOS, device=self.sim)

    def test_020_debug_android_console(self):
        self.__debug_console(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_020_debug_ios_console(self):
        self.__debug_console(platform=Platform.IOS, device=self.sim)

    def test_030_debug_android_sources(self):
        self.__debug_sources(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_030_debug_ios_sources(self):
        self.__debug_sources(platform=Platform.IOS, device=self.sim)

    def test_040_debug_android_network(self):
        self.__debug_network(platform=Platform.ANDROID, device=self.emu)

    @unittest.skipIf(Settings.HOST_OS != OSType.OSX,
                     'Can not debug iOS on non macOS hosts.')
    def test_040_debug_ios_network(self):
        self.__debug_network(platform=Platform.IOS, device=self.sim)

    def __debug_elements(self, platform, device):
        # Start debug and open elements tab
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.ELEMENTS)
        self.dev_tools.wait_element_by_text(text=ACTION_BAR_TITLE)

        # Add child and verify it is visible in CDT
        device.click(text=HOME_ADD_CHILD_BUTTON)
        self.dev_tools.doubleclick_line(text='StackLayout')
        self.dev_tools.doubleclick_line(text='ScrollView')
        time.sleep(4)
        self.dev_tools.doubleclick_line(text='FlexboxLayout')
        if platform == Platform.ANDROID:
            assert self.dev_tools.wait_element_by_text(
                text='StackLayout id=') is not None
        else:
            assert self.dev_tools.wait_element_by_text(
                text='StackLayout iosOverflowSafeArea="true" id=') is not None

        # Remove child and verify it is NOT visible in CDT
        device.click(text=HOME_REMOVE_CHILD_BUTTON)
        text = 'StackLayout id='
        if platform == Platform.IOS:
            text = 'StackLayout iosOverflowSafeArea="true" id='
        found = Wait.until(
            lambda: self.dev_tools.find_element_by_text(text=text) is None,
            timeout=10)
        assert found is True, 'Item not removed from elements tab after removed from app.'

    def __debug_console(self, platform, device):
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.CONSOLE)
        device.click(text=HOME_CONSOLE_BUTTON)
        if platform == Platform.ANDROID:
            self.dev_tools.wait_element_by_text(text='main-view-model.ts:34')
        else:
            self.dev_tools.wait_element_by_text(text='main-view-model.ts:35')

    def __debug_sources(self, platform, device):
        # Run debug, to network page and open network tab in CDT
        result = Tns.debug(app_name=self.app_name,
                           platform=platform,
                           emulator=True)
        device.click(text=HOME_NETWORK_BUTTON)
        self.dev_tools = ChromeDevTools(self.chrome, platform=platform)
        self.dev_tools.open_tab(tab=ChromeDevToolsTabs.SOURCES, verify=False)

        # Place breakpoint and verify it is hit
        self.dev_tools.load_source_file('network-page.ts')
        self.dev_tools.breakpoint(line=12)
        device.click(text=NET_GET_WITHOUT_BODY)
        self.dev_tools.wait_element_by_text(text='Paused on breakpoint',
                                            timeout=10)
        assert 'httpbin.org' not in File.read(result.log_file)
        self.dev_tools.continue_debug()
        TnsLogs.wait_for_log(log_file=result.log_file,
                             string_list=['httpbin.org'])

        # Add one more breakpoint and hit it
        self.dev_tools.load_source_file('network-page.ts')
        self.dev_tools.breakpoint(line=30)
        device.click(text=NET_GET_WITH_BODY)
        self.dev_tools.wait_element_by_text(text='Paused on breakpoint',
                                            timeout=60)
        assert 'My custom Arbitrary Header value' not in File.read(
            result.log_file)
        self.dev_tools.continue_debug()
        TnsLogs.wait_for_log(log_file=result.log_file,
                             string_list=['My custom Arbitrary Header value'])

    def __debug_network(self, platform, device):
        Tns.debug(app_name=self.app_name, platform=platform, emulator=True)
        self.dev_tools = ChromeDevTools(self.chrome,
                                        platform=platform,
                                        tab=ChromeDevToolsTabs.NETWORK)
        device.click(text=HOME_NETWORK_BUTTON)

        # Request without body
        device.click(text=NET_GET_WITHOUT_BODY)
        assert self.dev_tools.wait_element_by_text(text='get') is not None
        assert self.dev_tools.wait_element_by_text(text='200') is not None
        assert self.dev_tools.wait_element_by_text(text='0 B') is not None
        self.dev_tools.clean_network_tab()

        # Request with body
        device.click(text=NET_GET_WITH_BODY)
        assert self.dev_tools.wait_element_by_text(text='get',
                                                   timeout=60) is not None
        assert self.dev_tools.wait_element_by_text(text='200',
                                                   timeout=50) is not None
        assert self.dev_tools.wait_element_by_text(text=' B',
                                                   timeout=50) is not None
        self.dev_tools.clean_network_tab()
Exemplo n.º 10
0
 def setUp(self):
     self.chrome = Chrome()
    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)
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()