Chromium Code Reviews| 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" | |
| 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 Loading... | |
| 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 Loading... | |
| 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 ListValue* path_list; | |
| 553 if (!GetListParameter("value", &path_list)) { | |
| 554 return new Error(kBadRequest, "Missing or invalid 'value' parameter"); | |
| 555 } | |
| 556 | |
| 557 std::vector<std::string> paths; | |
| 558 for (size_t i = 0; i < path_list->GetSize(); ++i) { | |
|
kkania
2011/06/15 02:33:12
if there are multiple paths and the file input ele
nodchip
2011/06/15 07:58:27
Done.
| |
| 559 FilePath::StringType path; | |
| 560 if (!path_list->GetString(i, &path)) { | |
| 561 return new Error( | |
| 562 kBadRequest, | |
| 563 base::StringPrintf("'value' list item #%" PRIuS " is not a string", | |
| 564 i + 1)); | |
| 565 } | |
| 566 | |
| 567 if (!file_util::PathExists(FilePath(path))) { | |
| 568 return new Error( | |
| 569 kBadRequest, | |
| 570 base::StringPrintf("'%s' does not exist on the file system", | |
| 571 path.c_str())); | |
| 572 } | |
| 573 | |
| 574 paths.push_back(path); | |
| 575 } | |
| 576 | |
| 577 gfx::Point location; | |
| 578 Error* error = session_->GetClickableLocation(element, &location); | |
| 579 if (error) { | |
| 580 return error; | |
| 581 } | |
| 582 | |
| 583 return session_->DragAndDropFilePaths(location, paths); | |
| 584 } | |
| 585 | |
| 586 Error* ElementValueCommand::SendKeys() const { | |
| 501 ListValue* key_list; | 587 ListValue* key_list; |
| 502 if (!GetListParameter("value", &key_list)) { | 588 if (!GetListParameter("value", &key_list)) { |
| 503 response->SetError(new Error( | 589 return new Error(kBadRequest, "Missing or invalid 'value' parameter"); |
| 504 kBadRequest, "Missing or invalid 'value' parameter")); | |
| 505 return; | |
| 506 } | 590 } |
| 591 | |
| 507 // Flatten the given array of strings into one. | 592 // Flatten the given array of strings into one. |
| 508 string16 keys; | 593 string16 keys; |
| 509 for (size_t i = 0; i < key_list->GetSize(); ++i) { | 594 for (size_t i = 0; i < key_list->GetSize(); ++i) { |
| 510 string16 keys_list_part; | 595 string16 keys_list_part; |
| 511 key_list->GetString(i, &keys_list_part); | 596 key_list->GetString(i, &keys_list_part); |
| 512 for (size_t j = 0; j < keys_list_part.size(); ++j) { | 597 for (size_t j = 0; j < keys_list_part.size(); ++j) { |
| 513 if (CBU16_IS_SURROGATE(keys_list_part[j])) { | 598 if (CBU16_IS_SURROGATE(keys_list_part[j])) { |
| 514 response->SetError(new Error( | 599 return new Error(kBadRequest, |
| 515 kBadRequest, "ChromeDriver only supports characters in the BMP")); | 600 "ChromeDriver only supports characters in the BMP"); |
| 516 return; | |
| 517 } | 601 } |
| 518 } | 602 } |
| 519 keys.append(keys_list_part); | 603 keys.append(keys_list_part); |
| 520 } | 604 } |
| 521 | 605 |
| 522 Error* error = session_->SendKeys(element, keys); | 606 return session_->SendKeys(element, keys); |
| 523 if (error) | |
| 524 response->SetError(error); | |
| 525 } | 607 } |
| 526 | 608 |
| 527 ///////////////////// ElementTextCommand //////////////////// | 609 ///////////////////// ElementTextCommand //////////////////// |
| 528 | 610 |
| 529 ElementTextCommand::ElementTextCommand( | 611 ElementTextCommand::ElementTextCommand( |
| 530 const std::vector<std::string>& path_segments, | 612 const std::vector<std::string>& path_segments, |
| 531 DictionaryValue* parameters) | 613 DictionaryValue* parameters) |
| 532 : WebElementCommand(path_segments, parameters) {} | 614 : WebElementCommand(path_segments, parameters) {} |
| 533 | 615 |
| 534 ElementTextCommand::~ElementTextCommand() {} | 616 ElementTextCommand::~ElementTextCommand() {} |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 553 return; | 635 return; |
| 554 } | 636 } |
| 555 if (!result->IsType(Value::TYPE_STRING)) { | 637 if (!result->IsType(Value::TYPE_STRING)) { |
| 556 response->SetError(new Error(kUnknownError, "Result is not string type")); | 638 response->SetError(new Error(kUnknownError, "Result is not string type")); |
| 557 return; | 639 return; |
| 558 } | 640 } |
| 559 response->SetValue(result.release()); | 641 response->SetValue(result.release()); |
| 560 } | 642 } |
| 561 | 643 |
| 562 } // namespace webdriver | 644 } // namespace webdriver |
| OLD | NEW |