def test_runtime(self):
        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/web_debugger.html')
            with self.CreateCobaltRunner(url=url) as runner:
                with self.create_debugger_connection(runner) as debugger:
                    runner.WaitForJSTestsSetup()
                    debugger.enable_runtime()

                    # Evaluate a simple expression.
                    eval_response = debugger.evaluate_js('6 * 7')
                    self.assertEqual(
                        42, eval_response['result']['result']['value'])

                    # Set an attribute and read it back w/ WebDriver.
                    debugger.evaluate_js(
                        'document.body.setAttribute("web_debugger", "tested")')
                    self.assertEqual(
                        'tested',
                        runner.UniqueFind('body').get_attribute(
                            'web_debugger'))

                    # Log to console, and check we get the console event.
                    debugger.evaluate_js('console.log("hello")')
                    console_event = debugger.wait_event(
                        'Runtime.consoleAPICalled')
                    self.assertEqual(
                        'hello', console_event['params']['args'][0]['value'])

                    # End the test.
                    debugger.evaluate_js('onEndTest()')
                    self.assertTrue(runner.JSTestsSucceeded())
  def test_simple(self):

    with ThreadedWebServer(binding_address=self.GetBindingAddress()) as server:
      url = server.GetURL(file_name='testdata/disable_eval_with_csp.html')

      with self.CreateCobaltRunner(url=url) as runner:
        self.assertTrue(runner.JSTestsSucceeded())
Example #3
0
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/persistent_cookie.html')

            # The webpage listens for NUMPAD1, NUMPAD2 and NUMPAD3 at opening.
            with self.CreateCobaltRunner(url=url) as runner:
                # Press NUMPAD1 to verify basic cookie functionality and set
                # a persistent cookie.
                runner.WaitForJSTestsSetup()
                runner.SendKeys(keys.Keys.NUMPAD1)
                self.assertTrue(runner.JSTestsSucceeded())

            with self.CreateCobaltRunner(url=url) as runner:
                runner.WaitForJSTestsSetup()
                # Press NUMPAD2 to indicate this is the second time we opened
                # the webpage and verify a persistent cookie is on device. Then
                # clear this persistent cookie.
                runner.SendKeys(keys.Keys.NUMPAD2)
                self.assertTrue(runner.JSTestsSucceeded())

            with self.CreateCobaltRunner(url=url) as runner:
                runner.WaitForJSTestsSetup()
                # Press NUMPAD3 to verify the persistent cookie we cleared is
                # not on the device for this URL any more.
                runner.SendKeys(keys.Keys.NUMPAD3)
                self.assertTrue(runner.JSTestsSucceeded())
    def test_simple(self):
        """Set UA parameters when launching Cobalt."""

        with ThreadedWebServer(
                JavascriptRequestDetector,
                binding_address=self.GetBindingAddress()) as server:
            with self.CreateCobaltRunner(
                url=server.GetURL(file_name='testdata/override_ua_parameters.html'),
                target_params=[
                    '--user_agent_client_hints='\
                    'aux_field=foo.bar.baz.qux/21.2.1.41.0;'\
                    'brand=Cobalt;'\
                    'build_configuration=debug;'\
                    'chipset_model_number=foobar0000;'\
                    'cobalt_build_version_number=289852;'\
                    'cobalt_version=21.lts.2;'\
                    'connection_type=Wireless;'\
                    'device_type=ATV;'\
                    'evergreen_type=;'\
                    'evergreen_version=;'\
                    'javascript_engine_version=v8/7.7.299.8-jit;'\
                    'firmware_version=;'\
                    'model=QUUX;'\
                    'model_year=2018;'\
                    'original_design_manufacturer=Quuz;'\
                    'os_name_and_version=Corge grault-v7a\\; Garply 7.1.2\\; '\
                        'Waldo OS 6.0;'\
                    'starboard_version=Starboard/12;'\
                    'rasterizer_type=gles'
                ]) as runner:
                runner.WaitForJSTestsSetup()
                self.assertTrue(runner.JSTestsSucceeded())
Example #5
0
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/timer_hit_in_preload.html')
            with self.CreateCobaltRunner(url=url, target_params=['--preload'
                                                                 ]) as runner:
                self.assertTrue(runner.JSTestsSucceeded())
                self.assertTrue(runner.IsInPreload())
Example #6
0
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/suspend_visibility.html')

            with self.CreateCobaltRunner(url=url) as runner:
                runner.WaitForJSTestsSetup()
                runner.SendSuspend()
                runner.SendResume()
                self.assertTrue(runner.JSTestsSucceeded())
Example #7
0
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/preload_visibility.html')

            with self.CreateCobaltRunner(url=url, target_params=['--preload'
                                                                 ]) as runner:
                runner.WaitForJSTestsSetup()
                self.assertTrue(runner.IsInPreload())
                runner.SendResume()
                self.assertTrue(runner.JSTestsSucceeded())
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(
                file_name='testdata/timer_hit_after_preload.html')
            with self.CreateCobaltRunner(url=url, target_params=['--preload'
                                                                 ]) as runner:
                self.assertTrue(runner.IsInPreload())
                # setInterval will hit once during the .5 seconds.
                runner.PollUntilFound('#script_executed')
                runner.SendResume()
                self.assertTrue(runner.JSTestsSucceeded())
Example #9
0
  def test_simple(self):

    with ThreadedWebServer(binding_address=self.GetBindingAddress()) as server:
      url = server.GetURL(file_name='testdata/freeze_timers.html')

      with self.CreateCobaltRunner(url=url) as runner:
        runner.WaitForJSTestsSetup()
        time.sleep(2.5)
        runner.SendConceal()
        time.sleep(2.5)
        runner.SendFreeze()
        time.sleep(3.333)
        runner.SendFocus()
        self.assertTrue(runner.JSTestsSucceeded())
Example #10
0
    def _DelayedLink(self, query_parameter=''):
        """Test sending links into Cobalt after it's started."""

        logging.info('[ RUN ] ' + inspect.stack()[1][3] + ' ' +
                     query_parameter)
        # Step 2. Start Cobalt, and point it to the socket created in Step 1.
        try:
            with ThreadedWebServer(JavascriptRequestDetector,
                                   self.GetBindingAddress()) as server:
                with self.CreateCobaltRunner(url='about:blank') as runner:
                    target_url = server.GetURL(file_name='../testdata/' +
                                               _DEEP_LINKS_HTML)
                    if query_parameter != '':
                        target_url += '?' + query_parameter
                    cobalt_launcher_thread = threading.Thread(
                        target=DeepLink._LoadPage,
                        args=(self, runner.webdriver, target_url))
                    cobalt_launcher_thread.start()

                    # Step 3. Load & run the javascript resource.
                    _script_loading_signal.set()

                    # Step 4. Wait before sending the link.
                    runner.WaitForJSTestsSetup()

                    # Step 5. Send deep link
                    link = 'link 3'  # Expected by our test JS
                    logging.info('Sending link : ' + link)
                    self.assertTrue(runner.SendDeepLink(link))
                    logging.info('Links fired.')

                    # Step 6. Check to see if JSTestsSucceeded().
                    # Note that this call will check the DOM multiple times for a period
                    # of time (current default is 30 seconds).
                    success = runner.JSTestsSucceeded()
                    if success:
                        logging.info('[ OK ] ' + inspect.stack()[1][3])
                    else:
                        logging.info('[ FAILED ] ' + inspect.stack()[1][3])
                    self.assertTrue(success)
        except:  # pylint: disable=bare-except
            traceback.print_exc()
            # Consider an exception being thrown as a test failure.
            self.assertTrue(False)
        finally:
            logging.info('Cleaning up.')
            _script_loading_signal.set()
Example #11
0
    def test_simple(self):

        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/preload_font.html')

            with self.CreateCobaltRunner(url=url, target_params=['--preload'
                                                                 ]) as runner:
                runner.WaitForJSTestsSetup()
                runner.SendResume()
                start_time = time.time()
                while runner.IsInPreload():
                    if time.time() - start_time > _MAX_RESUME_WAIT_SECONDS:
                        raise Exception(
                            'Cobalt can not exit preload mode after receiving'
                            'resume signal')
                    time.sleep(.1)
                # At this point, Cobalt is in started mode.
                self.assertTrue(runner.JSTestsSucceeded())
    def test_simple(self):

        # Step 2. Start Cobalt, and point it to the socket created in Step 1.
        try:
            with ThreadedWebServer(JavascriptRequestDetector,
                                   self.GetBindingAddress()) as server:
                with self.CreateCobaltRunner(url='about:blank') as runner:
                    target_url = server.GetURL(
                        file_name='../testdata/' +
                        _CANCEL_SYNC_LOADS_WHEN_SUSPENDED_HTML)
                    cobalt_launcher_thread = threading.Thread(
                        target=CancelSyncLoadsWhenSuspended._LoadPage,
                        args=(self, runner.webdriver, target_url))
                    cobalt_launcher_thread.start()

                    # Step 3. Wait HTTP request for html resource.
                    logging.info('Waiting for script resource request')
                    request_received = _received_script_resource_request.wait(
                        _MAX_ALLOTTED_TIME_SECONDS)
                    logging.info(
                        'Request received: {}'.format(request_received))
                    # Step 5. Wait for a request for javascript resource.
                    self.assertTrue(request_received)

                    # Step 6. Suspend Cobalt process.
                    logging.info('Suspending Cobalt.')
                    runner.SendSuspend()
                    # Step 7. Cobalt disconnects from the socket.
                    # Step 8. Resume Cobalt process, which enables the JS test to pass.
                    logging.info('Resuming Cobalt.')
                    runner.SendResume()

                    # Step 9. Check to see if JSTestsSucceeded().
                    # Note that this call will check the DOM multiple times for a period
                    # of time (current default is 30 seconds).
                    self.assertTrue(runner.JSTestsSucceeded())
        except:  # pylint: disable=bare-except
            traceback.print_exc()
            # Consider an exception being thrown as a test failure.
            self.assertTrue(False)
        finally:
            logging.info('Cleaning up.')
            _test_finished.set()
  def test_simple(self):

    with ThreadedWebServer(binding_address=self.GetBindingAddress()) as server:
      url = server.GetURL(file_name='testdata/preload_launch_parameter.html')

      # Test without other parameters.
      with self.CreateCobaltRunner(
          url=url, target_params=['--preload']) as runner:
        runner.WaitForJSTestsSetup()
        self.assertTrue(runner.IsInPreload())
        runner.SendResume()
        self.assertTrue(runner.JSTestsSucceeded())

      # Test with another parameter.
      with self.CreateCobaltRunner(
          url=url + "?foo=bar", target_params=['--preload']) as runner:
        runner.WaitForJSTestsSetup()
        self.assertTrue(runner.IsInPreload())
        runner.SendResume()
        self.assertTrue(runner.JSTestsSucceeded())
Example #14
0
    def _StartLink(self, query_parameter=''):
        """Test the initial link provided when starting Cobalt."""

        logging.info('[ RUN ] ' + inspect.stack()[1][3] + ' ' +
                     query_parameter)
        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/' + _DEEP_LINKS_HTML)
            if query_parameter != '':
                url += '?' + query_parameter
            initial_deep_link = 'link 3'  # Expected by our test JS

            with self.CreateCobaltRunner(url=url,
                                         target_params=[
                                             '--link=' + initial_deep_link
                                         ]) as runner:
                success = runner.JSTestsSucceeded()
                if success:
                    logging.info('[ OK ] ' + inspect.stack()[1][3])
                else:
                    logging.info('[ FAILED ] ' + inspect.stack()[1][3])
                self.assertTrue(success)
    def test_dom(self):
        with ThreadedWebServer(
                binding_address=self.GetBindingAddress()) as server:
            url = server.GetURL(file_name='testdata/web_debugger.html')
            with self.CreateCobaltRunner(url=url) as runner:
                with self.create_debugger_connection(runner) as debugger:
                    runner.WaitForJSTestsSetup()
                    debugger.enable_runtime()
                    debugger.run_command('DOM.enable')

                    doc_response = debugger.run_command('DOM.getDocument')
                    doc_root = doc_response['result']['root']
                    self.assertEqual('#document', doc_root['nodeName'])

                    doc_url = doc_root['documentURL']
                    # remove query params (cert_scope, etc.)
                    doc_url = doc_url.split('?')[0]
                    self.assertEqual(url, doc_url)

                    # document: <html><head></head><body></body></html>
                    html_node = doc_root['children'][0]
                    body_node = html_node['children'][1]
                    self.assertEqual('BODY', body_node['nodeName'])

                    # body:
                    #   <h1><span>Web debugger</span></h1>
                    #   <div#test>
                    #     <div#A><div#A1/><div#A2/></div#A>
                    #     <div#B/>
                    #   </div#test>
                    debugger.run_command(
                        'DOM.requestChildNodes',
                        {
                            'nodeId': body_node['nodeId'],
                            'depth': -1,  # entire subtree
                        })
                    child_nodes_event = debugger.wait_event(
                        'DOM.setChildNodes')

                    h1 = child_nodes_event['params']['nodes'][0]
                    span = h1['children'][0]
                    text = span['children'][0]
                    self.assertEqual('H1', h1['nodeName'])
                    self.assertEqual('SPAN', span['nodeName'])
                    self.assertEqual('#text', text['nodeName'])
                    self.assertEqual('Web debugger', text['nodeValue'])

                    test_div = child_nodes_event['params']['nodes'][1]
                    child_a = test_div['children'][0]
                    child_a1 = child_a['children'][0]
                    child_a2 = child_a['children'][1]
                    child_b = test_div['children'][1]
                    self.assertEqual(2, test_div['childNodeCount'])
                    self.assertEqual(2, child_a['childNodeCount'])
                    self.assertEqual(0, child_b['childNodeCount'])
                    self.assertEqual(['id', 'test'], test_div['attributes'])
                    self.assertEqual(['id', 'A'], child_a['attributes'])
                    self.assertEqual(['id', 'A1'], child_a1['attributes'])
                    self.assertEqual(['id', 'A2'], child_a2['attributes'])
                    self.assertEqual(['id', 'B'], child_b['attributes'])
                    self.assertEqual([], child_b['children'])

                    # Repeat, but only to depth 2 - not reporting children of A & B.
                    debugger.run_command('DOM.requestChildNodes', {
                        'nodeId': body_node['nodeId'],
                        'depth': 2,
                    })
                    child_nodes_event = debugger.wait_event(
                        'DOM.setChildNodes')

                    test_div = child_nodes_event['params']['nodes'][1]
                    child_a = test_div['children'][0]
                    child_b = test_div['children'][1]
                    self.assertFalse('children' in child_a)
                    self.assertFalse('children' in child_b)
                    self.assertEqual(2, test_div['childNodeCount'])
                    self.assertEqual(2, child_a['childNodeCount'])
                    self.assertEqual(0, child_b['childNodeCount'])
                    self.assertEqual(['id', 'test'], test_div['attributes'])
                    self.assertEqual(['id', 'A'], child_a['attributes'])
                    self.assertEqual(['id', 'B'], child_b['attributes'])

                    # Repeat, to default depth of 1 - not reporting children of "#test".
                    debugger.run_command('DOM.requestChildNodes', {
                        'nodeId': body_node['nodeId'],
                    })
                    child_nodes_event = debugger.wait_event(
                        'DOM.setChildNodes')

                    test_div = child_nodes_event['params']['nodes'][1]
                    self.assertFalse('children' in test_div)
                    self.assertEqual(2, test_div['childNodeCount'])
                    self.assertEqual(['id', 'test'], test_div['attributes'])

                    # Get the test div as a remote object, and request it as a node.
                    # This sends a 'DOM.setChildNodes' event for each node up to the root.
                    eval_result = debugger.evaluate_js(
                        'document.getElementById("test")')
                    node_response = debugger.run_command(
                        'DOM.requestNode', {
                            'objectId':
                            eval_result['result']['result']['objectId'],
                        })
                    self.assertEqual(test_div['nodeId'],
                                     node_response['result']['nodeId'])

                    # Event reporting the requested <div#test>
                    node_event = debugger.wait_event('DOM.setChildNodes')
                    self.assertEqual(
                        test_div['nodeId'],
                        node_event['params']['nodes'][0]['nodeId'])
                    self.assertEqual(body_node['nodeId'],
                                     node_event['params']['parentId'])

                    # Event reporting the parent <body>
                    node_event = debugger.wait_event('DOM.setChildNodes')
                    self.assertEqual(
                        body_node['nodeId'],
                        node_event['params']['nodes'][0]['nodeId'])
                    self.assertEqual(html_node['nodeId'],
                                     node_event['params']['parentId'])

                    # Event reporting the parent <html>
                    node_event = debugger.wait_event('DOM.setChildNodes')
                    self.assertEqual(
                        html_node['nodeId'],
                        node_event['params']['nodes'][0]['nodeId'])
                    self.assertEqual(doc_root['nodeId'],
                                     node_event['params']['parentId'])

                    # Round trip resolving test div to an object, then back to a node.
                    resolve_response = debugger.run_command(
                        'DOM.resolveNode', {
                            'nodeId': test_div['nodeId'],
                        })
                    node_response = debugger.run_command(
                        'DOM.requestNode', {
                            'objectId':
                            resolve_response['result']['object']['objectId'],
                        })
                    self.assertEqual(test_div['nodeId'],
                                     node_response['result']['nodeId'])

                    # Event reporting the requested <div#test>
                    node_event = debugger.wait_event('DOM.setChildNodes')
                    self.assertEqual(
                        test_div['nodeId'],
                        node_event['params']['nodes'][0]['nodeId'])
                    self.assertEqual(body_node['nodeId'],
                                     node_event['params']['parentId'])
                    # Ignore the other two events reporting the parents.
                    node_event = debugger.wait_event('DOM.setChildNodes')
                    node_event = debugger.wait_event('DOM.setChildNodes')

                    # End the test.
                    debugger.evaluate_js('onEndTest()')
                    self.assertTrue(runner.JSTestsSucceeded())
Example #16
0
    def test_pointer_events(self):
        try:
            with ThreadedWebServer(
                    binding_address=self.GetBindingAddress()) as server:
                url = server.GetURL(file_name='testdata/pointer_test.html')

                with self.CreateCobaltRunner(url=url) as runner:
                    logging.info('JS Test Setup WaitForJSTestsSetup')
                    runner.WaitForJSTestsSetup()
                    logging.info('JS Test Setup')
                    self.assertTrue(runner.webdriver)

                    top_one = find_element_by_id(runner, 'top_one')
                    top_two = find_element_by_id(runner, 'top_two')
                    top_three = find_element_by_id(runner, 'top_three')
                    top_four = find_element_by_id(runner, 'top_four')
                    top_five = find_element_by_id(runner, 'top_five')
                    top_six = find_element_by_id(runner, 'top_six')

                    bottom_one = find_element_by_id(runner, 'bottom_one')
                    bottom_two = find_element_by_id(runner, 'bottom_two')
                    bottom_three = find_element_by_id(runner, 'bottom_three')
                    bottom_four = find_element_by_id(runner, 'bottom_four')
                    bottom_five = find_element_by_id(runner, 'bottom_five')
                    bottom_six = find_element_by_id(runner, 'bottom_six')

                    # Perform mouse actions with ActionChains.
                    #   https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#module-selenium.webdriver.common.action_chains
                    actions = ActionChains(runner.webdriver)
                    actions.move_to_element(top_one).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element(top_two).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element_with_offset(
                        top_two, 10, 10).pause(_SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element_with_offset(
                        top_two, 0, 0).pause(_SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element_with_offset(
                        top_two, -10, 0).pause(_SLEEP_AFTER_MOVE_TIME)
                    actions.click(top_three)
                    actions.click_and_hold(top_four)
                    actions.release(top_five)
                    actions.move_to_element(top_six).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element(bottom_six).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.click(bottom_five)
                    actions.click_and_hold(bottom_four)
                    actions.release(bottom_three)
                    actions.move_to_element(bottom_two).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.move_to_element(bottom_one).pause(
                        _SLEEP_AFTER_MOVE_TIME)
                    actions.perform()

                    find_element_by_id(runner, 'end').click()
                    self.assertTrue(runner.JSTestsSucceeded())
        except:  # pylint: disable=bare-except
            traceback.print_exc()
            # Consider an exception being thrown as a test failure.
            self.assertTrue(False)
        finally:
            logging.info('Cleaning up.')