Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Unified Diff: chrome/test/webdriver/commands/webelement_commands.cc

Issue 7055004: File upload API in chromedriver (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed error handlings according to latest changes and refactored. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/webdriver/commands/webelement_commands.cc
diff --git a/chrome/test/webdriver/commands/webelement_commands.cc b/chrome/test/webdriver/commands/webelement_commands.cc
index dae3e4eba697cf4ea7397069c73313c502834cbb..0bf78d6854d2842016a2251e9eeeb71dfd00098a 100644
--- a/chrome/test/webdriver/commands/webelement_commands.cc
+++ b/chrome/test/webdriver/commands/webelement_commands.cc
@@ -498,12 +498,91 @@ void ElementValueCommand::ExecuteGet(Response* const response) {
}
void ElementValueCommand::ExecutePost(Response* const response) {
+ std::string tag_name;
+ Error* error = GetElementAttribute("tagName", &tag_name);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ std::string type;
+ error = GetElementAttribute("type", &type);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ // If the element is a file upload control, set the file paths to the element.
+ // Otherwise send the value to the element as key input.
+ if (tag_name == "INPUT" && type == "file") {
kkania 2011/05/23 18:43:49 do case insensitive comparison
nodchip 2011/05/26 01:14:53 Done.
+ SetFilePathsToFileUploadControl(response);
+ } else {
+ SendKeys(response);
+ }
+}
+
+void ElementValueCommand::SetFilePathsToFileUploadControl(
+ Response* const response) const {
+ ListValue* key_list;
kkania 2011/05/23 18:43:49 rename key_list
nodchip 2011/05/26 01:14:53 Done.
+ if (!GetListParameter("value", &key_list)) {
+ response->SetError(new Error(kBadRequest,
+ "Missing or invalid 'value' parameter"));
+ return;
+ }
+
+ std::vector<std::string> paths;
+ for (size_t i = 0; i < key_list->GetSize(); ++i) {
+ std::string path;
+ key_list->GetString(i, &path);
kkania 2011/05/23 18:43:49 handle errors
kkania 2011/05/23 18:43:49 what do you think about checking that the path exi
nodchip 2011/05/26 01:14:53 Done.
nodchip 2011/05/26 01:14:53 Done.
+ paths.push_back(path);
+ }
+
+ bool is_displayed;
+ Error* error = session_->IsElementDisplayed(
kkania 2011/05/23 18:43:49 I think this code and the following bit is in quit
nodchip 2011/05/26 01:14:53 Done.
+ session_->current_target(), element, &is_displayed);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+ if (!is_displayed) {
+ response->SetError(new Error(kElementNotVisible,
+ "Element must be displayed"));
+ return;
+ }
+
+ gfx::Point location;
+ error = session_->GetElementLocationInView(element, &location);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ gfx::Size size;
+ error = session_->GetElementSize(session_->current_target(), element, &size);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ location.Offset(size.width() / 2, size.height() / 2);
+
+ error = session_->SetFilePathsToFileUploadControl(location, paths);
+ if (error) {
+ response->SetError(error);
+ return;
+ }
+
+ response->SetStatus(kSuccess);
+}
+
+void ElementValueCommand::SendKeys(Response* const response) const {
ListValue* key_list;
if (!GetListParameter("value", &key_list)) {
response->SetError(new Error(
kBadRequest, "Missing or invalid 'value' parameter"));
return;
}
+
// Flatten the given array of strings into one.
string16 keys;
for (size_t i = 0; i < key_list->GetSize(); ++i) {
@@ -524,6 +603,30 @@ void ElementValueCommand::ExecutePost(Response* const response) {
response->SetError(error);
}
+Error* ElementValueCommand::GetElementAttribute(const std::string& key,
+ std::string* value) const {
+ std::string script = base::StringPrintf(
+ "return (%s).apply(null, arguments);", atoms::GET_ATTRIBUTE);
kkania 2011/05/23 18:43:49 can we share this code with the attribute command
nodchip 2011/05/26 01:14:53 Done.
+
+ scoped_ptr<ListValue> args(new ListValue);
+ args->Append(element.ToValue());
+ args->Append(Value::CreateStringValue(key));
+
+ Value* result = NULL;
+ Error* error = session_->ExecuteScript(script, args.get(), &result);
+ if (error) {
+ return error;
+ }
+
+ if (result) {
+ result->GetAsString(value);
kkania 2011/05/23 18:43:49 return an error if not a string
nodchip 2011/05/26 01:14:53 Done.
+ delete result;
kkania 2011/05/23 18:43:49 use scoped_ptr
nodchip 2011/05/26 01:14:53 Done.
+ result = NULL;
+ }
+
+ return NULL;
+}
+
///////////////////// ElementTextCommand ////////////////////
ElementTextCommand::ElementTextCommand(

Powered by Google App Engine
This is Rietveld 408576698