Home | History | Annotate | Download | only in webdriver
      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/webdriver_error.h"
      6 
      7 #include <sstream>
      8 
      9 #include "chrome/common/automation_constants.h"
     10 
     11 namespace webdriver {
     12 
     13 namespace {
     14 
     15 // Returns the string equivalent of the given |ErrorCode|.
     16 const char* DefaultMessageForErrorCode(ErrorCode code) {
     17   switch (code) {
     18     case kSuccess:
     19       return "Success";
     20     case kNoSuchElement:
     21       return "The element could not be found";
     22     case kNoSuchFrame:
     23       return "The frame could not be found";
     24     case kUnknownCommand:
     25       return "Unknown command";
     26     case kStaleElementReference:
     27       return "Element reference is invalid";
     28     case kElementNotVisible:
     29       return "Element is not visible";
     30     case kInvalidElementState:
     31       return "Element is in an invalid state";
     32     case kUnknownError:
     33       return "Unknown error";
     34     case kElementNotSelectable:
     35       return "Element is not selectable";
     36     case kXPathLookupError:
     37       return "XPath lookup error";
     38     case kNoSuchWindow:
     39       return "The window could not be found";
     40     case kInvalidCookieDomain:
     41       return "The cookie domain is invalid";
     42     case kUnableToSetCookie:
     43       return "Unable to set cookie";
     44     case kUnexpectedAlertOpen:
     45       return "An open modal dialog blocked the operation";
     46     case kNoAlertOpenError:
     47       return "No JavaScript modal dialog is open";
     48     default:
     49       return "<unknown>";
     50   }
     51 }
     52 
     53 }  // namespace
     54 
     55 // static
     56 Error* Error::FromAutomationError(const automation::Error& error) {
     57   ErrorCode code = kUnknownError;
     58   switch (error.code()) {
     59     case automation::kNoJavaScriptModalDialogOpen:
     60       code = kNoAlertOpenError;
     61       break;
     62     case automation::kBlockedByModalDialog:
     63       code = kUnexpectedAlertOpen;
     64       break;
     65     case automation::kInvalidId:
     66       code = kNoSuchWindow;
     67     default:
     68       break;
     69   }
     70 
     71   // In Chrome 17 and before, the automation error was just a string.
     72   // Compare against some strings that correspond to webdriver errors.
     73   // TODO(kkania): Remove these comparisons when Chrome 17 is unsupported.
     74   if (code == kUnknownError) {
     75     if (error.message() ==
     76             "Command cannot be performed because a modal dialog is active" ||
     77         error.message() ==
     78             "Could not complete script execution because a modal "
     79                 "dialog is active") {
     80       code = kUnexpectedAlertOpen;
     81     } else if (error.message() == "No modal dialog is showing" ||
     82                error.message() == "No JavaScript modal dialog is showing") {
     83       code = kNoAlertOpenError;
     84     }
     85   }
     86 
     87   // If the automation error code did not refer to a webdriver error code
     88   // (besides unknown), include the error message from chrome. Otherwise,
     89   // include the webdriver error code and the webdriver error message.
     90   if (code == kUnknownError) {
     91     return new Error(code, error.message());
     92   } else {
     93     return new Error(code);
     94   }
     95 }
     96 
     97 Error::Error(ErrorCode code)
     98     : code_(code),
     99       details_(DefaultMessageForErrorCode(code)) {
    100 }
    101 
    102 Error::Error(ErrorCode code, const std::string& details)
    103     : code_(code), details_(details) {
    104 }
    105 
    106 Error::~Error() {
    107 }
    108 
    109 void Error::AddDetails(const std::string& details) {
    110   details_ = details + ";\n " + details_;
    111 }
    112 
    113 ErrorCode Error::code() const {
    114   return code_;
    115 }
    116 
    117 const std::string& Error::details() const {
    118   return details_;
    119 }
    120 
    121 }  // namespace webdriver
    122