diff options
author | frankf@google.com <frankf@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-17 17:23:26 +0000 |
---|---|---|
committer | frankf@google.com <frankf@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-17 17:23:26 +0000 |
commit | 71d6c2d930dad7233ae49c2f61e91790d70158a5 (patch) | |
tree | f516c1d13db486fc8750381eba66b783e204af4b /chrome/test | |
parent | af764826943765890a0f4e98456393189ce1d6d1 (diff) | |
download | chromium_src-71d6c2d930dad7233ae49c2f61e91790d70158a5.zip chromium_src-71d6c2d930dad7233ae49c2f61e91790d70158a5.tar.gz chromium_src-71d6c2d930dad7233ae49c2f61e91790d70158a5.tar.bz2 |
Refactor javascript injection functions in PyAuto.
- Get rid of tab proxy method and use JSON calls
- tab index should always come before windex
BUG=chromium:100281
TEST=Run all PyAuto tests
Review URL: http://codereview.chromium.org/8277025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105831 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/functional/nacl_sdk.py | 22 | ||||
-rw-r--r-- | chrome/test/functional/passwords.py | 6 | ||||
-rw-r--r-- | chrome/test/functional/pdf.py | 2 | ||||
-rw-r--r-- | chrome/test/functional/perf.py | 10 | ||||
-rw-r--r-- | chrome/test/functional/shortcuts.py | 2 | ||||
-rw-r--r-- | chrome/test/functional/test_utils.py | 10 | ||||
-rw-r--r-- | chrome/test/pyautolib/chromoting.py | 6 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 57 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyautolib.cc | 30 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyautolib.h | 19 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyautolib.i | 16 |
11 files changed, 68 insertions, 112 deletions
diff --git a/chrome/test/functional/nacl_sdk.py b/chrome/test/functional/nacl_sdk.py index 65e4290..6978c91 100644 --- a/chrome/test/functional/nacl_sdk.py +++ b/chrome/test/functional/nacl_sdk.py @@ -547,7 +547,7 @@ class NaClSDKTest(pyauto.PyUITest): success = self.WaitUntil( lambda: self.GetDOMValue( 'document.getElementById("statusField").innerHTML', - 0, tab_index), + tab_index), timeout=60, expect_retval='SUCCESS') self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) @@ -557,15 +557,15 @@ class NaClSDKTest(pyauto.PyUITest): } window.domAutomationController.send("done"); """ - self.ExecuteJavascript(js_code, 0, tab_index) + self.ExecuteJavascript(js_code, tab_index) result = self.ExecuteJavascript('document.helloForm.elements[1].click();', - 0, tab_index) + tab_index) self.assertEqual(result, '42', msg='Example %s failed. URL: %s' % (name, url)) result = self.ExecuteJavascript('document.helloForm.elements[2].click();', - 0, tab_index) + tab_index) self.assertEqual(result, 'dlrow olleH', msg='Example %s failed. URL: %s' % (name, url)) @@ -592,7 +592,7 @@ class NaClSDKTest(pyauto.PyUITest): } while (obj = obj.offsetParent); window.domAutomationController.send(curleft + ", " + curtop); """ - result = self.ExecuteJavascript(js_code, 0, tab_index) + result = self.ExecuteJavascript(js_code, tab_index) result_split = result.split(', ') x = int(result_split[0]) y = int(result_split[1]) @@ -618,14 +618,14 @@ class NaClSDKTest(pyauto.PyUITest): success = self.WaitUntil( lambda: self.GetDOMValue( 'document.getElementById("frequency_field").value', - 0, tab_index), + tab_index), timeout=30, expect_retval='440') self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) self.ExecuteJavascript( 'document.body.getElementsByTagName("button")[0].click();' 'window.domAutomationController.send("done")', - 0, tab_index) + tab_index) # TODO(chrisphan): Verify sound. @@ -640,14 +640,14 @@ class NaClSDKTest(pyauto.PyUITest): success = self.WaitUntil( lambda: self.GetDOMValue( 'document.getElementById("status_field").innerHTML', - 0, tab_index), + tab_index), timeout=60, expect_retval='SUCCESS') self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) self.ExecuteJavascript( 'document.geturl_form.elements[0].click();' 'window.domAutomationController.send("done")', - 0, tab_index) + tab_index) js_code = """ var output = document.getElementById("general_output").innerHTML; @@ -659,7 +659,7 @@ class NaClSDKTest(pyauto.PyUITest): window.domAutomationController.send(result); """ success = self.WaitUntil( - lambda: self.ExecuteJavascript(js_code, 0, tab_index), + lambda: self.ExecuteJavascript(js_code, tab_index), timeout=30, expect_retval='pass') self.assertTrue(success, msg='Example %s failed. URL: %s' % (name, url)) @@ -815,7 +815,7 @@ class NaClSDKTest(pyauto.PyUITest): tabs = info['windows'][0]['tabs'] for tab in tabs: if tab['url'] != 'about:blank': - if not self.GetDOMValue('document.body.innerHTML', 0, tab['index']): + if not self.GetDOMValue('document.body.innerHTML', tab['index']): self.fail(msg='Tab crashed on %s' % tab['url']) # TODO(chrisphan): Check for tab hangs and browser hangs. diff --git a/chrome/test/functional/passwords.py b/chrome/test/functional/passwords.py index cbd7d73..615fca5 100644 --- a/chrome/test/functional/passwords.py +++ b/chrome/test/functional/passwords.py @@ -62,7 +62,7 @@ class PasswordTest(pyauto.PyUITest): } window.domAutomationController.send("done"); """ - self.ExecuteJavascript(click_js, window_index, tab_index) + self.ExecuteJavascript(click_js, tab_index, window_index) # Wait until username/password is filled by the Password manager on the # login page. @@ -75,9 +75,9 @@ class PasswordTest(pyauto.PyUITest): """ self.assertTrue(self.WaitUntil( lambda: self.ExecuteJavascript(js_template % 'Email', - window_index, tab_index) != '' and + tab_index, window_index) != '' and self.ExecuteJavascript(js_template % 'Passwd', - window_index, tab_index) != '')) + tab_index, window_index) != '')) def testSavePassword(self): """Test saving a password and getting saved passwords.""" diff --git a/chrome/test/functional/pdf.py b/chrome/test/functional/pdf.py index 800ca99..f65af12 100644 --- a/chrome/test/functional/pdf.py +++ b/chrome/test/functional/pdf.py @@ -28,7 +28,7 @@ class PDFTest(pyauto.PyUITest): js = 'document.getElementsByName("plugin")[0].%s()' % action # Add an empty string so that there's something to return back # (or else it hangs) - return self.GetDOMValue('%s + ""' % js, 0, tab_index) + return self.GetDOMValue('%s + ""' % js, tab_index) def testPDFRunner(self): diff --git a/chrome/test/functional/perf.py b/chrome/test/functional/perf.py index 80a0224..ae66ca8 100644 --- a/chrome/test/functional/perf.py +++ b/chrome/test/functional/perf.py @@ -217,10 +217,10 @@ class BenchmarkPerfTest(BasePerfTest): """ self.assertTrue( self.WaitUntil( - lambda: 'Score:' in self.ExecuteJavascript(js, 0, 1), timeout=300, + lambda: 'Score:' in self.ExecuteJavascript(js, 1), timeout=300, expect_retval=True, retry_sleep=0.10), msg='Timed out when waiting for v8 benchmark score.') - val = self.ExecuteJavascript(js, 0, 1) # Window index 0, tab index 1. + val = self.ExecuteJavascript(js, 1) # Window index 0, tab index 1. score = int(val.split(':')[1].strip()) self._PrintSummaryResults('V8Benchmark', [score], 'score') @@ -260,7 +260,7 @@ class LiveWebappLoadTest(BasePerfTest): } window.domAutomationController.send("false"); """ % EXPECTED_SUBSTRING - return self.ExecuteJavascript(js, 0, 1) # Window index 0, tab index 1. + return self.ExecuteJavascript(js, 1) # Window index 0, tab index 1. def _RunSingleGmailTabOpen(): self._AppendTab('http://www.gmail.com') @@ -289,7 +289,7 @@ class LiveWebappLoadTest(BasePerfTest): } window.domAutomationController.send("false"); """ % EXPECTED_SUBSTRING - return self.ExecuteJavascript(js, 0, 1) # Window index 0, tab index 1. + return self.ExecuteJavascript(js, 1) # Window index 0, tab index 1. def _RunSingleCalendarTabOpen(): self._AppendTab('http://calendar.google.com') @@ -317,7 +317,7 @@ class LiveWebappLoadTest(BasePerfTest): } window.domAutomationController.send("false"); """ % EXPECTED_SUBSTRING - return self.ExecuteJavascript(js, 0, 1) # Window index 0, tab index 1. + return self.ExecuteJavascript(js, 1) # Window index 0, tab index 1. def _RunSingleDocsTabOpen(): self._AppendTab('http://docs.google.com') diff --git a/chrome/test/functional/shortcuts.py b/chrome/test/functional/shortcuts.py index 6d8735f..cf420eb 100644 --- a/chrome/test/functional/shortcuts.py +++ b/chrome/test/functional/shortcuts.py @@ -96,7 +96,7 @@ class ShortcutsTest(pyauto.PyUITest): self.ExecuteJavascript( 'var element = document.getElementById("clearBrowserDataOverlay");' 'if(element) window.domAutomationController.send(element.nodeName);' - 'else window.domAutomationController.send("")', 0, 1), + 'else window.domAutomationController.send("")', 1), expect_retval='DIV'), msg='Could not find the DOM UI window element.') def testViewSourceShortcut(self): diff --git a/chrome/test/functional/test_utils.py b/chrome/test/functional/test_utils.py index df1b9ee..271bec5 100644 --- a/chrome/test/functional/test_utils.py +++ b/chrome/test/functional/test_utils.py @@ -81,11 +81,11 @@ def GoogleAccountsLogin(test, username, password, tab_index=0, windex=0): 'window.domAutomationController.send("done")' % username password = 'document.getElementById("Passwd").value = "%s"; ' \ 'window.domAutomationController.send("done")' % password - test.ExecuteJavascript(email_id, windex, tab_index); - test.ExecuteJavascript(password, windex, tab_index); + test.ExecuteJavascript(email_id, tab_index, windex) + test.ExecuteJavascript(password, tab_index, windex) test.ExecuteJavascript('document.getElementById("gaia_loginform").submit();' 'window.domAutomationController.send("done")', - windex, tab_index) + tab_index, windex) def VerifyGoogleAccountCredsFilled(test, username, password, tab_index=0, @@ -100,9 +100,9 @@ def VerifyGoogleAccountCredsFilled(test, username, password, tab_index=0, windex: The window index, default is 0. """ email_value = test.GetDOMValue('document.getElementById("Email").value', - windex, tab_index) + tab_index, windex) passwd_value = test.GetDOMValue('document.getElementById("Passwd").value', - windex, tab_index) + tab_index, windex) test.assertEqual(email_value, username) # Not using assertEqual because if it fails it would end up dumping the # password (which is supposed to be private) diff --git a/chrome/test/pyautolib/chromoting.py b/chrome/test/pyautolib/chromoting.py index 6ded0ba..8d642d6 100644 --- a/chrome/test/pyautolib/chromoting.py +++ b/chrome/test/pyautolib/chromoting.py @@ -27,7 +27,7 @@ class ChromotingMixIn(object): """Helper that returns immediately after running a Javascript command.""" return self.ExecuteJavascript( '%s; window.domAutomationController.send("done");' % command, - windex, tab_index) + tab_index, windex) def _WaitForJavascriptCondition(self, condition, tab_index, windex): """Waits until the Javascript condition is true. @@ -36,7 +36,7 @@ class ChromotingMixIn(object): because it uses Javascript to check the condition instead of Python. """ return self.WaitUntil(lambda: self.GetDOMValue( - '(%s) ? "1" : ""' % condition, windex, tab_index)) + '(%s) ? "1" : ""' % condition, tab_index, windex)) def _ExecuteAndWaitForMode(self, command, mode, tab_index, windex): self.assertTrue(self._ExecuteJavascript(command, tab_index, windex), @@ -157,7 +157,7 @@ class ChromotingMixIn(object): 'HOST_WAITING_FOR_CONNECTION', tab_index, windex) return self.GetDOMValue( 'document.getElementById("access-code-display").innerText', - windex, tab_index) + tab_index, windex) def Connect(self, access_code, tab_index=1, windex=0): """Connects to a Chromoting host and starts the session. diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 9ace2a1..2ea3f11 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -710,7 +710,7 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): def __init__(self, ui_test, tab_index=0, windex=0, frame_xpath=''): """Initialize. - Refer to ExecuteJavascriptInTab() for the complete argument list + Refer to ExecuteJavascript() for the complete argument list description. Args: @@ -723,10 +723,10 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): def Execute(self, script): """Execute script in the tab.""" - return self._ui_test.ExecuteJavascriptInTab(script, - self.tab_index, - self.windex, - self.frame_xpath) + return self._ui_test.ExecuteJavascript(script, + self.tab_index, + self.windex, + self.frame_xpath) class JavascriptExecutorInRenderView(JavascriptExecutor): """Wrapper for injecting JavaScript in an extension view.""" @@ -2485,16 +2485,18 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): } return self._GetResultFromJSONRequest(cmd_dict, windex=windex) - def ExecuteJavascriptInTab(self, js, tab_index=0, windex=0, frame_xpath=''): + def ExecuteJavascript(self, js, tab_index=0, windex=0, frame_xpath=''): """Executes a script in the specified frame of a tab. - The invoked javascript function must send a result back via the - domAutomationController.send function, or this function will never return. + By default, execute the script in the top frame of the first tab in the + first window. The invoked javascript function must send a result back via + the domAutomationController.send function, or this function will never + return. Args: - js: script to be executed - windex: index of the window - tab_index: index of the tab + js: script to be executed. + windex: index of the window. + tab_index: index of the tab. frame_xpath: XPath of the frame to execute the script. Default is no frame. Example: '//frames[1]'. @@ -2524,9 +2526,9 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): domAutomationController.send function, or this function will never return. Args: - js: script to be executed + js: script to be executed. view: A dictionary representing a unique id for the render view as - returned for example by + returned for example by. self.GetBrowserInfo()['extension_views'][]['view']. Example: { 'render_process_id': 1, @@ -2554,6 +2556,25 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): json_string = '[' + result + ']' return json.loads(json_string)[0] + def GetDOMValue(self, expr, tab_index=0, windex=0, frame_xpath=''): + """Executes a Javascript expression and returns the value. + + This is a wrapper for ExecuteJavascript, eliminating the need to + explicitly call domAutomationController.send function. + + Args: + expr: expression value to be returned. + tab_index: index of the tab. + windex: index of the window. + frame_xpath: XPath of the frame to execute the script. Default is no + frame. Example: '//frames[1]'. + + Returns: + a string that was sent back via the domAutomationController.send method. + """ + js = 'window.domAutomationController.send(%s);' % expr + return self.ExecuteJavascript(js, tab_index, windex, frame_xpath) + def CallJavascriptFunc(self, function, args=[], tab_index=0, windex=0): """Executes a script which calls a given javascript function. @@ -2563,11 +2584,11 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): Defaults to first tab in first window. Args: - function: name of the function + function: name of the function. args: list of all the arguments to pass into the called function. These should be able to be converted to a string using the |str| function. - tab_index: index of the tab within the given window - windex: index of the window + tab_index: index of the tab within the given window. + windex: index of the window. Returns: a string that was sent back via the domAutomationController.send method @@ -2575,7 +2596,7 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): converted_args = map(lambda arg: json.dumps(arg), args) js = '%s(%s)' % (function, ', '.join(converted_args)) logging.debug('Executing javascript: %s', js) - return self.ExecuteJavascript(js, windex, tab_index) + return self.ExecuteJavascript(js, tab_index, windex) def JavascriptFocusElementById(self, field_id, tab_index=0, windex=0): """Uses Javascript to focus an element with the given ID in a webpage. @@ -2598,7 +2619,7 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): window.domAutomationController.send("done"); } """ % field_id - return self.ExecuteJavascript(focus_field_js, windex, tab_index) == 'done' + return self.ExecuteJavascript(focus_field_js, tab_index, windex) == 'done' def SignInToSync(self, username, password): """Signs in to sync using the given username and password. diff --git a/chrome/test/pyautolib/pyautolib.cc b/chrome/test/pyautolib/pyautolib.cc index d9fadfc..01de333 100644 --- a/chrome/test/pyautolib/pyautolib.cc +++ b/chrome/test/pyautolib/pyautolib.cc @@ -347,36 +347,6 @@ std::string PyUITestBase::_SendJSONRequest(int window_index, return response; } -std::wstring PyUITestBase::ExecuteJavascript(const std::wstring& script, - int window_index, - int tab_index, - const std::wstring& frame_xpath) { - scoped_refptr<BrowserProxy> browser_proxy = - automation()->GetBrowserWindow(window_index); - EXPECT_TRUE(browser_proxy.get()); - std::wstring response; - if (!browser_proxy.get()) - return response; - scoped_refptr<TabProxy> tab_proxy = - browser_proxy->GetTab(tab_index); - EXPECT_TRUE(tab_proxy.get()); - if (!tab_proxy.get()) - return response; - - EXPECT_TRUE(tab_proxy->ExecuteAndExtractString(frame_xpath, script, - &response)); - return response; -} - -std::wstring PyUITestBase::GetDOMValue(const std::wstring& expr, - int window_index, - int tab_index, - const std::wstring& frame_xpath) { - std::wstring script = std::wstring(L"window.domAutomationController.send(") + - expr + std::wstring(L")"); - return ExecuteJavascript(script, window_index, tab_index, frame_xpath); -} - bool PyUITestBase::ResetToDefaultTheme() { return automation()->ResetToDefaultTheme(); } diff --git a/chrome/test/pyautolib/pyautolib.h b/chrome/test/pyautolib/pyautolib.h index 1c21827..891023a 100644 --- a/chrome/test/pyautolib/pyautolib.h +++ b/chrome/test/pyautolib/pyautolib.h @@ -173,25 +173,6 @@ class PyUITestBase : public UITestBase { const std::string& request, int timeout); - // Execute javascript in a given tab, and return the response. This is - // a low-level method intended for use mostly by GetDOMValue(). Note that - // any complicated manipulation of the page should be done by something - // like WebDriver, not PyAuto. Also note that in order for the script to - // return a value to the calling code, it invokes - // window.domAutomationController.send(), passing in the intended return - // value. - std::wstring ExecuteJavascript(const std::wstring& script, - int window_index = 0, - int tab_index = 0, - const std::wstring& frame_xpath = L""); - - // Evaluate a Javascript expression and return the result as a string. This - // method is intended largely to read values out of the frame DOM. - std::wstring GetDOMValue(const std::wstring& expr, - int window_index = 0, - int tab_index = 0, - const std::wstring& frame_xpath = L""); - // Resets to the default theme. Returns true on success. bool ResetToDefaultTheme(); diff --git a/chrome/test/pyautolib/pyautolib.i b/chrome/test/pyautolib/pyautolib.i index 91d6023..cb0b41b 100644 --- a/chrome/test/pyautolib/pyautolib.i +++ b/chrome/test/pyautolib/pyautolib.i @@ -392,22 +392,6 @@ class PyUITestBase { const std::string& request, int timeout); - %feature("docstring", "Execute a string of javascript in the specified " - "(window, tab, frame) and return a string.") ExecuteJavascript; - std::wstring ExecuteJavascript(const std::wstring& script, - int window_index=0, - int tab_index=0, - const std::wstring& frame_xpath=""); - - %feature("docstring", "Evaluate a javascript expression in the specified " - "(window, tab, frame) and return the specified DOM value " - "as a string. This is a wrapper around " - "window.domAutomationController.send().") GetDOMValue; - std::wstring GetDOMValue(const std::wstring& expr, - int window_index=0, - int tab_index=0, - const std::wstring& frame_xpath=""); - %feature("docstring", "Resets to the default theme. " "Returns true on success.") ResetToDefaultTheme; bool ResetToDefaultTheme(); |