diff options
author | chrisgao@chromium.org <chrisgao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-09 03:50:04 +0000 |
---|---|---|
committer | chrisgao@chromium.org <chrisgao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-09 03:50:04 +0000 |
commit | c76891128237ecb5bc7b62bfeaffd6d3f90f1bfa (patch) | |
tree | d835877b9febd10189b1847cba262e1aed1e74e1 | |
parent | e185b7e812655de4178b9f802b9a2ee659c5cd43 (diff) | |
download | chromium_src-c76891128237ecb5bc7b62bfeaffd6d3f90f1bfa.zip chromium_src-c76891128237ecb5bc7b62bfeaffd6d3f90f1bfa.tar.gz chromium_src-c76891128237ecb5bc7b62bfeaffd6d3f90f1bfa.tar.bz2 |
[chromedriver]Implement command: title.
Review URL: https://chromiumcodereview.appspot.com/11746025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175699 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/test/chromedriver/chromedriver.py | 3 | ||||
-rw-r--r-- | chrome/test/chromedriver/command_executor_impl.cc | 2 | ||||
-rw-r--r-- | chrome/test/chromedriver/commands.cc | 16 | ||||
-rw-r--r-- | chrome/test/chromedriver/commands.h | 6 | ||||
-rwxr-xr-x | chrome/test/chromedriver/run_py_tests.py | 75 | ||||
-rw-r--r-- | chrome/test/chromedriver/webserver.py | 65 | ||||
-rw-r--r-- | chrome/test/data/chromedriver/empty.html | 1 | ||||
-rw-r--r-- | chrome/test/data/chromedriver/page_test.html | 9 |
8 files changed, 147 insertions, 30 deletions
diff --git a/chrome/test/chromedriver/chromedriver.py b/chrome/test/chromedriver/chromedriver.py index 97793e8..9789639 100644 --- a/chrome/test/chromedriver/chromedriver.py +++ b/chrome/test/chromedriver/chromedriver.py @@ -84,6 +84,9 @@ class ChromeDriver(object): def SwitchToMainFrame(self): self.SwitchToFrame(None) + def GetTitle(self): + return self._ExecuteSessionCommand('getTitle') + def Quit(self): """Quits the browser and ends the session.""" self._ExecuteSessionCommand('quit') diff --git a/chrome/test/chromedriver/command_executor_impl.cc b/chrome/test/chromedriver/command_executor_impl.cc index eb1014f..0b7ab2c 100644 --- a/chrome/test/chromedriver/command_executor_impl.cc +++ b/chrome/test/chromedriver/command_executor_impl.cc @@ -47,6 +47,8 @@ void CommandExecutorImpl::Init() { base::Bind(&ExecuteExecuteScript))); command_map_.Set("switchToFrame", base::Bind(execute_session_command, base::Bind(&ExecuteSwitchToFrame))); + command_map_.Set("getTitle", base::Bind(execute_session_command, + base::Bind(&ExecuteGetTitle))); Command quit_command = base::Bind(execute_session_command, base::Bind(&ExecuteQuit, &session_map_)); command_map_.Set("quit", quit_command); diff --git a/chrome/test/chromedriver/commands.cc b/chrome/test/chromedriver/commands.cc index 50f26c8..2b474f7 100644 --- a/chrome/test/chromedriver/commands.cc +++ b/chrome/test/chromedriver/commands.cc @@ -148,3 +148,19 @@ Status ExecuteSwitchToFrame( session->frame = frame; return Status(kOk); } + +Status ExecuteGetTitle( + Session* session, + const base::DictionaryValue& params, + scoped_ptr<base::Value>* value) { + const char* kGetTitleScript = + "function() {" + " if (document.title)" + " return document.title;" + " else" + " return document.URL;" + "}"; + base::ListValue args; + return session->chrome->CallFunction( + session->frame, kGetTitleScript, args, value); +} diff --git a/chrome/test/chromedriver/commands.h b/chrome/test/chromedriver/commands.h index de0040a..abf47a5 100644 --- a/chrome/test/chromedriver/commands.h +++ b/chrome/test/chromedriver/commands.h @@ -64,4 +64,10 @@ Status ExecuteSwitchToFrame( const base::DictionaryValue& params, scoped_ptr<base::Value>* value); +// Get the current page title. +Status ExecuteGetTitle( + Session* session, + const base::DictionaryValue& params, + scoped_ptr<base::Value>* value); + #endif // CHROME_TEST_CHROMEDRIVER_COMMANDS_H_ diff --git a/chrome/test/chromedriver/run_py_tests.py b/chrome/test/chromedriver/run_py_tests.py index 97fb734..0f3bb51 100755 --- a/chrome/test/chromedriver/run_py_tests.py +++ b/chrome/test/chromedriver/run_py_tests.py @@ -12,65 +12,80 @@ import sys import unittest import chromedriver +import webserver _THIS_DIR = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(0, os.path.join(_THIS_DIR, os.pardir, 'pylib')) +from common import chrome_paths from common import unittest_util class ChromeDriverTest(unittest.TestCase): """End to end tests for ChromeDriver.""" + @classmethod + def setUpClass(cls): + cls._http_server = webserver.WebServer(chrome_paths.GetTestData()) + + @classmethod + def tearDownClass(cls): + cls._http_server.Shutdown() + + @staticmethod + def GetHttpUrlForFile(file_path): + return ChromeDriverTest._http_server.GetUrl() + file_path + + def setUp(self): + self._driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) + + def tearDown(self): + self._driver.Quit() + def testStartStop(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) - driver.Quit() + pass def testLoadUrl(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) - driver.Load('http://www.google.com') - driver.Quit() + self._driver.Load(self.GetHttpUrlForFile('/chromedriver/empty.html')) def testEvaluateScript(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) - self.assertEquals(1, driver.ExecuteScript('return 1')) - self.assertEquals(None, driver.ExecuteScript('')) - driver.Quit() + self.assertEquals(1, self._driver.ExecuteScript('return 1')) + self.assertEquals(None, self._driver.ExecuteScript('')) def testEvaluateScriptWithArgs(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) script = ('document.body.innerHTML = "<div>b</div><div>c</div>";' + 'return {stuff: document.querySelectorAll("div")};') - stuff = driver.ExecuteScript(script)['stuff'] + stuff = self._driver.ExecuteScript(script)['stuff'] script = 'return arguments[0].innerHTML + arguments[1].innerHTML'; - self.assertEquals('bc', driver.ExecuteScript(script, stuff[0], stuff[1])) - driver.Quit() + self.assertEquals( + 'bc', self._driver.ExecuteScript(script, stuff[0], stuff[1])) def testEvaluateInvalidScript(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) self.assertRaises(chromedriver.ChromeDriverException, - driver.ExecuteScript, '{{{') - driver.Quit() + self._driver.ExecuteScript, '{{{') def testSwitchToFrame(self): - driver = chromedriver.ChromeDriver(_CHROMEDRIVER_LIB, _CHROME_BINARY) - driver.ExecuteScript( + self._driver.ExecuteScript( 'var frame = document.createElement("iframe");' 'frame.id="id";' 'frame.name="name";' 'document.body.appendChild(frame);') - self.assertTrue(driver.ExecuteScript('return window.top == window')) - driver.SwitchToFrame('id') - self.assertTrue(driver.ExecuteScript('return window.top != window')) - driver.SwitchToMainFrame() - self.assertTrue(driver.ExecuteScript('return window.top == window')) - driver.SwitchToFrame('name') - self.assertTrue(driver.ExecuteScript('return window.top != window')) - driver.SwitchToMainFrame() - self.assertTrue(driver.ExecuteScript('return window.top == window')) - driver.SwitchToFrameByIndex(0) - self.assertTrue(driver.ExecuteScript('return window.top != window')) - driver.Quit() + self.assertTrue(self._driver.ExecuteScript('return window.top == window')) + self._driver.SwitchToFrame('id') + self.assertTrue(self._driver.ExecuteScript('return window.top != window')) + self._driver.SwitchToMainFrame() + self.assertTrue(self._driver.ExecuteScript('return window.top == window')) + self._driver.SwitchToFrame('name') + self.assertTrue(self._driver.ExecuteScript('return window.top != window')) + self._driver.SwitchToMainFrame() + self.assertTrue(self._driver.ExecuteScript('return window.top == window')) + self._driver.SwitchToFrameByIndex(0) + self.assertTrue(self._driver.ExecuteScript('return window.top != window')) + + def testGetTitle(self): + script = 'document.title = "title"; return 1;' + self.assertEquals(1, self._driver.ExecuteScript(script)) + self.assertEqual('title', self._driver.GetTitle()) if __name__ == '__main__': diff --git a/chrome/test/chromedriver/webserver.py b/chrome/test/chromedriver/webserver.py new file mode 100644 index 0000000..e6571f8 --- /dev/null +++ b/chrome/test/chromedriver/webserver.py @@ -0,0 +1,65 @@ +# 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 BaseHTTPServer +import os +import threading + + +class _FileRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + """Sends back file resources relative to the server's |root_dir|.""" + + def do_GET(self): + if self.path.endswith('favicon.ico'): + self.send_error(404) + return + path = os.path.join(self.server.root_dir, *self.path.split('/')) + with open(path, 'r') as f: + data = f.read() + self.send_response(200) + self.send_header('Content-Length', len(data)) + self.end_headers() + self.wfile.write(data) + + +class WebServer(object): + """An HTTP or HTTPS server that serves files on its own thread.""" + + def __init__(self, root_dir, server_cert_and_key_path=None): + """Starts the web server on its own thread on an ephemeral port. + It is an HTTP server if parameter server_cert_and_key_path is not provided. + Otherwise, it is an HTTPS server. + + After this function returns, it is safe to assume the server is ready + to receive requests. + + Args: + root_dir: root path to serve files from. This parameter is required. + server_cert_and_key_path: path to a PEM file containing the cert and key. + if it is None, start the server as an HTTP one. + """ + self._server = BaseHTTPServer.HTTPServer( + ('127.0.0.1', 0), _FileRequestHandler) + self._server.root_dir = root_dir + if server_cert_and_key_path is not None: + self._is_https_enabled = True + self._server.socket = ssl.wrap_socket( + self._server.socket, certfile=server_cert_and_key_path, + server_side=True) + else: + self._is_https_enabled = False + + self._thread = threading.Thread(target=self._server.serve_forever) + self._thread.start() + + def GetUrl(self): + """Returns the base URL of the server.""" + if self._is_https_enabled: + return 'https://127.0.0.1:%s' % self._server.server_port + return 'http://127.0.0.1:%s' % self._server.server_port + + def Shutdown(self): + """Shuts down the server synchronously.""" + self._server.shutdown() + self._thread.join() diff --git a/chrome/test/data/chromedriver/empty.html b/chrome/test/data/chromedriver/empty.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/chrome/test/data/chromedriver/empty.html @@ -0,0 +1 @@ +<html></html> diff --git a/chrome/test/data/chromedriver/page_test.html b/chrome/test/data/chromedriver/page_test.html new file mode 100644 index 0000000..d5d039f --- /dev/null +++ b/chrome/test/data/chromedriver/page_test.html @@ -0,0 +1,9 @@ +<html> +<head> +<title>page test</title> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +</head> +<body> +page test body. +</body> +</html> |