diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-08 00:06:28 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-08 00:06:28 +0000 |
commit | 5fb586a686da09db00362bebdc282b0c3fae0ec5 (patch) | |
tree | de315ea045043ac98538590bb7eb78918da3718e /chrome/test/webdriver | |
parent | ae7ba1ee25011a3158ecf73e3014699fa8dc7406 (diff) | |
download | chromium_src-5fb586a686da09db00362bebdc282b0c3fae0ec5.zip chromium_src-5fb586a686da09db00362bebdc282b0c3fae0ec5.tar.gz chromium_src-5fb586a686da09db00362bebdc282b0c3fae0ec5.tar.bz2 |
Convert ChromeDriver to use only the JSON automation interface.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6614023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77212 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/webdriver')
-rw-r--r-- | chrome/test/webdriver/automation.cc | 294 | ||||
-rw-r--r-- | chrome/test/webdriver/automation.h | 40 | ||||
-rw-r--r-- | chrome/test/webdriver/commands/mouse_commands.cc | 5 | ||||
-rw-r--r-- | chrome/test/webdriver/session.cc | 38 | ||||
-rw-r--r-- | chrome/test/webdriver/session.h | 7 | ||||
-rw-r--r-- | chrome/test/webdriver/webdriver_key_converter.cc | 1 | ||||
-rw-r--r-- | chrome/test/webdriver/webdriver_key_converter_unittest.cc | 2 |
7 files changed, 134 insertions, 253 deletions
diff --git a/chrome/test/webdriver/automation.cc b/chrome/test/webdriver/automation.cc index 6f56f93..74046b9 100644 --- a/chrome/test/webdriver/automation.cc +++ b/chrome/test/webdriver/automation.cc @@ -24,9 +24,7 @@ #include "chrome/common/url_constants.h" #include "chrome/test/automation/automation_json_requests.h" #include "chrome/test/automation/automation_proxy.h" -#include "chrome/test/automation/browser_proxy.h" #include "chrome/test/automation/proxy_launcher.h" -#include "chrome/test/automation/tab_proxy.h" #include "googleurl/src/gurl.h" #include "ui/gfx/point.h" @@ -117,17 +115,6 @@ bool GetDefaultChromeExeDir(FilePath* browser_directory) { namespace webdriver { -WebKeyEvent::WebKeyEvent(automation::KeyEventTypes type, - ui::KeyboardCode key_code, - const std::string& unmodified_text, - const std::string& modified_text, - int modifiers) - : type(type), - key_code(key_code), - unmodified_text(unmodified_text), - modified_text(modified_text), - modifiers(modifiers) {} - Automation::Automation() {} Automation::~Automation() {} @@ -173,92 +160,74 @@ void Automation::ExecuteScript(int tab_id, const std::string& script, std::string* result, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - std::wstring wide_xpath = UTF8ToWide(frame_xpath); - std::wstring wide_script = UTF8ToWide(script); - std::wstring wide_result; - *success = tab->ExecuteAndExtractString( - wide_xpath, wide_script, &wide_result); - if (*success) - *result = WideToUTF8(wide_result); + + Value* unscoped_value; + if (!SendExecuteJavascriptJSONRequest( + automation(), windex, tab_index, frame_xpath, script, &unscoped_value)) { + *success = false; + return; + } + scoped_ptr<Value> value(unscoped_value); + *success = value->GetAsString(result); } void Automation::MouseMove(int tab_id, const gfx::Point& p, bool* success) { - std::string reply; - DictionaryValue dict; - - dict.SetString("command", "WebkitMouseMove"); - dict.SetInteger("x", p.x()); - dict.SetInteger("y", p.y()); - - *success = SendJSONRequest(tab_id, dict, &reply); - if (!*success) { - LOG(ERROR) << "Could not send mouse event. Reply: " << reply; + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { + *success = false; + return; } + + *success = SendMouseMoveJSONRequest( + automation(), windex, tab_index, p.x(), p.y()); } void Automation::MouseClick(int tab_id, const gfx::Point& p, - int flag, + automation::MouseButton button, bool* success) { - std::string reply; - DictionaryValue dict; - - dict.SetString("command", "WebkitMouseClick"); - dict.SetInteger("button_flags", flag); - dict.SetInteger("x", p.x()); - dict.SetInteger("y", p.y()); - - *success = SendJSONRequest(tab_id, dict, &reply); - if (!*success) { - LOG(ERROR) << "Could not send mouse event. Reply: " << reply; + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { + *success = false; + return; } + + *success = SendMouseClickJSONRequest( + automation(), windex, tab_index, button, p.x(), p.y()); } void Automation::MouseDrag(int tab_id, const gfx::Point& start, const gfx::Point& end, bool* success) { - std::string reply; - DictionaryValue dict; - - dict.SetString("command", "WebkitMouseDrag"); - dict.SetInteger("start_x", start.x()); - dict.SetInteger("start_y", start.y()); - dict.SetInteger("end_x", end.x()); - dict.SetInteger("end_y", end.y()); - - *success = SendJSONRequest(tab_id, dict, &reply); - if (!*success) { - LOG(ERROR) << "Could not send mouse event. Reply: " << reply; + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { + *success = false; + return; } + + *success = SendMouseDragJSONRequest( + automation(), windex, tab_index, start.x(), start.y(), end.x(), end.y()); } void Automation::SendWebKeyEvent(int tab_id, const WebKeyEvent& key_event, bool* success) { - std::string reply; - DictionaryValue dict; - - dict.SetString("command", "SendKeyEventToActiveTab"); - dict.SetInteger("type", key_event.type); - dict.SetInteger("nativeKeyCode", key_event.key_code); - dict.SetInteger("windowsKeyCode", key_event.key_code); - dict.SetString("unmodifiedText", key_event.unmodified_text); - dict.SetString("text", key_event.modified_text); - dict.SetInteger("modifiers", key_event.modifiers); - dict.SetBoolean("isSystemKey", false); - - *success = SendJSONRequest(tab_id, dict, &reply); - if (!*success) { - LOG(ERROR) << "Could not send web key event. Reply: " << reply; + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { + *success = false; + return; } + + *success = SendWebKeyEventJSONRequest( + automation(), windex, tab_index, key_event); } void Automation::NavigateToURL(int tab_id, @@ -280,163 +249,134 @@ void Automation::NavigateToURL(int tab_id, } void Automation::GoForward(int tab_id, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->GoForward(); + + *success = SendGoForwardJSONRequest(automation(), windex, tab_index); } void Automation::GoBack(int tab_id, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->GoBack(); + + *success = SendGoBackJSONRequest(automation(), windex, tab_index); } void Automation::Reload(int tab_id, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->Reload(); + + *success = SendReloadJSONRequest(automation(), windex, tab_index); } void Automation::GetURL(int tab_id, std::string* url, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - GURL gurl; - *success = tab->GetCurrentURL(&gurl); - if (*success) - *url = gurl.possibly_invalid_spec(); + + *success = SendGetTabURLJSONRequest(automation(), windex, tab_index, url); } void Automation::GetGURL(int tab_id, GURL* gurl, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { - *success = false; - return; - } - *success = tab->GetCurrentURL(gurl); + std::string url; + GetURL(tab_id, &url, success); + if (*success) + *gurl = GURL(url); } void Automation::GetTabTitle(int tab_id, std::string* tab_title, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - std::wstring wide_title; - *success = tab->GetTabTitle(&wide_title); - if (*success) - *tab_title = WideToUTF8(wide_title); + + *success = SendGetTabTitleJSONRequest( + automation(), windex, tab_index, tab_title); } void Automation::GetCookies(int tab_id, const GURL& gurl, std::string* cookies, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->GetCookies(gurl, cookies); -} -void Automation::GetCookieByName(int tab_id, - const GURL& gurl, - const std::string& cookie_name, - std::string* cookie, - bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { - *success = false; - return; - } - *success = tab->GetCookieByName(gurl, cookie_name, cookie); + *success = SendGetCookiesJSONRequest( + automation(), windex, gurl.possibly_invalid_spec(), cookies); } void Automation::DeleteCookie(int tab_id, const GURL& gurl, const std::string& cookie_name, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->DeleteCookie(gurl, cookie_name); + + *success = SendDeleteCookieJSONRequest( + automation(), + windex, + gurl.possibly_invalid_spec(), + cookie_name); } void Automation::SetCookie(int tab_id, const GURL& gurl, const std::string& cookie, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->SetCookie(gurl, cookie); + + *success = SendSetCookieJSONRequest( + automation(), + windex, + gurl.possibly_invalid_spec(), + cookie); } void Automation::GetTabIds(std::vector<int>* tab_ids, bool* success) { - *success = false; - int browser_count = 0; - if (!automation()->GetBrowserWindowCount(&browser_count)) { - LOG(ERROR) << "Failed to get browser window count"; - return; - } - TabIdMap tab_id_map; - for (int browser_index = 0; browser_index < browser_count; ++browser_index) { - scoped_refptr<BrowserProxy> browser = - automation()->GetBrowserWindow(browser_index); - if (!browser.get()) - continue; - int tab_count = 0; - if (!browser->GetTabCount(&tab_count)) - continue; - - for (int tab_index = 0; tab_index < tab_count; ++tab_index) { - scoped_refptr<TabProxy> tab = browser->GetTab(tab_index); - if (!tab.get()) - continue; - tab_ids->push_back(tab->handle()); - tab_id_map.insert(std::make_pair(tab->handle(), tab)); - } - } - - tab_id_map_ = tab_id_map; - *success = true; + *success = SendGetTabIdsJSONRequest(automation(), tab_ids); } -void Automation::DoesTabExist(int tab_id, bool* does_exist) { - TabProxy* tab = GetTabById(tab_id); - *does_exist = tab && tab->is_valid(); +void Automation::DoesTabExist(int tab_id, bool* does_exist, bool* success) { + *success = SendIsTabIdValidJSONRequest(automation(), tab_id, does_exist); } void Automation::CloseTab(int tab_id, bool* success) { - TabProxy* tab = GetTabById(tab_id); - if (!tab) { + int windex = 0, tab_index = 0; + if (!GetIndicesForTab(tab_id, &windex, &tab_index)) { *success = false; return; } - *success = tab->Close(true); + + *success = SendCloseTabJSONRequest(automation(), windex, tab_index); } void Automation::GetVersion(std::string* version) { @@ -444,61 +384,17 @@ void Automation::GetVersion(std::string* version) { } void Automation::WaitForAllTabsToStopLoading(bool* success) { - DictionaryValue dict; - dict.SetString("command", "WaitForAllTabsToStopLoading"); - std::string request, reply; - base::JSONWriter::Write(&dict, false, &request); - *success = automation()->SendJSONRequest(request, &reply); -} - -TabProxy* Automation::GetTabById(int tab_id) { - TabIdMap::const_iterator iter = tab_id_map_.find(tab_id); - if (iter != tab_id_map_.end()) { - return iter->second.get(); - } - return NULL; + *success = SendWaitForAllTabsToStopLoadingJSONRequest(automation()); } AutomationProxy* Automation::automation() const { return launcher_->automation(); } -bool Automation::SendJSONRequest(int tab_id, - const DictionaryValue& dict, - std::string* reply) { - std::string request; - - base::JSONWriter::Write(&dict, false, &request); - TabProxy* tab = GetTabById(tab_id); - if (!tab) { - LOG(ERROR) << "No such tab"; - return false; - } - - int tab_index = 0; - if (!tab->GetTabIndex(&tab_index)) { - LOG(ERROR) << "Could not get tab index"; - return false; - } - - scoped_refptr<BrowserProxy> browser = tab->GetParentBrowser(); - if (!browser.get()) { - LOG(ERROR) << "Could not get parent browser of tab"; - return false; - } - - if (!browser->ActivateTab(tab_index)) { - LOG(ERROR) << "Could not activate tab"; - return false; - } - - return browser->SendJSONRequest(request, reply); -} - bool Automation::GetIndicesForTab( int tab_id, int* browser_index, int* tab_index) { - if (!SendGetIndicesFromTabJSONRequest(automation(), tab_id, - browser_index, tab_index)) { + if (!SendGetIndicesFromTabIdJSONRequest(automation(), tab_id, + browser_index, tab_index)) { LOG(ERROR) << "Could not get browser and tab indices for WebDriver tab id"; return false; } diff --git a/chrome/test/webdriver/automation.h b/chrome/test/webdriver/automation.h index b773f4c..62c9d21 100644 --- a/chrome/test/webdriver/automation.h +++ b/chrome/test/webdriver/automation.h @@ -20,7 +20,7 @@ class DictionaryValue; class FilePath; class GURL; class ProxyLauncher; -class TabProxy; +struct WebKeyEvent; namespace gfx { class Point; @@ -28,20 +28,6 @@ class Point; namespace webdriver { -struct WebKeyEvent { - WebKeyEvent(automation::KeyEventTypes type, - ui::KeyboardCode key_code, - const std::string& unmodified_text, - const std::string& modified_text, - int modifiers); - - automation::KeyEventTypes type; - ui::KeyboardCode key_code; - std::string unmodified_text; - std::string modified_text; - int modifiers; -}; - // Creates and controls the Chrome instance. // This class should be created and accessed on a single thread. // Note: All member functions are void because they are invoked @@ -80,11 +66,6 @@ class Automation { void GetTabTitle(int tab_id, std::string* tab_title, bool* success); void GetCookies( int tab_id, const GURL& gurl, std::string* cookies, bool* success); - void GetCookieByName(int tab_id, - const GURL& gurl, - const std::string& cookie_name, - std::string* cookie, - bool* success); void DeleteCookie(int tab_id, const GURL& gurl, const std::string& cookie_name, @@ -92,7 +73,10 @@ class Automation { void SetCookie( int tab_id, const GURL& gurl, const std::string& cookie, bool* success); void MouseMove(int tab_id, const gfx::Point& p, bool* success); - void MouseClick(int tab_id, const gfx::Point& p, int flag, bool* success); + void MouseClick(int tab_id, + const gfx::Point& p, + automation::MouseButton button, + bool* success); void MouseDrag(int tab_id, const gfx::Point& start, const gfx::Point& end, @@ -103,7 +87,7 @@ class Automation { void GetTabIds(std::vector<int>* tab_ids, bool* success); // Check if the given tab exists currently. - void DoesTabExist(int tab_id, bool* does_exist); + void DoesTabExist(int tab_id, bool* does_exist, bool* success); void CloseTab(int tab_id, bool* success); @@ -114,20 +98,10 @@ class Automation { void WaitForAllTabsToStopLoading(bool* success); private: - typedef std::map<int, scoped_refptr<TabProxy> > TabIdMap; - - TabProxy* GetTabById(int tab_id); AutomationProxy* automation() const; + bool GetIndicesForTab(int tab_id, int* browser_index, int* tab_index); scoped_ptr<ProxyLauncher> launcher_; - // Map from tab ID to |TabProxy|. The tab ID is simply the |AutomationHandle| - // for the proxy. - TabIdMap tab_id_map_; - - bool SendJSONRequest( - int tab_id, const DictionaryValue& dict, std::string* reply); - - bool GetIndicesForTab(int tab_id, int* browser_index, int* tab_index); DISALLOW_COPY_AND_ASSIGN(Automation); }; diff --git a/chrome/test/webdriver/commands/mouse_commands.cc b/chrome/test/webdriver/commands/mouse_commands.cc index b70c574..626dbfe 100644 --- a/chrome/test/webdriver/commands/mouse_commands.cc +++ b/chrome/test/webdriver/commands/mouse_commands.cc @@ -5,11 +5,11 @@ #include "chrome/test/webdriver/commands/mouse_commands.h" #include "base/values.h" +#include "chrome/common/automation_constants.h" #include "chrome/test/webdriver/commands/response.h" #include "chrome/test/webdriver/error_codes.h" #include "chrome/test/webdriver/session.h" #include "chrome/test/webdriver/web_element_id.h" -#include "ui/base/events.h" #include "ui/gfx/point.h" namespace webdriver { @@ -43,7 +43,7 @@ void MouseCommand::ExecutePost(Response* response) { switch (cmd_) { case kClick: VLOG(1) << "Mouse click at: (" << x << ", " << y << ")" << std::endl; - session_->MouseClick(gfx::Point(x, y), ui::EF_LEFT_BUTTON_DOWN); + session_->MouseClick(gfx::Point(x, y), automation::kLeftButton); break; case kHover: @@ -108,4 +108,3 @@ HoverCommand::HoverCommand(const std::vector<std::string>& path_segments, HoverCommand::~HoverCommand() {} } // namespace webdriver - diff --git a/chrome/test/webdriver/session.cc b/chrome/test/webdriver/session.cc index 400c7eb..4402797 100644 --- a/chrome/test/webdriver/session.cc +++ b/chrome/test/webdriver/session.cc @@ -30,6 +30,7 @@ #include "chrome/app/chrome_command_ids.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_switches.h" +#include "chrome/test/automation/automation_json_requests.h" #include "chrome/test/test_launcher_utils.h" #include "chrome/test/webdriver/session_manager.h" #include "chrome/test/webdriver/utility_functions.h" @@ -259,14 +260,15 @@ bool Session::GetTabTitle(std::string* tab_title) { return success; } -void Session::MouseClick(const gfx::Point& click, int flags) { +void Session::MouseClick(const gfx::Point& click, + automation::MouseButton button) { bool success = false; RunSessionTask(NewRunnableMethod( automation_.get(), &Automation::MouseClick, current_window_id_, click, - flags, + button, &success)); } @@ -309,16 +311,20 @@ bool Session::GetCookies(const GURL& url, std::string* cookies) { bool Session::GetCookieByName(const GURL& url, const std::string& cookie_name, std::string* cookie) { - bool success = false; - RunSessionTask(NewRunnableMethod( - automation_.get(), - &Automation::GetCookieByName, - current_window_id_, - url, - cookie_name, - cookie, - &success)); - return success; + std::string cookies; + if (!GetCookies(url, &cookies)) + return false; + + std::string namestr = cookie_name + "="; + std::string::size_type idx = cookies.find(namestr); + if (idx != std::string::npos) { + cookies.erase(0, idx + namestr.length()); + *cookie = cookies.substr(0, cookies.find(";")); + } else { + cookie->clear(); + } + + return true; } bool Session::DeleteCookie(const GURL& url, const std::string& cookie_name) { @@ -359,12 +365,18 @@ ErrorCode Session::SwitchToWindow(const std::string& name) { int switch_to_id = 0; int name_no = 0; if (base::StringToInt(name, &name_no)) { + bool success = false; bool does_exist = false; RunSessionTask(NewRunnableMethod( automation_.get(), &Automation::DoesTabExist, name_no, - &does_exist)); + &does_exist, + &success)); + if (!success) { + LOG(ERROR) << "Unable to determine if window exists"; + return kUnknownError; + } if (does_exist) switch_to_id = name_no; } diff --git a/chrome/test/webdriver/session.h b/chrome/test/webdriver/session.h index 222823c..8198f28 100644 --- a/chrome/test/webdriver/session.h +++ b/chrome/test/webdriver/session.h @@ -12,6 +12,7 @@ #include "base/scoped_ptr.h" #include "base/string16.h" #include "base/threading/thread.h" +#include "chrome/common/automation_constants.h" #include "chrome/test/webdriver/automation.h" #include "chrome/test/webdriver/error_codes.h" @@ -74,10 +75,8 @@ class Session { // ownership of |element|. ErrorCode SendKeys(const WebElementId& element, const string16& keys); - // Click events with the mouse should use the values found in: - // views/events/event.h. In the Webdriver JSON spec the MouseMove - // function directly maps to the hover command. - void MouseClick(const gfx::Point& click, int flags); + // Clicks the mouse at the given location using the given button. + void MouseClick(const gfx::Point& click, automation::MouseButton button); bool MouseMove(const gfx::Point& location); bool MouseDrag(const gfx::Point& start, const gfx::Point& end); diff --git a/chrome/test/webdriver/webdriver_key_converter.cc b/chrome/test/webdriver/webdriver_key_converter.cc index 70ca6d8..f8f3db6 100644 --- a/chrome/test/webdriver/webdriver_key_converter.cc +++ b/chrome/test/webdriver/webdriver_key_converter.cc @@ -6,6 +6,7 @@ #include "base/utf_string_conversions.h" #include "chrome/common/automation_constants.h" +#include "chrome/test/automation/automation_json_requests.h" #include "chrome/test/webdriver/keycode_text_conversion.h" namespace { diff --git a/chrome/test/webdriver/webdriver_key_converter_unittest.cc b/chrome/test/webdriver/webdriver_key_converter_unittest.cc index 9d91b92..cc9c473 100644 --- a/chrome/test/webdriver/webdriver_key_converter_unittest.cc +++ b/chrome/test/webdriver/webdriver_key_converter_unittest.cc @@ -7,7 +7,7 @@ #include "base/string16.h" #include "base/utf_string_conversions.h" -#include "chrome/test/webdriver/automation.h" +#include "chrome/test/automation/automation_json_requests.h" #include "chrome/test/webdriver/webdriver_key_converter.h" #include "testing/gtest/include/gtest/gtest.h" |