def DoClick():
   if hasattr(self, 'selector'):
     code = ('document.querySelector(\'' + _EscapeSelector(self.selector) +
         '\').click();')
     try:
       tab.ExecuteJavaScript(code)
     except exceptions.EvaluateException:
       raise page_action.PageActionFailed(
           'Cannot find element with selector ' + self.selector)
   elif hasattr(self, 'text'):
     callback_code = 'function(element) { element.click(); }'
     try:
       util.FindElementAndPerformAction(tab, self.text, callback_code)
     except exceptions.EvaluateException:
       raise page_action.PageActionFailed(
           'Cannot find element with text ' + self.text)
   elif hasattr(self, 'xpath'):
     code = ('document.evaluate("%s",'
                                'document,'
                                'null,'
                                'XPathResult.FIRST_ORDERED_NODE_TYPE,'
                                'null)'
               '.singleNodeValue.click()' % re.escape(self.xpath))
     try:
       tab.ExecuteJavaScript(code)
     except exceptions.EvaluateException:
       raise page_action.PageActionFailed(
           'Cannot find element with xpath ' + self.xpath)
   else:
     raise page_action.PageActionFailed(
         'No condition given to javascript_click')
Example #2
0
  def TapSelectedElement(self, tab, js_cmd):
    assert self.HasElementSelector()
    if hasattr(self, 'text'):
      callback_code = 'function(element) { %s }' % js_cmd
      util.FindElementAndPerformAction(tab, self.text, callback_code)
    else:
      if hasattr(self, 'element_function'):
        element_function = self.element_function
      elif hasattr(self, 'selector'):
        element_cmd = ('document.querySelector(\'' +
            _EscapeSelector(self.selector) + '\')')
        element_function = 'function(callback) { callback(%s); }' % element_cmd
      elif hasattr(self, 'xpath'):
        element_cmd = ('document.evaluate("%s",'
                                          'document,'
                                          'null,'
                                          'XPathResult.FIRST_ORDERED_NODE_TYPE,'
                                          'null)'
                              '.singleNodeValue' % re.escape(self.xpath))
        element_function = 'function(callback) { callback(%s); }' % element_cmd
      else:
        assert False

      tab.ExecuteJavaScript('(%s)(function(element) { %s });' %
                                (element_function, js_cmd))
Example #3
0
    def RunAction(self, page, tab, previous_action):
        tab.ExecuteJavaScript('console.time("' + self.GetTimelineMarkerName() +
                              '")')

        if hasattr(self, 'seconds'):
            time.sleep(self.seconds)

        elif getattr(self, 'condition', None) == 'navigate':
            if not previous_action:
                raise page_action.PageActionFailed(
                    'You need to perform an action '
                    'before waiting for navigate.')
            previous_action.WillRunAction(page, tab)
            action_to_perform = lambda: previous_action.RunAction(
                page, tab, None)
            tab.PerformActionAndWaitForNavigate(action_to_perform,
                                                self.timeout)
            tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()

        elif getattr(self, 'condition', None) == 'href_change':
            if not previous_action:
                raise page_action.PageActionFailed(
                    'You need to perform an action '
                    'before waiting for a href change.')
            previous_action.WillRunAction(page, tab)
            old_url = tab.EvaluateJavaScript('document.location.href')
            previous_action.RunAction(page, tab, None)
            tab.WaitForJavaScriptExpression(
                'document.location.href != "%s"' % old_url, self.timeout)

        elif getattr(self, 'condition', None) == 'element':
            if hasattr(self, 'text'):
                callback_code = 'function(element) { return element != null; }'
                util.WaitFor(
                    lambda: util.FindElementAndPerformAction(
                        tab, self.text, callback_code), self.timeout)
            elif hasattr(self, 'selector'):
                tab.WaitForJavaScriptExpression(
                    'document.querySelector("%s") != null' %
                    re.escape(self.selector), self.timeout)
            elif hasattr(self, 'xpath'):
                code = ('document.evaluate("%s",'
                        'document,'
                        'null,'
                        'XPathResult.FIRST_ORDERED_NODE_TYPE,'
                        'null)'
                        '.singleNodeValue' % re.escape(self.xpath))
                tab.WaitForJavaScriptExpression('%s != null' % code,
                                                self.timeout)
            else:
                raise page_action.PageActionFailed(
                    'No element condition given to wait')
        elif hasattr(self, 'javascript'):
            tab.WaitForJavaScriptExpression(self.javascript, self.timeout)
        else:
            raise page_action.PageActionFailed('No wait condition found')

        tab.ExecuteJavaScript('console.timeEnd("' +
                              self.GetTimelineMarkerName() + '")')
Example #4
0
 def DoClick():
     assert hasattr(self, 'selector') or hasattr(self, 'text')
     if hasattr(self, 'selector'):
         code = 'document.querySelector(\'' + self.selector + '\').click();'
         try:
             tab.ExecuteJavaScript(code)
         except exceptions.EvaluateException:
             raise page_action.PageActionFailed(
                 'Cannot find element with selector ' + self.selector)
     else:
         callback_code = 'function(element) { element.click(); }'
         try:
             util.FindElementAndPerformAction(tab, self.text,
                                              callback_code)
         except exceptions.EvaluateException:
             raise page_action.PageActionFailed(
                 'Cannot find element with text ' + self.text)
Example #5
0
    def RunAction(self, page, tab, previous_action):
        assert hasattr(self, 'condition')

        if self.condition == 'duration':
            assert hasattr(self, 'seconds')
            time.sleep(self.seconds)

        elif self.condition == 'navigate':
            if not previous_action:
                raise page_action.PageActionFailed(
                    'You need to perform an action '
                    'before waiting for navigate.')
            previous_action.WillRunAction()
            action_to_perform = lambda: previous_action.RunAction(
                page, tab, None)
            tab.PerformActionAndWaitForNavigate(action_to_perform)

        elif self.condition == 'href_change':
            if not previous_action:
                raise page_action.PageActionFailed(
                    'You need to perform an action '
                    'before waiting for a href change.')
            previous_action.WillRunAction()
            old_url = tab.EvaluateJavaScript('document.location.href')
            previous_action.RunAction(page, tab, None)
            util.WaitFor(
                lambda: tab.EvaluateJavaScript('document.location.href') !=
                old_url, self.DEFAULT_TIMEOUT)

        elif self.condition == 'element':
            assert hasattr(self, 'text') or hasattr(self, 'selector')
            if self.text:
                callback_code = 'function(element) { return element != null; }'
                util.WaitFor(
                    lambda: util.FindElementAndPerformAction(
                        tab, self.text, callback_code), self.DEFAULT_TIMEOUT)
            elif self.selector:
                util.WaitFor(
                    lambda: tab.
                    EvaluateJavaScript('document.querySelector("%s") != null' %
                                       self.selector), self.DEFAULT_TIMEOUT)

        elif self.condition == 'javascript':
            assert hasattr(self, 'javascript')
            util.WaitFor(lambda: tab.EvaluateJavaScript(self.javascript),
                         self.DEFAULT_TIMEOUT)
Example #6
0
  def WaitForPageToLoad(obj, tab, timeout, poll_interval=0.1):
    """Waits for various wait conditions present in obj."""
    if hasattr(obj, 'post_navigate_javascript_to_execute'):
      tab.EvaluateJavaScript(obj.post_navigate_javascript_to_execute)

    if hasattr(obj, 'wait_seconds'):
      time.sleep(obj.wait_seconds)
    if hasattr(obj, 'wait_for_element_with_text'):
      callback_code = 'function(element) { return element != null; }'
      util.WaitFor(
          lambda: util.FindElementAndPerformAction(
              tab, obj.wait_for_element_with_text, callback_code),
          timeout, poll_interval)
    if hasattr(obj, 'wait_for_element_with_selector'):
      util.WaitFor(lambda: tab.EvaluateJavaScript(
           'document.querySelector(\'' + obj.wait_for_element_with_selector +
           '\') != null'), timeout, poll_interval)
    if hasattr(obj, 'wait_for_javascript_expression'):
      util.WaitFor(
          lambda: tab.EvaluateJavaScript(obj.wait_for_javascript_expression),
          timeout, poll_interval)
Example #7
0
    def RunAction(self, page, tab):
        assert not self._RunsPreviousAction(), \
            ('"navigate" and "href_change" support for wait is deprecated, use '
             'wait_until instead')
        tab.ExecuteJavaScript('console.time("' +
                              self._GetUniqueTimelineMarkerName() + '")')

        if hasattr(self, 'seconds'):
            time.sleep(self.seconds)
        elif getattr(self, 'condition', None) == 'element':
            if hasattr(self, 'text'):
                callback_code = 'function(element) { return element != null; }'
                util.WaitFor(
                    lambda: util.FindElementAndPerformAction(
                        tab, self.text, callback_code), self.timeout)
            elif hasattr(self, 'selector'):
                tab.WaitForJavaScriptExpression(
                    'document.querySelector("%s") != null' %
                    re.escape(self.selector), self.timeout)
            elif hasattr(self, 'xpath'):
                code = ('document.evaluate("%s",'
                        'document,'
                        'null,'
                        'XPathResult.FIRST_ORDERED_NODE_TYPE,'
                        'null)'
                        '.singleNodeValue' % re.escape(self.xpath))
                tab.WaitForJavaScriptExpression('%s != null' % code,
                                                self.timeout)
            else:
                raise page_action.PageActionFailed(
                    'No element condition given to wait')
        elif hasattr(self, 'javascript'):
            tab.WaitForJavaScriptExpression(self.javascript, self.timeout)
        else:
            raise page_action.PageActionFailed('No wait condition found')

        tab.ExecuteJavaScript('console.timeEnd("' +
                              self._GetUniqueTimelineMarkerName() + '")')