summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/webdriver_command.cc
blob: f861374a762dd481e875be69a5a363830ced95c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/webdriver/commands/webdriver_command.h"

#include <string>

#include "base/logging.h"
#include "base/singleton.h"
#include "base/string_util.h"
#include "base/values.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/automation/automation_proxy.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/session_manager.h"
#include "chrome/test/webdriver/utility_functions.h"

namespace webdriver {

const std::string WebDriverCommand::kElementDictionaryKey = "ELEMENT";

bool WebDriverCommand::IsElementIdDictionary(
    const DictionaryValue* const dictionary) {
  // Test that it has the element key and that it is a string.
  Value* element_id;
  return dictionary->Get(kElementDictionaryKey, &element_id) &&
         element_id->GetType() == Value::TYPE_STRING;
}

DictionaryValue* WebDriverCommand::GetElementIdAsDictionaryValue(
    const std::string& element_id) {
  DictionaryValue* dictionary = new DictionaryValue;
  dictionary->SetString(kElementDictionaryKey, element_id);
  return dictionary;
}

bool WebDriverCommand::Init(Response* const response) {
  // There should be at least 3 path segments to match "/session/$id".
  std::string session_id = GetPathVariable(2);
  if (session_id.length() == 0) {
    SET_WEBDRIVER_ERROR(response, "No session ID specified", kBadRequest);
    return false;
  }

  VLOG(1) << "Fetching session: " << session_id;
  session_ = SessionManager::GetInstance()->GetSession(session_id);
  if (session_ == NULL) {
    SET_WEBDRIVER_ERROR(response, "Session not found: " + session_id,
                        kSessionNotFound);
    return false;
  }

  response->SetField("sessionId", Value::CreateStringValue(session_id));
  return true;
}

}  // namespace webdriver