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

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 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 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/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/third_party/icu/icu_utf.h" 9 #include "base/third_party/icu/icu_utf.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 if (!result->IsType(Value::TYPE_STRING) && 491 if (!result->IsType(Value::TYPE_STRING) &&
492 !result->IsType(Value::TYPE_NULL)) { 492 !result->IsType(Value::TYPE_NULL)) {
493 response->SetError(new Error( 493 response->SetError(new Error(
494 kUnknownError, "Result is not string or null type")); 494 kUnknownError, "Result is not string or null type"));
495 return; 495 return;
496 } 496 }
497 response->SetValue(result.release()); 497 response->SetValue(result.release());
498 } 498 }
499 499
500 void ElementValueCommand::ExecutePost(Response* const response) { 500 void ElementValueCommand::ExecutePost(Response* const response) {
501 std::string tag_name;
502 Error* error = GetElementAttribute("tagName", &tag_name);
503 if (error) {
504 response->SetError(error);
505 return;
506 }
507
508 std::string type;
509 error = GetElementAttribute("type", &type);
510 if (error) {
511 response->SetError(error);
512 return;
513 }
514
515 // If the element is a file upload control, set the file paths to the element.
516 // Otherwise send the value to the element as key input.
517 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.
518 SetFilePathsToFileUploadControl(response);
519 } else {
520 SendKeys(response);
521 }
522 }
523
524 void ElementValueCommand::SetFilePathsToFileUploadControl(
525 Response* const response) const {
526 ListValue* key_list;
kkania 2011/05/23 18:43:49 rename key_list
nodchip 2011/05/26 01:14:53 Done.
527 if (!GetListParameter("value", &key_list)) {
528 response->SetError(new Error(kBadRequest,
529 "Missing or invalid 'value' parameter"));
530 return;
531 }
532
533 std::vector<std::string> paths;
534 for (size_t i = 0; i < key_list->GetSize(); ++i) {
535 std::string path;
536 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.
537 paths.push_back(path);
538 }
539
540 bool is_displayed;
541 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.
542 session_->current_target(), element, &is_displayed);
543 if (error) {
544 response->SetError(error);
545 return;
546 }
547 if (!is_displayed) {
548 response->SetError(new Error(kElementNotVisible,
549 "Element must be displayed"));
550 return;
551 }
552
553 gfx::Point location;
554 error = session_->GetElementLocationInView(element, &location);
555 if (error) {
556 response->SetError(error);
557 return;
558 }
559
560 gfx::Size size;
561 error = session_->GetElementSize(session_->current_target(), element, &size);
562 if (error) {
563 response->SetError(error);
564 return;
565 }
566
567 location.Offset(size.width() / 2, size.height() / 2);
568
569 error = session_->SetFilePathsToFileUploadControl(location, paths);
570 if (error) {
571 response->SetError(error);
572 return;
573 }
574
575 response->SetStatus(kSuccess);
576 }
577
578 void ElementValueCommand::SendKeys(Response* const response) const {
501 ListValue* key_list; 579 ListValue* key_list;
502 if (!GetListParameter("value", &key_list)) { 580 if (!GetListParameter("value", &key_list)) {
503 response->SetError(new Error( 581 response->SetError(new Error(
504 kBadRequest, "Missing or invalid 'value' parameter")); 582 kBadRequest, "Missing or invalid 'value' parameter"));
505 return; 583 return;
506 } 584 }
585
507 // Flatten the given array of strings into one. 586 // Flatten the given array of strings into one.
508 string16 keys; 587 string16 keys;
509 for (size_t i = 0; i < key_list->GetSize(); ++i) { 588 for (size_t i = 0; i < key_list->GetSize(); ++i) {
510 string16 keys_list_part; 589 string16 keys_list_part;
511 key_list->GetString(i, &keys_list_part); 590 key_list->GetString(i, &keys_list_part);
512 for (size_t j = 0; j < keys_list_part.size(); ++j) { 591 for (size_t j = 0; j < keys_list_part.size(); ++j) {
513 if (CBU16_IS_SURROGATE(keys_list_part[j])) { 592 if (CBU16_IS_SURROGATE(keys_list_part[j])) {
514 response->SetError(new Error( 593 response->SetError(new Error(
515 kBadRequest, "ChromeDriver only supports characters in the BMP")); 594 kBadRequest, "ChromeDriver only supports characters in the BMP"));
516 return; 595 return;
517 } 596 }
518 } 597 }
519 keys.append(keys_list_part); 598 keys.append(keys_list_part);
520 } 599 }
521 600
522 Error* error = session_->SendKeys(element, keys); 601 Error* error = session_->SendKeys(element, keys);
523 if (error) 602 if (error)
524 response->SetError(error); 603 response->SetError(error);
525 } 604 }
526 605
606 Error* ElementValueCommand::GetElementAttribute(const std::string& key,
607 std::string* value) const {
608 std::string script = base::StringPrintf(
609 "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.
610
611 scoped_ptr<ListValue> args(new ListValue);
612 args->Append(element.ToValue());
613 args->Append(Value::CreateStringValue(key));
614
615 Value* result = NULL;
616 Error* error = session_->ExecuteScript(script, args.get(), &result);
617 if (error) {
618 return error;
619 }
620
621 if (result) {
622 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.
623 delete result;
kkania 2011/05/23 18:43:49 use scoped_ptr
nodchip 2011/05/26 01:14:53 Done.
624 result = NULL;
625 }
626
627 return NULL;
628 }
629
527 ///////////////////// ElementTextCommand //////////////////// 630 ///////////////////// ElementTextCommand ////////////////////
528 631
529 ElementTextCommand::ElementTextCommand( 632 ElementTextCommand::ElementTextCommand(
530 const std::vector<std::string>& path_segments, 633 const std::vector<std::string>& path_segments,
531 DictionaryValue* parameters) 634 DictionaryValue* parameters)
532 : WebElementCommand(path_segments, parameters) {} 635 : WebElementCommand(path_segments, parameters) {}
533 636
534 ElementTextCommand::~ElementTextCommand() {} 637 ElementTextCommand::~ElementTextCommand() {}
535 638
536 bool ElementTextCommand::DoesGet() { 639 bool ElementTextCommand::DoesGet() {
(...skipping 16 matching lines...) Expand all
553 return; 656 return;
554 } 657 }
555 if (!result->IsType(Value::TYPE_STRING)) { 658 if (!result->IsType(Value::TYPE_STRING)) {
556 response->SetError(new Error(kUnknownError, "Result is not string type")); 659 response->SetError(new Error(kUnknownError, "Result is not string type"));
557 return; 660 return;
558 } 661 }
559 response->SetValue(result.release()); 662 response->SetValue(result.release());
560 } 663 }
561 664
562 } // namespace webdriver 665 } // namespace webdriver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698