OLD | NEW |
---|---|
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" | |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/string_util.h" | |
8 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
9 #include "base/third_party/icu/icu_utf.h" | 11 #include "base/third_party/icu/icu_utf.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 #include "chrome/test/webdriver/commands/response.h" | 13 #include "chrome/test/webdriver/commands/response.h" |
12 #include "chrome/test/webdriver/session.h" | 14 #include "chrome/test/webdriver/session.h" |
13 #include "chrome/test/webdriver/webdriver_error.h" | 15 #include "chrome/test/webdriver/webdriver_error.h" |
14 #include "third_party/webdriver/atoms.h" | 16 #include "third_party/webdriver/atoms.h" |
15 #include "ui/gfx/point.h" | 17 #include "ui/gfx/point.h" |
16 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
17 | 19 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 } | 60 } |
59 | 61 |
60 void ElementAttributeCommand::ExecuteGet(Response* const response) { | 62 void ElementAttributeCommand::ExecuteGet(Response* const response) { |
61 // There should be at least 7 segments to match | 63 // There should be at least 7 segments to match |
62 // "/session/$session/element/$id/attribute/$name" | 64 // "/session/$session/element/$id/attribute/$name" |
63 if (path_segments_.size() < 7) { | 65 if (path_segments_.size() < 7) { |
64 response->SetError(new Error(kBadRequest, "Path segments is less than 7")); | 66 response->SetError(new Error(kBadRequest, "Path segments is less than 7")); |
65 return; | 67 return; |
66 } | 68 } |
67 | 69 |
68 std::string script = base::StringPrintf( | 70 const std::string key = path_segments_.at(6); |
69 "return (%s).apply(null, arguments);", atoms::GET_ATTRIBUTE); | 71 Value* value; |
70 | 72 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) { | 73 if (error) { |
78 response->SetError(error); | 74 response->SetError(error); |
79 return; | 75 return; |
80 } | 76 } |
81 response->SetValue(result); | 77 |
78 response->SetValue(value); | |
82 } | 79 } |
83 | 80 |
84 ///////////////////// ElementClearCommand //////////////////// | 81 ///////////////////// ElementClearCommand //////////////////// |
85 | 82 |
86 ElementClearCommand::ElementClearCommand( | 83 ElementClearCommand::ElementClearCommand( |
87 const std::vector<std::string>& path_segments, | 84 const std::vector<std::string>& path_segments, |
88 DictionaryValue* parameters) | 85 DictionaryValue* parameters) |
89 : WebElementCommand(path_segments, parameters) {} | 86 : WebElementCommand(path_segments, parameters) {} |
90 | 87 |
91 ElementClearCommand::~ElementClearCommand() {} | 88 ElementClearCommand::~ElementClearCommand() {} |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 if (!result->IsType(Value::TYPE_STRING) && | 488 if (!result->IsType(Value::TYPE_STRING) && |
492 !result->IsType(Value::TYPE_NULL)) { | 489 !result->IsType(Value::TYPE_NULL)) { |
493 response->SetError(new Error( | 490 response->SetError(new Error( |
494 kUnknownError, "Result is not string or null type")); | 491 kUnknownError, "Result is not string or null type")); |
495 return; | 492 return; |
496 } | 493 } |
497 response->SetValue(result.release()); | 494 response->SetValue(result.release()); |
498 } | 495 } |
499 | 496 |
500 void ElementValueCommand::ExecutePost(Response* const response) { | 497 void ElementValueCommand::ExecutePost(Response* const response) { |
498 bool is_input = false; | |
499 Error* error = HasAttributeWithLowerCaseValueASCII("tagName", "input", | |
500 &is_input); | |
501 if (error) { | |
502 response->SetError(error); | |
503 return; | |
504 } | |
505 | |
506 bool is_file = false; | |
507 error = HasAttributeWithLowerCaseValueASCII("type", "file", &is_file); | |
508 if (error) { | |
509 response->SetError(error); | |
510 return; | |
511 } | |
512 | |
513 // If the element is a file upload control, set the file paths to the element. | |
514 // Otherwise send the value to the element as key input. | |
515 if (is_input && is_file) { | |
516 error = DragAndDropFilePaths(); | |
517 } else { | |
518 error = SendKeys(); | |
519 } | |
520 | |
521 if (error) { | |
522 response->SetError(error); | |
523 return; | |
524 } | |
525 | |
526 response->SetStatus(kSuccess); | |
kkania
2011/06/01 16:16:41
this is the default status of the response, so you
nodchip
2011/06/02 07:15:53
Done.
| |
527 } | |
528 | |
529 Error* ElementValueCommand::HasAttributeWithLowerCaseValueASCII( | |
530 const std::string& key, const std::string& value, bool* result) const { | |
531 Value* unscoped_value = NULL; | |
532 Error* error = session_->GetAttribute(element, key, &unscoped_value); | |
533 scoped_ptr<Value> scoped_value(unscoped_value); | |
534 unscoped_value = NULL; | |
535 | |
536 if (error) { | |
537 return error; | |
538 } | |
539 | |
540 if (!scoped_value->IsType(Value::TYPE_STRING)) { | |
541 return new Error(kUnknownError, | |
542 "Could not get the attribute value as a string"); | |
543 } | |
544 | |
545 std::string actual_value; | |
546 if (!scoped_value->GetAsString(&actual_value)) { | |
kkania
2011/06/01 16:16:41
I believe GetAsString will do the same check as yo
nodchip
2011/06/02 07:15:53
Done.
| |
547 return new Error(kUnknownError, "Could not get the attribute value"); | |
kkania
2011/06/01 16:16:41
change error message to "Attribute '%s' did not ha
nodchip
2011/06/02 07:15:53
Done.
| |
548 } | |
549 | |
550 *result = LowerCaseEqualsASCII(actual_value, value.c_str()); | |
551 return NULL; | |
552 } | |
553 | |
554 Error* ElementValueCommand::DragAndDropFilePaths() const { | |
555 ListValue* path_list; | |
556 if (!GetListParameter("value", &path_list)) { | |
557 return new Error(kBadRequest, "Missing or invalid 'value' parameter"); | |
558 } | |
559 | |
560 std::vector<std::string> paths; | |
561 for (size_t i = 0; i < path_list->GetSize(); ++i) { | |
562 FilePath::StringType path; | |
563 if (!path_list->GetString(i, &path)) { | |
564 return new Error(kBadRequest, "Invalid element in 'value' parameter"); | |
kkania
2011/06/01 16:16:41
this is a bit unclear, how about including somethi
nodchip
2011/06/02 07:15:53
Done.
| |
565 } | |
566 | |
567 if (!file_util::PathExists(FilePath(path))) { | |
568 return new Error(kBadRequest, "Path does not exist"); | |
kkania
2011/06/01 16:16:41
put the path in the error msg
nodchip
2011/06/02 07:15:53
Done.
| |
569 } | |
570 | |
571 paths.push_back(path); | |
572 } | |
573 | |
574 gfx::Point location; | |
575 Error* error = session_->GetClickableLocation(element, &location); | |
576 if (error) { | |
577 return error; | |
578 } | |
579 | |
580 return session_->DragAndDropFilePaths(location, paths); | |
581 } | |
582 | |
583 Error* ElementValueCommand::SendKeys() const { | |
501 ListValue* key_list; | 584 ListValue* key_list; |
502 if (!GetListParameter("value", &key_list)) { | 585 if (!GetListParameter("value", &key_list)) { |
503 response->SetError(new Error( | 586 return new Error(kBadRequest, "Missing or invalid 'value' parameter"); |
504 kBadRequest, "Missing or invalid 'value' parameter")); | |
505 return; | |
506 } | 587 } |
588 | |
507 // Flatten the given array of strings into one. | 589 // Flatten the given array of strings into one. |
508 string16 keys; | 590 string16 keys; |
509 for (size_t i = 0; i < key_list->GetSize(); ++i) { | 591 for (size_t i = 0; i < key_list->GetSize(); ++i) { |
510 string16 keys_list_part; | 592 string16 keys_list_part; |
511 key_list->GetString(i, &keys_list_part); | 593 key_list->GetString(i, &keys_list_part); |
512 for (size_t j = 0; j < keys_list_part.size(); ++j) { | 594 for (size_t j = 0; j < keys_list_part.size(); ++j) { |
513 if (CBU16_IS_SURROGATE(keys_list_part[j])) { | 595 if (CBU16_IS_SURROGATE(keys_list_part[j])) { |
514 response->SetError(new Error( | 596 return new Error(kBadRequest, |
515 kBadRequest, "ChromeDriver only supports characters in the BMP")); | 597 "ChromeDriver only supports characters in the BMP"); |
516 return; | |
517 } | 598 } |
518 } | 599 } |
519 keys.append(keys_list_part); | 600 keys.append(keys_list_part); |
520 } | 601 } |
521 | 602 |
522 Error* error = session_->SendKeys(element, keys); | 603 return session_->SendKeys(element, keys); |
523 if (error) | |
524 response->SetError(error); | |
525 } | 604 } |
526 | 605 |
527 ///////////////////// ElementTextCommand //////////////////// | 606 ///////////////////// ElementTextCommand //////////////////// |
528 | 607 |
529 ElementTextCommand::ElementTextCommand( | 608 ElementTextCommand::ElementTextCommand( |
530 const std::vector<std::string>& path_segments, | 609 const std::vector<std::string>& path_segments, |
531 DictionaryValue* parameters) | 610 DictionaryValue* parameters) |
532 : WebElementCommand(path_segments, parameters) {} | 611 : WebElementCommand(path_segments, parameters) {} |
533 | 612 |
534 ElementTextCommand::~ElementTextCommand() {} | 613 ElementTextCommand::~ElementTextCommand() {} |
(...skipping 18 matching lines...) Expand all Loading... | |
553 return; | 632 return; |
554 } | 633 } |
555 if (!result->IsType(Value::TYPE_STRING)) { | 634 if (!result->IsType(Value::TYPE_STRING)) { |
556 response->SetError(new Error(kUnknownError, "Result is not string type")); | 635 response->SetError(new Error(kUnknownError, "Result is not string type")); |
557 return; | 636 return; |
558 } | 637 } |
559 response->SetValue(result.release()); | 638 response->SetValue(result.release()); |
560 } | 639 } |
561 | 640 |
562 } // namespace webdriver | 641 } // namespace webdriver |
OLD | NEW |