Home | History | Annotate | Download | only in commands
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/test/webdriver/commands/target_locator_commands.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/values.h"
      9 #include "chrome/test/webdriver/commands/response.h"
     10 #include "chrome/test/webdriver/webdriver_element_id.h"
     11 #include "chrome/test/webdriver/webdriver_error.h"
     12 #include "chrome/test/webdriver/webdriver_session.h"
     13 #include "chrome/test/webdriver/webdriver_util.h"
     14 
     15 namespace webdriver {
     16 
     17 WindowHandleCommand::WindowHandleCommand(
     18     const std::vector<std::string>& path_segments,
     19     base::DictionaryValue* parameters)
     20     : WebDriverCommand(path_segments, parameters) {}
     21 
     22 WindowHandleCommand::~WindowHandleCommand() {}
     23 
     24 bool WindowHandleCommand::DoesGet() {
     25   return true;
     26 }
     27 
     28 void WindowHandleCommand::ExecuteGet(Response* const response) {
     29   response->SetValue(new base::StringValue(
     30       WebViewIdToString(session_->current_target().view_id)));
     31 }
     32 
     33 WindowHandlesCommand::WindowHandlesCommand(
     34     const std::vector<std::string>& path_segments,
     35     base::DictionaryValue* parameters)
     36     : WebDriverCommand(path_segments, parameters) {}
     37 
     38 WindowHandlesCommand::~WindowHandlesCommand() {}
     39 
     40 bool WindowHandlesCommand::DoesGet() {
     41   return true;
     42 }
     43 
     44 void WindowHandlesCommand::ExecuteGet(Response* const response) {
     45   std::vector<WebViewInfo> views;
     46   Error* error = session_->GetViews(&views);
     47   if (error) {
     48     response->SetError(error);
     49     return;
     50   }
     51   base::ListValue* id_list = new base::ListValue();
     52   for (size_t i = 0; i < views.size(); ++i) {
     53     if (!views[i].view_id.IsTab() &&
     54         views[i].view_id.GetId().type() != AutomationId::kTypeAppShell)
     55       continue;
     56     id_list->Append(new base::StringValue(WebViewIdToString(views[i].view_id)));
     57   }
     58   response->SetValue(id_list);
     59 }
     60 
     61 WindowCommand::WindowCommand(
     62     const std::vector<std::string>& path_segments,
     63     base::DictionaryValue* parameters)
     64     : WebDriverCommand(path_segments, parameters) {}
     65 
     66 WindowCommand::~WindowCommand() {}
     67 
     68 bool WindowCommand::DoesPost() {
     69   return true;
     70 }
     71 
     72 bool WindowCommand::DoesDelete() {
     73   return true;
     74 }
     75 
     76 void WindowCommand::ExecutePost(Response* const response) {
     77   std::string name;
     78   if (!GetStringParameter("name", &name)) {
     79     response->SetError(new Error(
     80         kBadRequest, "Missing or invalid 'name' parameter"));
     81     return;
     82   }
     83 
     84   Error* error = session_->SwitchToView(name);
     85   if (error)
     86     response->SetError(error);
     87 }
     88 
     89 void WindowCommand::ExecuteDelete(Response* const response) {
     90   Error* error = session_->CloseWindow();
     91   if (error)
     92     response->SetError(error);
     93 }
     94 
     95 bool WindowCommand::ShouldRunPreAndPostCommandHandlers() {
     96   return false;
     97 }
     98 
     99 SwitchFrameCommand::SwitchFrameCommand(
    100     const std::vector<std::string>& path_segments,
    101     base::DictionaryValue* parameters)
    102     : WebDriverCommand(path_segments, parameters) {}
    103 
    104 SwitchFrameCommand::~SwitchFrameCommand() {}
    105 
    106 bool SwitchFrameCommand::DoesPost() {
    107   return true;
    108 }
    109 
    110 void SwitchFrameCommand::ExecutePost(Response* const response) {
    111   std::string id;
    112   int index = 0;
    113   ElementId element;
    114   Error* error = NULL;
    115   if (GetStringParameter("id", &id)) {
    116     error = session_->SwitchToFrameWithNameOrId(id);
    117   } else if (GetIntegerParameter("id", &index)) {
    118     error = session_->SwitchToFrameWithIndex(index);
    119   } else if (GetWebElementParameter("id", &element)) {
    120     error = session_->SwitchToFrameWithElement(element);
    121   } else if (IsNullParameter("id") || !HasParameter("id")) {
    122     // Treat null 'id' and no 'id' as the same.
    123     // See http://code.google.com/p/selenium/issues/detail?id=1479.
    124     session_->SwitchToTopFrame();
    125   } else {
    126     error = new Error(kBadRequest, "Invalid 'id' parameter");
    127   }
    128   if (error)
    129     response->SetError(error);
    130 }
    131 
    132 bool SwitchFrameCommand::GetWebElementParameter(const std::string& key,
    133                                                 ElementId* out) const {
    134   const base::DictionaryValue* value;
    135   if (!GetDictionaryParameter(key, &value))
    136     return false;
    137 
    138   ElementId id(value);
    139   if (!id.is_valid())
    140     return false;
    141 
    142   *out = id;
    143   return true;
    144 }
    145 
    146 ActiveElementCommand::ActiveElementCommand(
    147     const std::vector<std::string>& path_segments,
    148     base::DictionaryValue* parameters)
    149     : WebDriverCommand(path_segments, parameters) {}
    150 
    151 ActiveElementCommand::~ActiveElementCommand() {}
    152 
    153 bool ActiveElementCommand::DoesPost() {
    154   return true;
    155 }
    156 
    157 void ActiveElementCommand::ExecutePost(Response* const response) {
    158   base::ListValue args;
    159   base::Value* result = NULL;
    160   Error* error = session_->ExecuteScript(
    161       "return document.activeElement || document.body", &args, &result);
    162   if (error) {
    163     response->SetError(error);
    164     return;
    165   }
    166   response->SetValue(result);
    167 }
    168 
    169 }  // namespace webdriver
    170