summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/webdriver_error.cc
blob: 0884e100bf871ec25069aabd809c881db5494ad6 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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/webdriver_error.h"

#include <sstream>

#include "chrome/common/automation_constants.h"

namespace webdriver {

namespace {

// Returns the string equivalent of the given |ErrorCode|.
const char* DefaultMessageForErrorCode(ErrorCode code) {
  switch (code) {
    case kSuccess:
      return "Success";
    case kNoSuchElement:
      return "The element could not be found";
    case kNoSuchFrame:
      return "The frame could not be found";
    case kUnknownCommand:
      return "Unknown command";
    case kStaleElementReference:
      return "Element reference is invalid";
    case kElementNotVisible:
      return "Element is not visible";
    case kInvalidElementState:
      return "Element is in an invalid state";
    case kUnknownError:
      return "Unknown error";
    case kElementNotSelectable:
      return "Element is not selectable";
    case kXPathLookupError:
      return "XPath lookup error";
    case kNoSuchWindow:
      return "The window could not be found";
    case kInvalidCookieDomain:
      return "The cookie domain is invalid";
    case kUnableToSetCookie:
      return "Unable to set cookie";
    case kUnexpectedAlertOpen:
      return "An open modal dialog blocked the operation";
    case kNoAlertOpenError:
      return "No JavaScript modal dialog is open";
    default:
      return "<unknown>";
  }
}

}  // namespace

// static
Error* Error::FromAutomationError(const automation::Error& error) {
  ErrorCode code = kUnknownError;
  switch (error.code()) {
    case automation::kNoJavaScriptModalDialogOpen:
      code = kNoAlertOpenError;
      break;
    case automation::kBlockedByModalDialog:
      code = kUnexpectedAlertOpen;
      break;
    default:
      break;
  }

  // In Chrome 17 and before, the automation error was just a string.
  // Compare against some strings that correspond to webdriver errors.
  // TODO(kkania): Remove these comparisons when Chrome 17 is unsupported.
  if (code == kUnknownError) {
    if (error.message() ==
            "Command cannot be performed because a modal dialog is active" ||
        error.message() ==
            "Could not complete script execution because a modal "
                "dialog is active") {
      code = kUnexpectedAlertOpen;
    } else if (error.message() == "No modal dialog is showing" ||
               error.message() == "No JavaScript modal dialog is showing") {
      code = kNoAlertOpenError;
    }
  }

  // If the automation error code did not refer to a webdriver error code
  // (besides unknown), include the error message from chrome. Otherwise,
  // include the webdriver error code and the webdriver error message.
  if (code == kUnknownError) {
    return new Error(code, error.message());
  } else {
    return new Error(code);
  }
}

Error::Error(ErrorCode code)
    : code_(code),
      details_(DefaultMessageForErrorCode(code)) {
}

Error::Error(ErrorCode code, const std::string& details)
    : code_(code), details_(details) {
}

Error::~Error() {
}

void Error::AddDetails(const std::string& details) {
  details_ = details + ";\n " + details_;
}

ErrorCode Error::code() const {
  return code_;
}

const std::string& Error::details() const {
  return details_;
}

}  // namespace webdriver