summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/set_timeout_commands.cc
blob: f0a8066083b58573d356f8831549af42a471cbfc (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2011 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/set_timeout_commands.h"

#include <string>

#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/webdriver_error.h"
#include "chrome/test/webdriver/webdriver_session.h"

namespace webdriver {

SetTimeoutCommand::SetTimeoutCommand(
    const std::vector<std::string>& path_segments,
    const DictionaryValue* const parameters) :
  WebDriverCommand(path_segments, parameters) {}

SetTimeoutCommand::~SetTimeoutCommand() {}

bool SetTimeoutCommand::DoesPost() {
  return true;
}

void SetTimeoutCommand::ExecutePost(Response* const response) {
  // Timeout value in milliseconds
  const char kTimeoutMsKey[] = "ms";

  if (!HasParameter(kTimeoutMsKey)) {
    response->SetError(new Error(kBadRequest, "Request missing ms parameter"));
    return;
  }

  int ms_to_wait;
  if (!GetIntegerParameter(kTimeoutMsKey, &ms_to_wait)) {
    // Client may have sent us a floating point number. Since DictionaryValue
    // will not do a down cast for us, we must explicitly check for it here.
    // Note webdriver only supports whole milliseconds for a timeout value, so
    // we are safe to downcast.
    double ms;
    if (!GetDoubleParameter(kTimeoutMsKey, &ms)) {
      response->SetError(new Error(
          kBadRequest, "ms parameter is not a number"));
      return;
    }
    ms_to_wait = static_cast<int>(ms);
  }

  // Validate the wait time before setting it to the session.
  if (ms_to_wait < 0) {
    response->SetError(new Error(kBadRequest, "Timeout must be non-negative"));
    return;
  }

  SetTimeout(ms_to_wait);
}

SetAsyncScriptTimeoutCommand::SetAsyncScriptTimeoutCommand(
    const std::vector<std::string>& path_segments,
    const DictionaryValue* const parameters)
    : SetTimeoutCommand(path_segments, parameters) {}

SetAsyncScriptTimeoutCommand::~SetAsyncScriptTimeoutCommand() {}

void SetAsyncScriptTimeoutCommand::SetTimeout(int timeout_ms) {
  session_->set_async_script_timeout(timeout_ms);
}

ImplicitWaitCommand::ImplicitWaitCommand(
    const std::vector<std::string>& path_segments,
    const DictionaryValue* const parameters)
    : SetTimeoutCommand(path_segments, parameters) {}

ImplicitWaitCommand::~ImplicitWaitCommand() {}

void ImplicitWaitCommand::SetTimeout(int timeout_ms) {
  session_->set_implicit_wait(timeout_ms);
}

}  // namespace webdriver