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

Side by Side 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 according to the code review. Created 9 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/test/webdriver/commands/webelement_commands.h" 5 #include "chrome/test/webdriver/commands/webelement_commands.h"
6 6
7 #include "base/file_util.h"
8 #include "base/format_macros.h"
7 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_util.h"
8 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
9 #include "base/third_party/icu/icu_utf.h" 12 #include "base/third_party/icu/icu_utf.h"
10 #include "base/values.h" 13 #include "base/values.h"
11 #include "chrome/test/webdriver/commands/response.h" 14 #include "chrome/test/webdriver/commands/response.h"
12 #include "chrome/test/webdriver/session.h" 15 #include "chrome/test/webdriver/session.h"
13 #include "chrome/test/webdriver/webdriver_error.h" 16 #include "chrome/test/webdriver/webdriver_error.h"
14 #include "third_party/webdriver/atoms.h" 17 #include "third_party/webdriver/atoms.h"
15 #include "ui/gfx/point.h" 18 #include "ui/gfx/point.h"
16 #include "ui/gfx/size.h" 19 #include "ui/gfx/size.h"
17 20
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } 61 }
59 62
60 void ElementAttributeCommand::ExecuteGet(Response* const response) { 63 void ElementAttributeCommand::ExecuteGet(Response* const response) {
61 // There should be at least 7 segments to match 64 // There should be at least 7 segments to match
62 // "/session/$session/element/$id/attribute/$name" 65 // "/session/$session/element/$id/attribute/$name"
63 if (path_segments_.size() < 7) { 66 if (path_segments_.size() < 7) {
64 response->SetError(new Error(kBadRequest, "Path segments is less than 7")); 67 response->SetError(new Error(kBadRequest, "Path segments is less than 7"));
65 return; 68 return;
66 } 69 }
67 70
68 std::string script = base::StringPrintf( 71 const std::string key = path_segments_.at(6);
69 "return (%s).apply(null, arguments);", atoms::GET_ATTRIBUTE); 72 Value* value;
70 73 Error* error = session_->GetAttribute(element, key, &value);
71 ListValue args;
72 args.Append(element.ToValue());
73 args.Append(Value::CreateStringValue(path_segments_.at(6)));
74
75 Value* result = NULL;
76 Error* error = session_->ExecuteScript(script, &args, &result);
77 if (error) { 74 if (error) {
78 response->SetError(error); 75 response->SetError(error);
79 return; 76 return;
80 } 77 }
81 response->SetValue(result); 78
79 response->SetValue(value);
82 } 80 }
83 81
84 ///////////////////// ElementClearCommand //////////////////// 82 ///////////////////// ElementClearCommand ////////////////////
85 83
86 ElementClearCommand::ElementClearCommand( 84 ElementClearCommand::ElementClearCommand(
87 const std::vector<std::string>& path_segments, 85 const std::vector<std::string>& path_segments,
88 DictionaryValue* parameters) 86 DictionaryValue* parameters)
89 : WebElementCommand(path_segments, parameters) {} 87 : WebElementCommand(path_segments, parameters) {}
90 88
91 ElementClearCommand::~ElementClearCommand() {} 89 ElementClearCommand::~ElementClearCommand() {}
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 if (!result->IsType(Value::TYPE_STRING) && 489 if (!result->IsType(Value::TYPE_STRING) &&
492 !result->IsType(Value::TYPE_NULL)) { 490 !result->IsType(Value::TYPE_NULL)) {
493 response->SetError(new Error( 491 response->SetError(new Error(
494 kUnknownError, "Result is not string or null type")); 492 kUnknownError, "Result is not string or null type"));
495 return; 493 return;
496 } 494 }
497 response->SetValue(result.release()); 495 response->SetValue(result.release());
498 } 496 }
499 497
500 void ElementValueCommand::ExecutePost(Response* const response) { 498 void ElementValueCommand::ExecutePost(Response* const response) {
499 bool is_input = false;
500 Error* error = HasAttributeWithLowerCaseValueASCII("tagName", "input",
501 &is_input);
502 if (error) {
503 response->SetError(error);
504 return;
505 }
506
507 bool is_file = false;
508 error = HasAttributeWithLowerCaseValueASCII("type", "file", &is_file);
509 if (error) {
510 response->SetError(error);
511 return;
512 }
513
514 // If the element is a file upload control, set the file paths to the element.
515 // Otherwise send the value to the element as key input.
516 if (is_input && is_file) {
517 error = DragAndDropFilePaths();
518 } else {
519 error = SendKeys();
520 }
521
522 if (error) {
523 response->SetError(error);
524 return;
525 }
526 }
527
528 Error* ElementValueCommand::HasAttributeWithLowerCaseValueASCII(
529 const std::string& key, const std::string& value, bool* result) const {
530 Value* unscoped_value = NULL;
531 Error* error = session_->GetAttribute(element, key, &unscoped_value);
532 scoped_ptr<Value> scoped_value(unscoped_value);
533 unscoped_value = NULL;
534
535 if (error) {
536 return error;
537 }
538
539 std::string actual_value;
540 if (!scoped_value->GetAsString(&actual_value)) {
541 return new Error(
542 kUnknownError,
543 base::StringPrintf("Attribute '%s' did not have a string value",
544 key.c_str()));
545 }
546
547 *result = LowerCaseEqualsASCII(actual_value, value.c_str());
548 return NULL;
549 }
550
551 Error* ElementValueCommand::DragAndDropFilePaths() const {
552 bool multiple = false;
553 Error* error = HasAttributeWithLowerCaseValueASCII("multiple", "true",
554 &multiple);
555
556 if (error) {
557 return error;
558 }
559
560 ListValue* path_list;
561 if (!GetListParameter("value", &path_list)) {
562 return new Error(kBadRequest, "Missing or invalid 'value' parameter");
563 }
564
565 if (!multiple && path_list->GetSize() > 1) {
566 return new Error(kBadRequest, "The element can not hold multiple files");
567 }
568
569 std::vector<std::string> paths;
570 for (size_t i = 0; i < path_list->GetSize(); ++i) {
571 FilePath::StringType path;
572 if (!path_list->GetString(i, &path)) {
573 return new Error(
574 kBadRequest,
575 base::StringPrintf("'value' list item #%" PRIuS " is not a string",
576 i + 1));
577 }
578
579 if (!file_util::PathExists(FilePath(path))) {
580 return new Error(
581 kBadRequest,
582 base::StringPrintf("'%s' does not exist on the file system",
583 path.c_str()));
584 }
585
586 paths.push_back(path);
kkania 2011/06/28 00:58:38 whoops, this won't work because path is a FilePath
nodchip 2011/06/28 02:30:49 Done.
587 }
588
589 gfx::Point location;
590 error = session_->GetClickableLocation(element, &location);
591 if (error) {
592 return error;
593 }
594
595 return session_->DragAndDropFilePaths(location, paths);
596 }
597
598 Error* ElementValueCommand::SendKeys() const {
501 ListValue* key_list; 599 ListValue* key_list;
502 if (!GetListParameter("value", &key_list)) { 600 if (!GetListParameter("value", &key_list)) {
503 response->SetError(new Error( 601 return new Error(kBadRequest, "Missing or invalid 'value' parameter");
504 kBadRequest, "Missing or invalid 'value' parameter"));
505 return;
506 } 602 }
603
507 // Flatten the given array of strings into one. 604 // Flatten the given array of strings into one.
508 string16 keys; 605 string16 keys;
509 for (size_t i = 0; i < key_list->GetSize(); ++i) { 606 for (size_t i = 0; i < key_list->GetSize(); ++i) {
510 string16 keys_list_part; 607 string16 keys_list_part;
511 key_list->GetString(i, &keys_list_part); 608 key_list->GetString(i, &keys_list_part);
512 for (size_t j = 0; j < keys_list_part.size(); ++j) { 609 for (size_t j = 0; j < keys_list_part.size(); ++j) {
513 if (CBU16_IS_SURROGATE(keys_list_part[j])) { 610 if (CBU16_IS_SURROGATE(keys_list_part[j])) {
514 response->SetError(new Error( 611 return new Error(kBadRequest,
515 kBadRequest, "ChromeDriver only supports characters in the BMP")); 612 "ChromeDriver only supports characters in the BMP");
516 return;
517 } 613 }
518 } 614 }
519 keys.append(keys_list_part); 615 keys.append(keys_list_part);
520 } 616 }
521 617
522 Error* error = session_->SendKeys(element, keys); 618 return session_->SendKeys(element, keys);
523 if (error)
524 response->SetError(error);
525 } 619 }
526 620
527 ///////////////////// ElementTextCommand //////////////////// 621 ///////////////////// ElementTextCommand ////////////////////
528 622
529 ElementTextCommand::ElementTextCommand( 623 ElementTextCommand::ElementTextCommand(
530 const std::vector<std::string>& path_segments, 624 const std::vector<std::string>& path_segments,
531 DictionaryValue* parameters) 625 DictionaryValue* parameters)
532 : WebElementCommand(path_segments, parameters) {} 626 : WebElementCommand(path_segments, parameters) {}
533 627
534 ElementTextCommand::~ElementTextCommand() {} 628 ElementTextCommand::~ElementTextCommand() {}
(...skipping 18 matching lines...) Expand all
553 return; 647 return;
554 } 648 }
555 if (!result->IsType(Value::TYPE_STRING)) { 649 if (!result->IsType(Value::TYPE_STRING)) {
556 response->SetError(new Error(kUnknownError, "Result is not string type")); 650 response->SetError(new Error(kUnknownError, "Result is not string type"));
557 return; 651 return;
558 } 652 }
559 response->SetValue(result.release()); 653 response->SetValue(result.release());
560 } 654 }
561 655
562 } // namespace webdriver 656 } // namespace webdriver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698