Exemple #1
0
  def WaitForJavaScriptCondition(self, condition, **kwargs):
    """Wait for a JavaScript condition to become truthy.

    Example: runner.WaitForJavaScriptCondition('window.foo == 10');

    Args:
      condition: The JavaScript condition (provided as string).

    Optional keyword args:
      timeout: The number in seconds to wait for the condition to become
          True (default to 60).
      context_id: The id of an iframe where to execute the code; the main page
          has context_id=1, the first iframe context_id=2, etc.
      Additional keyword arguments provide values to be interpolated within
          the expression. See telemetry.util.js_template for details.

    Returns:
      The value returned by the JavaScript condition that got interpreted as
      true.

    Raises:
      py_utils.TimeoutException
      exceptions.EvaluationException
      exceptions.WebSocketException
      exceptions.DevtoolsTargetCrashException
    """
    # Use the default both when timeout=None or the option is ommited.
    timeout = kwargs.pop('timeout', None) or 60
    context_id = kwargs.pop('context_id', None)
    condition = js_template.Render(condition, **kwargs)

    def IsJavaScriptExpressionTrue():
      try:
        return self._EvaluateJavaScript(condition, context_id, timeout)
      except exceptions.EvaluationException:
        # Eval may throw due to e.g. navigation.
        return False

    try:
      return py_utils.WaitFor(IsJavaScriptExpressionTrue, timeout)
    except py_utils.TimeoutException as e:
      # Try to make timeouts a little more actionable by dumping console output.
      debug_message = None
      try:
        debug_message = (
            'Console output:\n%s' %
            self.GetCurrentConsoleOutputBuffer())
      except Exception as e: # pylint: disable=broad-except
        debug_message = (
            'Exception thrown when trying to capture console output: %s' %
            repr(e))
      # Rethrow with the original stack trace for better debugging.
      raise py_utils.TimeoutException, \
          py_utils.TimeoutException(
              'Timeout after %ss while waiting for JavaScript:' % timeout +
              condition + '\n' +  e.message + '\n' + debug_message), \
          sys.exc_info()[2]
Exemple #2
0
 def RunAction(self, tab):
     code = 'function(element) { return element != null; }'
     try:
         self.EvaluateCallback(tab,
                               code,
                               wait=True,
                               timeout_in_seconds=self.timeout)
     except py_utils.TimeoutException as e:
         # Rethrow with the original stack trace for better debugging.
         raise py_utils.TimeoutException, \
             py_utils.TimeoutException(
                 'Timeout while waiting for element.\n' + e.message), \
             sys.exc_info()[2]
    def WaitForJavaScriptCondition2(self, condition, **kwargs):
        """Wait for a JavaScript condition to become true.

    Example: runner.WaitForJavaScriptCondition2('window.foo == 10');

    Args:
      condition: The JavaScript condition (provided as string).

    Optional keyword args:
      timeout: The number in seconds to wait for the condition to become
          True (default to 60).
      context_id: The id of an iframe where to execute the code; the main page
          has context_id=1, the first iframe context_id=2, etc.
      Additional keyword arguments provide values to be interpolated within
          the expression. See telemetry.util.js_template for details.

    Raises:
      py_utils.TimeoutException
      exceptions.EvaluationException
      exceptions.WebSocketException
      exceptions.DevtoolsTargetCrashException
    """
        # Use the default both when timeout=None or the option is ommited.
        timeout = kwargs.get('timeout') or 60
        context_id = kwargs.get('context_id')
        condition = js_template.Render(condition, **kwargs)

        def IsJavaScriptExpressionTrue():
            return bool(self._runtime.Evaluate(condition, context_id, timeout))

        try:
            py_utils.WaitFor(IsJavaScriptExpressionTrue, timeout)
        except py_utils.TimeoutException as e:
            # Try to make timeouts a little more actionable by dumping console output.
            debug_message = None
            try:
                debug_message = ('Console output:\n%s' %
                                 self.GetCurrentConsoleOutputBuffer())
            except Exception as e:
                debug_message = (
                    'Exception thrown when trying to capture console output: %s'
                    % repr(e))
            raise py_utils.TimeoutException(e.message + '\n' + debug_message)
Exemple #4
0
    def WaitForJavaScriptExpression(self, expr, timeout):
        """Waits for the given JavaScript expression to be True.

    This method is robust against any given Evaluation timing out.

    Args:
      expr: The expression to evaluate.
      timeout: The number of seconds to wait for the expression to be True.

    Raises:
      py_utils.TimeoutException: On a timeout.
      exceptions.Error: See EvaluateJavaScript() for a detailed list of
      possible exceptions.
    """
        def IsJavaScriptExpressionTrue():
            try:
                return bool(self.EvaluateJavaScript(expr))
            except py_utils.TimeoutException:
                # If the main thread is busy for longer than Evaluate's timeout, we
                # may time out here early. Instead, we want to wait for the full
                # timeout of this method.
                return False

        try:
            py_utils.WaitFor(IsJavaScriptExpressionTrue, timeout)
        except py_utils.TimeoutException as e:
            # Try to make timeouts a little more actionable by dumping console output.
            debug_message = None
            try:
                debug_message = (
                    'Console output:\n%s' %
                    self._inspector_backend.GetCurrentConsoleOutputBuffer())
            except Exception as e:
                debug_message = (
                    'Exception thrown when trying to capture console output: %s'
                    % repr(e))
            raise py_utils.TimeoutException(e.message + '\n' + debug_message)