diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-16 16:34:15 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-16 16:34:15 +0000 |
commit | f18744fd5ec25ee780633e039a9f53d2cce3b49c (patch) | |
tree | 4d81c829dbf6e1710f546277a7907f5c0a928937 /chrome/test/automation/dom_element_proxy.cc | |
parent | 966ddf0958e0e3eb3a145c5a9908bc8662e174be (diff) | |
download | chromium_src-f18744fd5ec25ee780633e039a9f53d2cce3b49c.zip chromium_src-f18744fd5ec25ee780633e039a9f53d2cce3b49c.tar.gz chromium_src-f18744fd5ec25ee780633e039a9f53d2cce3b49c.tar.bz2 |
Add ability to manipulate DOM elements from the automation proxy. Rework the way that javascript is packaged and parsed in the JavascriptExecutionController, and add some waiting methods.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/1632001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44778 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/automation/dom_element_proxy.cc')
-rw-r--r-- | chrome/test/automation/dom_element_proxy.cc | 304 |
1 files changed, 144 insertions, 160 deletions
diff --git a/chrome/test/automation/dom_element_proxy.cc b/chrome/test/automation/dom_element_proxy.cc index c7bc6da..feeeebf 100644 --- a/chrome/test/automation/dom_element_proxy.cc +++ b/chrome/test/automation/dom_element_proxy.cc @@ -4,61 +4,63 @@ #include "chrome/test/automation/dom_element_proxy.h" -#include "base/json/string_escape.h" -#include "base/string_util.h" -#include "base/utf_string_conversions.h" +#include "chrome/test/automation/javascript_execution_controller.h" +#include "chrome/test/automation/javascript_message_utils.h" #include "testing/gtest/include/gtest/gtest.h" -namespace { +using javascript_utils::JavaScriptPrintf; -// Convenience wrapper for GetDoubleQuotedJson function. -std::string GetDoubleQuotedJson(std::string utf8_string) { - return base::GetDoubleQuotedJson(UTF8ToUTF16(utf8_string)); -} +// JavaScriptObjectProxy methods +JavaScriptObjectProxy::JavaScriptObjectProxy( + JavaScriptExecutionController* executor, int handle) + : executor_(executor->AsWeakPtr()), handle_(handle) {} -} // namespace +JavaScriptObjectProxy::~JavaScriptObjectProxy() { + if (is_valid()) + executor_->Remove(handle_); +} -DOMElementProxyRef DOMElementProxy::GetContentDocument() { - const char* script = "%s.contentDocument;"; - DOMElementProxy* element = NULL; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str()), &element); - return element; +// DOMElementProxy::By methods +// static +DOMElementProxy::By DOMElementProxy::By::XPath( + const std::string& xpath) { + return By(TYPE_XPATH, xpath); } -DOMElementProxyRef DOMElementProxy::GetDocumentFromFrame( - const std::vector<std::string>& frame_names) { - if (!is_valid()) - return NULL; +// static +DOMElementProxy::By DOMElementProxy::By::Selectors( + const std::string& selectors) { + return By(TYPE_SELECTORS, selectors); +} - const char* script = "domAutomation.getDocumentFromFrame(%s, %s);"; - std::string string_script = StringPrintf( - script, this->GetReferenceJavaScript().c_str(), - JavaScriptExecutionController::Serialize(frame_names).c_str()); - DOMElementProxy* element = NULL; - executor_->ExecuteJavaScriptAndParse(string_script, &element); - return element; +// static +DOMElementProxy::By DOMElementProxy::By::Text( + const std::string& text) { + return By(TYPE_TEXT, text); } -DOMElementProxyRef DOMElementProxy::GetDocumentFromFrame( - const std::string& frame_name) { - if (!is_valid()) +// DOMElementProxy methods +DOMElementProxyRef DOMElementProxy::GetContentDocument() { + int element_handle; + if (!GetValue("contentdocument", &element_handle)) return NULL; - - std::vector<std::string> frame_names; - frame_names.push_back(frame_name); - return GetDocumentFromFrame(frame_names); + return executor_->GetObjectProxy<DOMElementProxy>(element_handle); } DOMElementProxyRef DOMElementProxy::GetDocumentFromFrame( - const std::string& frame_name1, const std::string& frame_name2) { + const std::vector<std::string>& frame_names) { if (!is_valid()) return NULL; - std::vector<std::string> frame_names; - frame_names.push_back(frame_name1); - frame_names.push_back(frame_name2); - return GetDocumentFromFrame(frame_names); + const char* script = "domAutomation.getDocumentFromFrame(" + "domAutomation.getObject(%s), %s);"; + int element_handle; + if (!executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), frame_names), + &element_handle)) { + return NULL; + } + return executor_->GetObjectProxy<DOMElementProxy>(element_handle); } DOMElementProxyRef DOMElementProxy::GetDocumentFromFrame( @@ -69,137 +71,115 @@ DOMElementProxyRef DOMElementProxy::GetDocumentFromFrame( std::vector<std::string> frame_names; frame_names.push_back(frame_name1); - frame_names.push_back(frame_name2); - frame_names.push_back(frame_name3); + if (!frame_name2.empty()) + frame_names.push_back(frame_name2); + if (!frame_name3.empty()) + frame_names.push_back(frame_name3); return GetDocumentFromFrame(frame_names); } -bool DOMElementProxy::FindByXPath(const std::string& xpath, - std::vector<DOMElementProxyRef>* elements) { - DCHECK(elements); - if (!is_valid()) - return false; - - const char* script = "domAutomation.findByXPath(%s, %s);"; - std::vector<DOMElementProxy*> element_pointers; - if (!executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(xpath).c_str()), - &element_pointers)) - return false; - for (size_t i = 0; i < element_pointers.size(); i++) - elements->push_back(element_pointers[i]); - return true; -} - -DOMElementProxyRef DOMElementProxy::FindByXPath(const std::string& xpath) { +DOMElementProxyRef DOMElementProxy::FindElement(const By& by) { if (!is_valid()) return NULL; - const char* script = "domAutomation.find1ByXPath(%s, %s);"; - DOMElementProxy* element = NULL; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(xpath).c_str()), - &element); - return element; + const char* script = "domAutomation.findElement(" + "domAutomation.getObject(%s), %s);"; + int element_handle; + if (!executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), by), &element_handle)) { + return NULL; + } + return executor_->GetObjectProxy<DOMElementProxy>(element_handle); } -bool DOMElementProxy::FindBySelectors( - const std::string& selectors, std::vector<DOMElementProxyRef>* elements) { +bool DOMElementProxy::FindElements(const By& by, + std::vector<DOMElementProxyRef>* elements) { DCHECK(elements); if (!is_valid()) return false; - const char* script = "domAutomation.findBySelectors(%s, %s);"; - std::vector<DOMElementProxy*> element_pointers; - if (!executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(selectors).c_str()), - &element_pointers)) + const char* script = "domAutomation.findElements(" + "domAutomation.getObject(%s), %s);"; + std::vector<int> element_handles; + if (!executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), by), &element_handles)) { return false; - for (size_t i = 0; i < element_pointers.size(); i++) - elements->push_back(element_pointers[i]); + } + for (size_t i = 0; i < element_handles.size(); i++) { + elements->push_back(executor_->GetObjectProxy<DOMElementProxy>( + element_handles[i])); + } return true; } -DOMElementProxyRef DOMElementProxy::FindBySelectors( - const std::string& selectors) { - if (!is_valid()) - return NULL; - - const char* script = "domAutomation.find1BySelectors(%s, %s);"; - DOMElementProxy* element = NULL; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(selectors).c_str()), - &element); - return element; -} - -bool DOMElementProxy::FindByText(const std::string& text, - std::vector<DOMElementProxyRef>* elements) { +bool DOMElementProxy::WaitForVisibleElementCount( + const By& by, int count, std::vector<DOMElementProxyRef>* elements) { DCHECK(elements); if (!is_valid()) return false; - const char* script = "domAutomation.findByText(%s, %s);"; - std::vector<DOMElementProxy*> element_pointers; - if (!executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(text).c_str()), - &element_pointers)) + const char* script = "domAutomation.waitForVisibleElementCount(" + "domAutomation.getObject(%s), %s, %s," + "domAutomation.getCallId());"; + std::vector<int> element_handles; + if (!executor_->ExecuteAsyncJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), by, count), &element_handles)) { return false; - for (size_t i = 0; i < element_pointers.size(); i++) - elements->push_back(element_pointers[i]); - return true; + } + if (static_cast<int>(element_handles.size()) == count) { + for (size_t i = 0; i < element_handles.size(); i++) { + elements->push_back(executor_->GetObjectProxy<DOMElementProxy>( + element_handles[i])); + } + } + return static_cast<int>(element_handles.size()) == count; } -DOMElementProxyRef DOMElementProxy::FindByText(const std::string& text) { - if (!is_valid()) +DOMElementProxyRef DOMElementProxy::WaitFor1VisibleElement(const By& by) { + std::vector<DOMElementProxyRef> elements; + if (!WaitForVisibleElementCount(by, 1, &elements)) return NULL; + return elements[0]; +} - const char* script = "domAutomation.find1ByText(%s, %s);"; - DOMElementProxy* element = NULL; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(text).c_str()), - &element); - return element; +bool DOMElementProxy::WaitForElementsToDisappear(const By& by) { + std::vector<DOMElementProxyRef> elements; + return WaitForVisibleElementCount(by, 0, &elements); } bool DOMElementProxy::Click() { - const char* script = "domAutomation.click(%s);"; if (!is_valid()) return false; + const char* script = "domAutomation.click(domAutomation.getObject(%s));"; return executor_->ExecuteJavaScript( - StringPrintf(script, this->GetReferenceJavaScript().c_str())); + JavaScriptPrintf(script, this->handle())); } bool DOMElementProxy::Type(const std::string& text) { - const char* script = "domAutomation.type(%s, %s);"; if (!is_valid()) return false; + const char* script = "domAutomation.type(domAutomation.getObject(%s), %s);"; bool success = false; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(text).c_str()), - &success); + if (!executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), text), &success)) { + return false; + } return success; } bool DOMElementProxy::SetText(const std::string& text) { - const char* script = "domAutomation.setText(%s, %s);"; if (!is_valid()) return false; + const char* script = "domAutomation.setText(" + "domAutomation.getObject(%s), %s);"; bool success = false; - executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(text).c_str()), - &success); + if (!executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), text), &success)) { + return false; + } return success; } @@ -209,11 +189,10 @@ bool DOMElementProxy::GetProperty(const std::string& property, if (!is_valid()) return false; - const char* script = "%s.%s;"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(property).c_str()), - out); + const char* script = "domAutomation.getProperty(" + "domAutomation.getObject(%s), %s);"; + return executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), property), out); } bool DOMElementProxy::GetAttribute(const std::string& attribute, @@ -222,41 +201,22 @@ bool DOMElementProxy::GetAttribute(const std::string& attribute, if (!is_valid()) return false; - const char* script = "%s.getAttribute(%s);"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str(), - GetDoubleQuotedJson(attribute).c_str()), - out); + const char* script = "domAutomation.getAttribute(" + "domAutomation.getObject(%s), %s);"; + return executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), attribute), out); } bool DOMElementProxy::GetText(std::string* text) { - DCHECK(text); - if (!is_valid()) - return false; - - const char* script = "domAutomation.getText(%s);"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str()), text); + return GetValue("text", text); } bool DOMElementProxy::GetInnerHTML(std::string* html) { - DCHECK(html); - if (!is_valid()) - return false; - - const char* script = "domAutomation.getInnerHTML(%s);"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str()), html); + return GetValue("innerhtml", html); } bool DOMElementProxy::GetId(std::string* id) { - DCHECK(id); - if (!is_valid()) - return false; - - const char* script = "%s.id;"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str()), id); + return GetValue("id", id); } bool DOMElementProxy::GetName(std::string* name) { @@ -264,14 +224,13 @@ bool DOMElementProxy::GetName(std::string* name) { } bool DOMElementProxy::GetVisibility(bool* visibility) { - DCHECK(visibility); - if (!is_valid()) - return false; + return GetValue("visibility", visibility); +} - const char* script = "domAutomation.isVisible(%s);"; - return executor_->ExecuteJavaScriptAndParse( - StringPrintf(script, this->GetReferenceJavaScript().c_str()), - visibility); +void DOMElementProxy::EnsureFindNoElements(const By& by) { + std::vector<DOMElementProxyRef> elements; + ASSERT_TRUE(FindElements(by, &elements)); + ASSERT_EQ(0u, elements.size()); } void DOMElementProxy::EnsureTextMatches(const std::string& expected_text) { @@ -297,3 +256,28 @@ void DOMElementProxy::EnsureVisibilityMatches(bool expected_visibility) { ASSERT_TRUE(GetVisibility(&visibility)); ASSERT_EQ(expected_visibility, visibility); } + +void DOMElementProxy::EnsureAttributeEventuallyMatches( + const std::string& attribute, const std::string& new_value) { + ASSERT_TRUE(is_valid()); + + const char* script = "domAutomation.waitForAttribute(" + "domAutomation.getObject(%s), %s, %s," + "domAutomation.getCallId())"; + if (!executor_->ExecuteAsyncJavaScript( + JavaScriptPrintf(script, this->handle(), attribute, new_value))) { + FAIL() << "Executing or parsing JavaScript failed"; + } +} + +template <typename T> +bool DOMElementProxy::GetValue(const std::string& type, T* out) { + DCHECK(out); + if (!is_valid()) + return false; + + const char* script = "domAutomation.getValue(" + "domAutomation.getObject(%s), %s);"; + return executor_->ExecuteJavaScriptAndGetReturn( + JavaScriptPrintf(script, this->handle(), type), out); +} |