def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = 'document.body' gesture_source_type = page_action.GetGestureSourceTypeFromOptions(tab) if self._use_touch: gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__scrollAction.start({ element: element, left_start_ratio: %s, top_start_ratio: %s, direction: '%s', speed: %s, gesture_source_type: %s }); }''' % (self._left_start_ratio, self._top_start_ratio, self._direction, self._speed, gesture_source_type) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__scrollActionDone', 60)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = '(document.scrollingElement || document.body)' gesture_source_type = self._synthetic_gesture_source if self._use_touch: gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' # TODO(catapult:#3028): Fix interpolation of JavaScript values. code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__scrollAction.start({ element: element, left_start_ratio: %s, top_start_ratio: %s, direction: '%s', speed: %s, gesture_source_type: %s }); }''' % (self._left_start_ratio, self._top_start_ratio, self._direction, self._speed, gesture_source_type) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__scrollActionDone', 60)
def RunAction(self, tab): if not self.HasElementSelector(): self.element_function = 'document.body' gesture_source_type = page_action.GetGestureSourceTypeFromOptions(tab) tap_cmd = (''' window.__tapAction.start({ element: element, left_position_percentage: %s, top_position_percentage: %s, duration_ms: %s, gesture_source_type: %s });''' % (self.left_position_percentage, self.top_position_percentage, self.duration_ms, gesture_source_type)) code = ''' function(element, errorMsg) { if (!element) { throw Error('Cannot find element: ' + errorMsg); } %s; }''' % tap_cmd page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function) tab.WaitForJavaScriptExpression('window.__tapActionDone', 60)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = '(document.scrollingElement || document.body)' code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__swipeAction.start({ element: element, left_start_ratio: %s, top_start_ratio: %s, direction: '%s', distance: %s, speed: %s }); }''' % (self._left_start_ratio, self._top_start_ratio, self._direction, self._distance, self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__swipeActionDone', 60)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = '(document.scrollingElement || document.body)' gesture_source_type = self._synthetic_gesture_source if self._use_touch: gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' code = js_template.Render(''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__scrollAction.start({ element: element, left_start_ratio: {{ left_start_ratio }}, top_start_ratio: {{ top_start_ratio }}, direction: {{ direction }}, speed: {{ speed }}, gesture_source_type: {{ @gesture_source_type }} }); }''', left_start_ratio=self._left_start_ratio, top_start_ratio=self._top_start_ratio, direction=self._direction, speed=self._speed, gesture_source_type=gesture_source_type) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptCondition('window.__scrollActionDone', timeout=60)
def RunAction(self, tab): # TODO(catapult:#3028): Fix interpolation of JavaScript values. code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__scrollBounceAction.start({ element: element, left_start_ratio: %s, top_start_ratio: %s, direction: '%s', distance: %s, overscroll: %s, repeat_count: %s, speed: %s }); }''' % (self._left_start_ratio, self._top_start_ratio, self._direction, self._distance, self._overscroll, self._repeat_count, self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__scrollBounceActionDone', 60)
def RunAction(self, tab): if not self.HasElementSelector(): self.element_function = 'document.body' tap_cmd = (''' window.__tapAction.start({ element: element, left_position_percentage: %s, top_position_percentage: %s, duration_ms: %s, gesture_source_type: %s });''' % (self.left_position_percentage, self.top_position_percentage, self.duration_ms, self._synthetic_gesture_source)) code = ''' function(element, errorMsg) { if (!element) { throw Error('Cannot find element: ' + errorMsg); } %s; }''' % tap_cmd page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function) # The second disjunct handles the case where the tap action leads to an # immediate navigation (in which case the expression below might already be # evaluated on the new page). tab.WaitForJavaScriptExpression( 'window.__tapActionDone || window.__tapAction === undefined', 60)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = 'document.body' gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' if (page_action.IsGestureSourceTypeSupported(tab, 'mouse') and not self._use_touch): gesture_source_type = 'chrome.gpuBenchmarking.MOUSE_INPUT' code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__dragAction.start({ element: element, left_start_ratio: %s, top_start_ratio: %s, left_end_ratio: %s, top_end_ratio: %s, speed: %s, gesture_source_type: %s }); }''' % (self._left_start_ratio, self._top_start_ratio, self._left_end_ratio, self._top_end_ratio, self._speed, gesture_source_type) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__dragActionDone', 60)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = '(document.scrollingElement || document.body)' code = js_template.Render(""" function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__swipeAction.start({ element: element, left_start_ratio: {{ left_start_ratio }}, top_start_ratio: {{ top_start_ratio }}, direction: {{ direction }}, distance: {{ distance }}, speed: {{ speed }} }); }""", left_start_ratio=self._left_start_ratio, top_start_ratio=self._top_start_ratio, direction=self._direction, distance=self._distance, speed=self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptCondition('window.__swipeActionDone', timeout=60)
def RunAction(self, tab): if not self.HasElementSelector(): self.element_function = 'document.body' code = js_template.Render(''' function(element, errorMsg) { if (!element) { throw Error('Cannot find element: ' + errorMsg); } window.__tapAction.start({ element: element, left_position_percentage: {{ left_position_percentage }}, top_position_percentage: {{ top_position_percentage }}, duration_ms: {{ duration_ms }}, gesture_source_type: {{ @gesture_source_type }} }); }''', left_position_percentage=self.left_position_percentage, top_position_percentage=self.top_position_percentage, duration_ms=self.duration_ms, gesture_source_type=self._synthetic_gesture_source) page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function) # The second disjunct handles the case where the tap action leads to an # immediate navigation (in which case the expression below might already be # evaluated on the new page). tab.WaitForJavaScriptCondition2( 'window.__tapActionDone || window.__tapAction === undefined', timeout=60)
def RunAction(self, tab): scale_factor = (self._scale_factor if self._scale_factor else PinchAction._GetDefaultScaleFactorForPage(tab)) code = js_template.Render(''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__pinchAction.start({ element: element, left_anchor_ratio: {{ left_anchor_ratio }}, top_anchor_ratio: {{ top_anchor_ratio }}, scale_factor: {{ scale_factor }}, speed: {{ speed }} }); }''', left_anchor_ratio=self._left_anchor_ratio, top_anchor_ratio=self._top_anchor_ratio, scale_factor=scale_factor, speed=self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60)
def RunAction(self, tab): code = js_template.Render( """ function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__scrollBounceAction.start({ element: element, left_start_ratio: {{ left_start_ratio }}, top_start_ratio: {{ top_start_ratio }}, direction: {{ direction }}, distance: {{ distance }}, overscroll: {{ overscroll }}, repeat_count: {{ repeat_count }}, speed: {{ speed }} }); }""", left_start_ratio=self._left_start_ratio, top_start_ratio=self._top_start_ratio, direction=self._direction, distance=self._distance, overscroll=self._overscroll, repeat_count=self._repeat_count, speed=self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptCondition( 'window.__scrollBounceActionDone', timeout=60)
def RunAction(self, tab): code = ''' function(element, errorMsg) { if (!element) { throw Error('Cannot find element: ' + errorMsg); } element.click(); }''' page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function)
def testEvaluateCallbackWithElement(self): self.Navigate('blank.html') runner = action_runner.ActionRunner(self._tab) runner.ExecuteJavaScript(''' (function() { function createElement(id, textContent) { var el = document.createElement("div"); el.id = id; el.textContent = textContent; document.body.appendChild(el); } createElement('test-1', 'foo'); createElement('test-2', 'bar'); createElement('test-3', 'baz'); })();''') self.assertEqual( 'foo', page_action.EvaluateCallbackWithElement( self._tab, 'function(el) { return el.textContent; }', selector='#test-1')) self.assertEqual( 'bar', page_action.EvaluateCallbackWithElement( self._tab, 'function(el) { return el.textContent; }', text='bar')) self.assertEqual( 'baz', page_action.EvaluateCallbackWithElement( self._tab, 'function(el) { return el.textContent; }', element_function='document.getElementById("test-3")')) self.assertEqual( 'baz', page_action.EvaluateCallbackWithElement( self._tab, 'function(el) { return el.textContent; }', element_function=''' (function() { return document.getElementById("test-3"); })()''')) # Test for when the element is not found. self.assertEqual( None, page_action.EvaluateCallbackWithElement( self._tab, 'function(el) { return el; }', element_function='document.getElementById("test-4")')) # Test the info message. self.assertEqual( 'using selector "#test-1"', page_action.EvaluateCallbackWithElement( self._tab, 'function(el, info) { return info; }', selector='#test-1'))
def RunAction(self, tab): code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__mouseClickAction.start({ element: element }); }''' page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector) tab.WaitForJavaScriptCondition2('window.__mouseClickActionDone', timeout=60)
def RunAction(self, tab): code = 'function(element) { return element != null; }' try: page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function, wait=True, timeout_in_seconds=self.timeout_in_seconds) 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 RunAction(self, tab): code = ''' function(element, errorMsg) { if (!element) { throw Error('Cannot find element: ' + errorMsg); } element.click(); }''' # Click handler that plays media or requests fullscreen may not take # effects without user_gesture set to True. page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function, user_gesture=True)
def RunAction(self, tab): if (self._selector is None and self._text is None and self._element_function is None): self._element_function = 'document.body' gesture_source_type = 'chrome.gpuBenchmarking.TOUCH_INPUT' if (page_action.IsGestureSourceTypeSupported(tab, 'mouse') and not self._use_touch): gesture_source_type = 'chrome.gpuBenchmarking.MOUSE_INPUT' code = js_template.Render( """ function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__dragAction.start({ element: element, left_start_ratio: {{ left_start_ratio }}, top_start_ratio: {{ top_start_ratio }}, left_end_ratio: {{ left_end_ratio }}, top_end_ratio: {{ top_end_ratio }}, speed: {{ speed }}, gesture_source_type: {{ @gesture_source_type }} }); }""", left_start_ratio=self._left_start_ratio, top_start_ratio=self._top_start_ratio, left_end_ratio=self._left_end_ratio, top_end_ratio=self._top_end_ratio, speed=self._speed, gesture_source_type=gesture_source_type) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptCondition('window.__dragActionDone', timeout=60)
def RunAction(self, tab): scale_factor = (self._scale_factor if self._scale_factor else PinchAction._GetDefaultScaleFactorForPage(tab)) # TODO(catapult:#3028): Fix interpolation of JavaScript values. code = ''' function(element, info) { if (!element) { throw Error('Cannot find element: ' + info); } window.__pinchAction.start({ element: element, left_anchor_ratio: %s, top_anchor_ratio: %s, scale_factor: %s, speed: %s }); }''' % (self._left_anchor_ratio, self._top_anchor_ratio, scale_factor, self._speed) page_action.EvaluateCallbackWithElement( tab, code, selector=self._selector, text=self._text, element_function=self._element_function) tab.WaitForJavaScriptExpression('window.__pinchActionDone', 60)
def RunAction(self, tab): code = 'function(element) { return element != null; }' page_action.EvaluateCallbackWithElement( tab, code, selector=self.selector, text=self.text, element_function=self.element_function, wait=True, timeout_in_seconds=self.timeout_in_seconds)
def Evaluate2(): page_action.EvaluateCallbackWithElement(self._tab, 'function() {}', selector='div', element_function='foo')
def Evaluate3(): page_action.EvaluateCallbackWithElement(self._tab, 'function() {}', text='foo', element_function='')