diff options
author | edmundyan@chromium.org <edmundyan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-24 15:11:13 +0000 |
---|---|---|
committer | edmundyan@chromium.org <edmundyan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-24 15:11:13 +0000 |
commit | 1357025daea3e1993fab23375dc888f40f79ca11 (patch) | |
tree | a473736bcb0abc02b74589b06e4cf39c5678a47b | |
parent | 5f191845cec2a7ca702e8d495a93abf40ac9f2d4 (diff) | |
download | chromium_src-1357025daea3e1993fab23375dc888f40f79ca11.zip chromium_src-1357025daea3e1993fab23375dc888f40f79ca11.tar.gz chromium_src-1357025daea3e1993fab23375dc888f40f79ca11.tar.bz2 |
Adding xpath selectors to wait/click_element actions
BUG=278465
TEST=tools/telemetry/run_tests ClickElementActionTest.testClickWithXPathWaitForRefChange
Review URL: https://chromiumcodereview.appspot.com/22966013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@219450 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 49 insertions, 5 deletions
diff --git a/tools/telemetry/telemetry/page/actions/click_element.py b/tools/telemetry/telemetry/page/actions/click_element.py index 8d4aa79..7891153 100644 --- a/tools/telemetry/telemetry/page/actions/click_element.py +++ b/tools/telemetry/telemetry/page/actions/click_element.py @@ -1,6 +1,9 @@ # Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. + +import re + from telemetry.core import util from telemetry.core import exceptions from telemetry.page import page as page_module @@ -12,7 +15,6 @@ class ClickElementAction(page_action.PageAction): def RunAction(self, page, tab, previous_action): def DoClick(): - assert hasattr(self, 'selector') or hasattr(self, 'text') if hasattr(self, 'selector'): code = 'document.querySelector(\'' + self.selector + '\').click();' try: @@ -20,13 +22,28 @@ class ClickElementAction(page_action.PageAction): except exceptions.EvaluateException: raise page_action.PageActionFailed( 'Cannot find element with selector ' + self.selector) - else: + 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 click_element') if hasattr(self, 'wait_for_navigate'): tab.PerformActionAndWaitForNavigate(DoClick) diff --git a/tools/telemetry/telemetry/page/actions/click_element_unittest.py b/tools/telemetry/telemetry/page/actions/click_element_unittest.py index a32baec..c50d8c9 100644 --- a/tools/telemetry/telemetry/page/actions/click_element_unittest.py +++ b/tools/telemetry/telemetry/page/actions/click_element_unittest.py @@ -40,3 +40,20 @@ class ClickElementActionTest(tab_test_case.TabTestCase): self.assertEquals( self._tab.EvaluateJavaScript('document.location.pathname;'), '/blank.html') + + def testClickWithXPathWaitForRefChange(self): + self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir()) + self._tab.Navigate( + self._browser.http_server.UrlOf('page_with_link.html')) + self._tab.WaitForDocumentReadyStateToBeComplete() + self.assertEquals( + self._tab.EvaluateJavaScript('document.location.pathname;'), + '/page_with_link.html') + + data = {'xpath': '//a[@id="clickme"]', 'wait_for_href_change': True} + i = click_element.ClickElementAction(data) + i.RunAction(None, self._tab, None) + + self.assertEquals( + self._tab.EvaluateJavaScript('document.location.pathname;'), + '/blank.html') diff --git a/tools/telemetry/telemetry/page/actions/wait.py b/tools/telemetry/telemetry/page/actions/wait.py index 2210a2d..d610cd4 100644 --- a/tools/telemetry/telemetry/page/actions/wait.py +++ b/tools/telemetry/telemetry/page/actions/wait.py @@ -43,17 +43,27 @@ class WaitAction(page_action.PageAction): 'document.location.href') != old_url, self.timeout) elif self.condition == 'element': - assert hasattr(self, 'text') or hasattr(self, 'selector') if hasattr(self, 'text'): callback_code = 'function(element) { return element != null; }' util.WaitFor( lambda: util.FindElementAndPerformAction( tab, self.text, callback_code), self.timeout) - else: + elif hasattr(self, 'selector'): util.WaitFor(lambda: tab.EvaluateJavaScript( '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)) + util.WaitFor(lambda: tab.EvaluateJavaScript('%s != null' % code), + self.timeout) + else: + raise page_action.PageActionFailed( + 'No element condition given to wait') elif self.condition == 'javascript': assert hasattr(self, 'javascript') util.WaitFor(lambda: tab.EvaluateJavaScript(self.javascript), |