Пример #1
0
void IECommandExecutor::DispatchCommand() {
  Response response(this->session_id_);
  CommandHandlerMap::const_iterator found_iterator = 
      this->command_handlers_.find(this->current_command_.command_type());

  if (found_iterator == this->command_handlers_.end()) {
    response.SetErrorResponse(501, "Command not implemented");
  } else {
    BrowserHandle browser;
    int status_code = this->GetCurrentBrowser(&browser);
    if (status_code == SUCCESS) {
      bool alert_is_active = false;
      HWND alert_handle = browser->GetActiveDialogWindowHandle();
      if (alert_handle != NULL) {
        // Found a window handle, make sure it's an actual alert,
        // and not a showModalDialog() window.
        vector<char> window_class_name(34);
        ::GetClassNameA(alert_handle, &window_class_name[0], 34);
        if (strcmp("#32770", &window_class_name[0]) == 0) {
          alert_is_active = true;
        }
      }
      int command_type = this->current_command_.command_type();
      if (alert_is_active &&
          command_type != GetAlertText &&
          command_type != SendKeysToAlert &&
          command_type != AcceptAlert &&
          command_type != DismissAlert) {
        response.SetErrorResponse(EMODALDIALOGOPENED, "Modal dialog present");
        ::SendMessage(alert_handle, WM_COMMAND, IDCANCEL, NULL);
        this->serialized_response_ = response.Serialize();
        return;
      }
    }

	CommandHandlerHandle command_handler = found_iterator->second;
    command_handler->Execute(*this, this->current_command_, &response);

    status_code = this->GetCurrentBrowser(&browser);
    if (status_code == SUCCESS) {
      this->is_waiting_ = browser->wait_required();
      if (this->is_waiting_) {
        ::PostMessage(this->m_hWnd, WD_WAIT, NULL, NULL);
      }
    }
  }

  this->serialized_response_ = response.Serialize();
}
void IECommandExecutor::DispatchCommand() {
  LOG(TRACE) << "Entering IECommandExecutor::DispatchCommand";

  Response response(this->session_id_);
  CommandHandlerMap::const_iterator found_iterator = 
      this->command_handlers_.find(this->current_command_.command_type());

  if (found_iterator == this->command_handlers_.end()) {
    LOG(WARN) << "Unable to find command handler for " << this->current_command_.command_type();
    response.SetErrorResponse(501, "Command not implemented");
  } else {
    BrowserHandle browser;
    int status_code = WD_SUCCESS;
    if (this->current_command_.command_type() != webdriver::CommandType::NewSession) {
      // There should never be a modal dialog or alert to check for if the command
      // is the "newSession" command.
      status_code = this->GetCurrentBrowser(&browser);
      if (status_code == WD_SUCCESS) {
        bool alert_is_active = false;
        HWND alert_handle = browser->GetActiveDialogWindowHandle();
        if (alert_handle != NULL) {
          // Found a window handle, make sure it's an actual alert,
          // and not a showModalDialog() window.
          vector<char> window_class_name(34);
          ::GetClassNameA(alert_handle, &window_class_name[0], 34);
          if (strcmp(ALERT_WINDOW_CLASS, &window_class_name[0]) == 0) {
            alert_is_active = true;
          } else {
            LOG(WARN) << "Found alert handle does not have a window class consistent with an alert";
          }
        } else {
          LOG(DEBUG) << "No alert handle is found";
        }
        if (alert_is_active) {
          Alert dialog(browser, alert_handle);
          std::string command_type = this->current_command_.command_type();
          if (command_type == webdriver::CommandType::GetAlertText ||
              command_type == webdriver::CommandType::SendKeysToAlert ||
              command_type == webdriver::CommandType::AcceptAlert ||
              command_type == webdriver::CommandType::DismissAlert) {
            LOG(DEBUG) << "Alert is detected, and the sent command is valid";
          } else {
            LOG(DEBUG) << "Unexpected alert is detected, and the sent command is invalid when an alert is present";
            std::string alert_text = dialog.GetText();
            if (this->unexpected_alert_behavior_ == ACCEPT_UNEXPECTED_ALERTS) {
              LOG(DEBUG) << "Automatically accepting the alert";
              dialog.Accept();
            } else if (this->unexpected_alert_behavior_ == DISMISS_UNEXPECTED_ALERTS || command_type == webdriver::CommandType::Quit) {
              // If a quit command was issued, we should not ignore an unhandled
              // alert, even if the alert behavior is set to "ignore".
              LOG(DEBUG) << "Automatically dismissing the alert";
              if (dialog.is_standard_alert()) {
                dialog.Dismiss();
              } else {
                // The dialog was non-standard. The most common case of this is
                // an onBeforeUnload dialog, which must be accepted to continue.
                dialog.Accept();
              }
            }
            if (command_type != webdriver::CommandType::Quit) {
              // To keep pace with what Firefox does, we'll return the text of the
              // alert in the error response.
              Json::Value response_value;
              response_value["message"] = "Modal dialog present";
              response_value["alert"]["text"] = alert_text;
              response.SetResponse(EMODALDIALOGOPENED, response_value);
              this->serialized_response_ = response.Serialize();
              return;
            } else {
              LOG(DEBUG) << "Quit command was issued. Continuing with command after automatically closing alert.";
            }
          }
        }
      } else {
        LOG(WARN) << "Unable to find current browser";
      }
    }
	  CommandHandlerHandle command_handler = found_iterator->second;
    command_handler->Execute(*this, this->current_command_, &response);

    status_code = this->GetCurrentBrowser(&browser);
    if (status_code == WD_SUCCESS) {
      this->is_waiting_ = browser->wait_required();
      if (this->is_waiting_) {
        if (this->page_load_timeout_ >= 0) {
          this->wait_timeout_ = clock() + (this->page_load_timeout_ / 1000 * CLOCKS_PER_SEC);
        }
        ::PostMessage(this->m_hWnd, WD_WAIT, NULL, NULL);
      }
    } else {
      if (this->current_command_.command_type() != webdriver::CommandType::Quit) {
        LOG(WARN) << "Unable to get current browser";
      }
    }
  }

  this->serialized_response_ = response.Serialize();
}