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')
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() + '")')
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)
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)
def RunAction(self, tab): try: tab.ExecuteJavaScript('window.__loopMedia("%s", %i);' % (self._selector, self._loop_count)) if self._timeout_in_seconds > 0: self.WaitForEvent(tab, self._selector, 'loop', self._timeout_in_seconds) except exceptions.EvaluateException: raise page_action.PageActionFailed( 'Cannot loop media element(s) with ' 'selector = %s.' % self._selector)
def DoTap(): assert hasattr(self, 'find_element_expression') event = 'new CustomEvent("tap", {bubbles: true})' code = '(%s).dispatchEvent(%s)' % (self.find_element_expression, event) try: tab.ExecuteJavaScript(code) except exceptions.EvaluateException: raise page_action.PageActionFailed( 'Cannot find element with code ' + self.find_element_javascript)
def RunAction(self, tab): try: tab.ExecuteJavaScript( 'window.__seekMedia("%s", "%s", %i, "%s");' % (self._selector, self._seconds, self._log_time, self._label)) if self._timeout_in_seconds > 0: self.WaitForEvent(tab, self._selector, 'seeked', self._timeout_in_seconds) except exceptions.EvaluateException: raise page_action.PageActionFailed( 'Cannot seek media element(s) with ' 'selector = %s.' % self._selector)
def RunNavigateSteps(self, page, tab): """Navigates the tab to the page URL attribute. Runs the 'navigate_steps' page attribute as a compound action. """ navigate_actions = GetCompoundActionFromPage(page, 'navigate_steps') if not any(isinstance(action, navigate.NavigateAction) for action in navigate_actions): raise page_action.PageActionFailed( 'No NavigateAction in navigate_steps') self._RunCompoundAction(page, tab, navigate_actions, False)
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() + '")')
def RunAction(self, page, tab): try: selector = self.selector if hasattr(self, 'selector') else '' tab.ExecuteJavaScript('window.__playMedia("%s");' % selector) timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60 # Check if we need to wait for 'playing' event to fire. if hasattr(self, 'wait_for_playing') and self.wait_for_playing: self.WaitForEvent(tab, selector, 'playing', timeout) # Check if we need to wait for 'ended' event to fire. if hasattr(self, 'wait_for_ended') and self.wait_for_ended: self.WaitForEvent(tab, selector, 'ended', timeout) except exceptions.EvaluateException: raise page_action.PageActionFailed('Cannot play media element(s) with ' 'selector = %s.' % selector)
def RunAction(self, tab): try: assert hasattr(self, 'loop_count') and self.loop_count > 0 selector = self.selector if hasattr(self, 'selector') else '' tab.ExecuteJavaScript('window.__loopMedia("%s", %i);' % (selector, self.loop_count)) timeout = (self.wait_timeout if hasattr(self, 'wait_timeout') else 60 * self.loop_count) # Check if there is no need to wait for all loops to end if hasattr(self, 'wait_for_loop') and not self.wait_for_loop: return self.WaitForEvent(tab, selector, 'loop', timeout) except exceptions.EvaluateException: raise page_action.PageActionFailed('Cannot loop media element(s) with ' 'selector = %s.' % selector)
def RunAction(self, tab): try: tab.ExecuteJavaScript('window.__playMedia("%s");' % self._selector) # Check if we need to wait for 'playing' event to fire. if self._playing_event_timeout_in_seconds > 0: self.WaitForEvent(tab, self._selector, 'playing', self._playing_event_timeout_in_seconds) # Check if we need to wait for 'ended' event to fire. if self._ended_event_timeout_in_seconds > 0: self.WaitForEvent(tab, self._selector, 'ended', self._ended_event_timeout_in_seconds) except exceptions.EvaluateException: raise page_action.PageActionFailed( 'Cannot play media element(s) with ' 'selector = %s.' % self._selector)
def RunAction(self, page, tab, previous_action): try: assert hasattr(self, 'seek_time') selector = self.selector if hasattr(self, 'selector') else '' log_seek = self.log_seek == True if hasattr(self, 'log_seek') else True seek_label = self.seek_label if hasattr(self, 'seek_label') else '' tab.ExecuteJavaScript('window.__seekMedia("%s", "%s", %i, "%s");' % (selector, self.seek_time, log_seek, seek_label)) timeout = self.wait_timeout if hasattr(self, 'wait_timeout') else 60 # Check if we need to wait for 'seeked' event to fire. if hasattr(self, 'wait_for_seeked') and self.wait_for_seeked: self.WaitForEvent(tab, selector, 'seeked', timeout) except exceptions.EvaluateException: raise page_action.PageActionFailed('Cannot seek media element(s) with ' 'selector = %s.' % selector)
def _RunCompoundAction(self, page, tab, actions): for i, action in enumerate(actions): prev_action = actions[i - 1] if i > 0 else None next_action = actions[i + 1] if i < len(actions) - 1 else None if (action.RunsPreviousAction() and next_action and next_action.RunsPreviousAction()): raise page_action.PageActionFailed('Consecutive actions cannot both ' 'have RunsPreviousAction() == True.') if not (next_action and next_action.RunsPreviousAction()): action.WillRunAction(page, tab) self.WillRunAction(page, tab, action) try: action.RunAction(page, tab, prev_action) finally: self.DidRunAction(page, tab, action)
def _RunCompoundAction(self, page, tab, actions): for i, action in enumerate(actions): prev_action = actions[i - 1] if i > 0 else None next_action = actions[i + 1] if i < len(actions) - 1 else None if (action.RunsPreviousAction() and next_action and next_action.RunsPreviousAction()): raise page_action.PageActionFailed( 'Consecutive actions cannot both ' 'have RunsPreviousAction() == True.') if not (next_action and next_action.RunsPreviousAction()): action.WillRunAction(page, tab) self.WillRunAction(page, tab, action) try: action.RunAction(page, tab, prev_action) finally: self.DidRunAction(page, tab, action) # Closing the connections periodically is needed; otherwise we won't be # able to open enough sockets, and the pages will time out. util.CloseConnections(tab)