| 991 | } |
| 992 | |
| 993 | void IECommandExecutor::DispatchCommand() { |
| 994 | LOG(TRACE) << "Entering IECommandExecutor::DispatchCommand"; |
| 995 | |
| 996 | Response response; |
| 997 | |
| 998 | if (!this->command_handlers_->IsValidCommand(this->current_command_.command_type())) { |
| 999 | LOG(WARN) << "Unable to find command handler for " << this->current_command_.command_type(); |
| 1000 | response.SetErrorResponse(ERROR_UNKNOWN_COMMAND, "Command not implemented"); |
| 1001 | } else if (!this->current_command_.is_valid_parameters()) { |
| 1002 | response.SetErrorResponse(ERROR_INVALID_ARGUMENT, "parameters property of command is not a valid JSON object"); |
| 1003 | } else { |
| 1004 | BrowserHandle browser; |
| 1005 | int status_code = WD_SUCCESS; |
| 1006 | std::string command_type = this->current_command_.command_type(); |
| 1007 | if (command_type != webdriver::CommandType::NewSession) { |
| 1008 | // There should never be a modal dialog or alert to check for if the command |
| 1009 | // is the "newSession" command. |
| 1010 | status_code = this->GetCurrentBrowser(&browser); |
| 1011 | if (status_code == WD_SUCCESS) { |
| 1012 | LOG(DEBUG) << "Checking for alert before executing " << command_type << " command"; |
| 1013 | HWND alert_handle = NULL; |
| 1014 | bool alert_is_active = this->IsAlertActive(browser, &alert_handle); |
| 1015 | if (alert_is_active) { |
| 1016 | if (this->IsCommandValidWithAlertPresent()) { |
| 1017 | LOG(DEBUG) << "Alert is detected, and the sent command is valid"; |
| 1018 | } else { |
| 1019 | LOG(DEBUG) << "Unexpected alert is detected, and the sent command " |
| 1020 | << "is invalid when an alert is present"; |
| 1021 | bool is_quit_command = command_type == webdriver::CommandType::Quit; |
| 1022 | |
| 1023 | std::string alert_text; |
| 1024 | bool is_notify_unexpected_alert = this->HandleUnexpectedAlert(browser, |
| 1025 | alert_handle, |
| 1026 | is_quit_command, |
| 1027 | &alert_text); |
| 1028 | if (!is_quit_command) { |
| 1029 | if (is_notify_unexpected_alert) { |
| 1030 | // To keep pace with what the specification suggests, we'll |
| 1031 | // return the text of the alert in the error response. |
| 1032 | response.SetErrorResponse(EUNEXPECTEDALERTOPEN, |
| 1033 | "Modal dialog present with text: " + alert_text); |
| 1034 | response.AddAdditionalData("text", alert_text); |
| 1035 | this->serialized_response_ = response.Serialize(); |
| 1036 | return; |
| 1037 | } else { |
| 1038 | LOG(DEBUG) << "Command other than quit was issued, and option " |
| 1039 | << "to not notify was specified. Continuing with " |
| 1040 | << "command after automatically closing alert."; |
| 1041 | // Push a wait cycle, then re-execute the current command (which |
| 1042 | // hasn't actually been executed yet). Note that an empty string |
| 1043 | // for the deferred response parameter of CreateWaitThread will |
| 1044 | // re-queue the execution of the command. |
| 1045 | this->CreateWaitThread("", true); |
| 1046 | return; |
| 1047 | } |
| 1048 | } else { |
| 1049 | LOG(DEBUG) << "Quit command was issued. Continuing with " |
| 1050 | << "command after automatically closing alert."; |
no test coverage detected