void ActionsCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get current browser");
    return;
  }
  ParametersMap::const_iterator actions_parameter_iterator = command_parameters.find("actions");
  if (actions_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: actions");
    return;
  }
  if (!actions_parameter_iterator->second.isArray()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Actions value is not an array");
    return;
  }
  std::string error_info = "";
  status_code = executor.input_manager()->PerformInputSequence(browser_wrapper,
                                                               actions_parameter_iterator->second,
                                                               &error_info);
  if (status_code != WD_SUCCESS) {
    if (status_code == EMOVETARGETOUTOFBOUNDS) {
      response->SetErrorResponse(status_code, error_info);
    } else {
      response->SetErrorResponse(status_code, "Unexpected error performing action sequence.");
    }
    return;
  }
  response->SetSuccessResponse(Json::Value::null);
}
  void ScreenshotCommandHandler::ExecuteInternal(
      const IECommandExecutor& executor,
      const ParametersMap& command_parameters,
      Response* response) {
    LOG(TRACE) << "Entering ScreenshotCommandHandler::ExecuteInternal";

    BrowserHandle browser_wrapper;
    int status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code, "Unable to get browser");
      return;
    }

    bool isSameColour = true;
    HRESULT hr;
    int i = 0;
    int tries = 4;
    const bool should_resize_window = executor.enable_full_page_screenshot();
    do {
      this->ClearImage();

      this->image_ = new CImage();
      if (should_resize_window) {
        hr = this->CaptureFullPage(browser_wrapper);
      } else {
        hr = this->CaptureViewport(browser_wrapper);
      }
      if (FAILED(hr)) {
        LOGHR(WARN, hr) << "Failed to capture browser image at " << i << " try";
        this->ClearImage();
        response->SetSuccessResponse("");
        return;
      }

      isSameColour = IsSameColour();
      if (isSameColour) {
        ::Sleep(2000);
        LOG(DEBUG) << "Failed to capture non single color browser image at " << i << " try";
      }

      i++;
    } while ((i < tries) && isSameColour);

    // now either correct or single color image is got
    std::string base64_screenshot = "";
    hr = this->GetBase64Data(base64_screenshot);
    if (FAILED(hr)) {
      LOGHR(WARN, hr) << "Unable to transform browser image to Base64 format";
      this->ClearImage();
      response->SetSuccessResponse("");
      return;
    }

    this->ClearImage();
    response->SetSuccessResponse(base64_screenshot);
  }
示例#3
0
int IECommandHandler::GetElement(const IECommandExecutor& executor,
                                 const std::string& element_id,
                                 ElementHandle* element_wrapper) {
  int status_code = EOBSOLETEELEMENT;
  ElementHandle candidate_wrapper;
  int result = executor.GetManagedElement(element_id, &candidate_wrapper);
  if (result != SUCCESS) {
    status_code = 404;
  } else {
    // Verify that the element is still valid by walking up the
    // DOM tree until we find no parent or the html tag
    CComPtr<IHTMLElement> parent(candidate_wrapper->element());
    while (parent) {
      CComQIPtr<IHTMLHtmlElement> html(parent);
      if (html) {
        status_code = SUCCESS;
        *element_wrapper = candidate_wrapper;
        break;
      }

      CComPtr<IHTMLElement> next;
      HRESULT hr = parent->get_parentElement(&next);
      if (FAILED(hr)) {
        //std::cout << hr << " [" << (_bstr_t(_com_error((DWORD) hr).ErrorMessage())) << "]";
      }

      if (next == NULL) {
        BSTR tag;
        parent->get_tagName(&tag);
        //std::cout << "Found null parent of element with tag " << _bstr_t(tag);
      }
      parent = next;
    }

    if (status_code != SUCCESS) {
      IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
      mutable_executor.RemoveManagedElement(element_id);
    } else {
      // If the element is attached to the DOM, validate that its document
      // is the currently-focused document (via frames).
      BrowserHandle current_browser;
      executor.GetCurrentBrowser(&current_browser);
      CComPtr<IHTMLDocument2> focused_doc;
      current_browser->GetDocument(&focused_doc);

      CComPtr<IDispatch> parent_doc_dispatch;
      parent->get_document(&parent_doc_dispatch);

      if (!focused_doc.IsEqualObject(parent_doc_dispatch)) {
        status_code = EOBSOLETEELEMENT;
      }
    }
  }

  return status_code;
}
void GetTimeoutsCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  Json::Value response_value;
  response_value["implicit"] = executor.implicit_wait_timeout();
  response_value["script"] = executor.async_script_timeout();
  response_value["pageLoad"] = executor.page_load_timeout();
  response->SetSuccessResponse(response_value);
}
void SetWindowPositionCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("windowHandle");
  ParametersMap::const_iterator x_parameter_iterator = command_parameters.find("x");
  ParametersMap::const_iterator y_parameter_iterator = command_parameters.find("y");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(400, "Missing parameter in URL: windowHandle");
    return;
  } else if (x_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(400, "Missing parameter: x");
    return;
  } else if (y_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(400, "Missing parameter: y");
    return;
  } else {
    int status_code = WD_SUCCESS;
    int x = x_parameter_iterator->second.asInt();
    int y = y_parameter_iterator->second.asInt();
    std::string window_id = id_parameter_iterator->second.asString();

    BrowserHandle browser_wrapper;
    if (window_id == "current") {
      status_code = executor.GetCurrentBrowser(&browser_wrapper);
    } else {
      status_code = executor.GetManagedBrowser(window_id, &browser_wrapper);
    }
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code, "Error retrieving window with handle " + window_id);
      return;
    }

    HWND window_handle = browser_wrapper->GetTopLevelWindowHandle();
    POINT desired_location;
    desired_location.x = x;
    desired_location.y = y;
      
    BOOL set_window_pos_result = ::SetWindowPos(window_handle, NULL, desired_location.x, desired_location.y, 0, 0, SWP_NOSIZE);
    if (!set_window_pos_result) {
      response->SetErrorResponse(EUNHANDLEDERROR,
                                  "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }

    response->SetSuccessResponse(Json::Value::null);
  }
}
void GetAllCookiesCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  Json::Value response_value(Json::arrayValue);
  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get browser");
    return;
  }

  std::vector<BrowserCookie> cookies;
  status_code = browser_wrapper->cookie_manager()->GetCookies(
      browser_wrapper->GetCurrentUrl(),
      &cookies);
  if (status_code == EUNHANDLEDERROR) {
    std::string error = "Could not retrieve cookies. The most common cause ";
    error.append("of this error is a mismatch in the bitness between the ");
    error.append("driver and browser. In particular, be sure you are not ");
    error.append("attempting to use a 64-bit IEDriverServer.exe against ");
    error.append("IE 10 or 11, even on 64-bit Windows.");
    response->SetErrorResponse(status_code, error);
    return;
  }
  std::vector<BrowserCookie>::iterator it = cookies.begin();
  for (; it != cookies.end(); ++it) {
    response_value.append(it->ToJson());
  }

  response->SetSuccessResponse(response_value);
}
void IsElementSelectedCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
    return;
  } else {
    std::string element_id = id_parameter_iterator->second.asString();

    BrowserHandle browser_wrapper;
    int status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");
      return;
    }

    ElementHandle element_wrapper;
    status_code = this->GetElement(executor, element_id, &element_wrapper);
    if (status_code == WD_SUCCESS) {
      bool is_selected = element_wrapper->IsSelected();
      response->SetSuccessResponse(is_selected);
    } else {
      response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE, "Element is no longer valid");
      return;
    }
  }
}
void AcceptAlertCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get current browser");
    return;
  }
  // This sleep is required to give IE time to draw the dialog.
  ::Sleep(100);
  HWND alert_handle = browser_wrapper->GetActiveDialogWindowHandle();
  if (alert_handle == NULL) {
    response->SetErrorResponse(ENOSUCHALERT, "No alert is active");
  } else {
    Alert dialog(browser_wrapper, alert_handle);
    status_code = dialog.Accept();
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code,
                                 "Could not find OK button");
    }

    // Add sleep to give IE time to close dialog and start Navigation if it's necessary
    ::Sleep(100);
    browser_wrapper->set_wait_required(true);

    response->SetSuccessResponse(Json::Value::null);
  }
}
void DeleteCookieCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator name_parameter_iterator = command_parameters.find("name");
  if (name_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: name");
    return;
  }

  std::string cookie_name = name_parameter_iterator->second.asString();
  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get browser");
    return;
  }

  BrowserCookie cookie;
  cookie.set_name(cookie_name);
  browser_wrapper->cookie_manager()->DeleteCookie(
      browser_wrapper->GetCurrentUrl(),
      cookie);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to delete cookie");
    return;
  }

  response->SetSuccessResponse(Json::Value::null);
}
示例#10
0
int IECommandHandler::GetElement(const IECommandExecutor& executor,
                                 const std::string& element_id,
                                 ElementHandle* element_wrapper) {
  LOG(TRACE) << "Entering IECommandHandler::GetElement";

  ElementHandle candidate_wrapper;
  int result = executor.GetManagedElement(element_id, &candidate_wrapper);
  if (result != SUCCESS) {
    // This bears some explanation. Technically, passing an invalid ID in the
    // URL for an element command should result in a 404. However, since the
    // language bindings don't make up their own element IDs, any call from
    // a language binding is more than likely an ID that the IE driver assigned
    // it, and it was at one time valid. Therefore, we'll assume that not finding
    // the element ID in the cache means it's stale.
    LOG(WARN) << "Unable to get managed element, element not found, assuming stale";
    return EOBSOLETEELEMENT;
  } else {
    if (!candidate_wrapper->IsAttachedToDom()) {
      LOG(WARN) << "Found managed element is no longer valid";
      IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
      mutable_executor.RemoveManagedElement(element_id);
      return EOBSOLETEELEMENT;
    } else {
      // If the element is attached to the DOM, validate that its document
      // is the currently-focused document (via frames).
      BrowserHandle current_browser;
      executor.GetCurrentBrowser(&current_browser);
      CComPtr<IHTMLDocument2> focused_doc;
      current_browser->GetDocument(&focused_doc);

      CComPtr<IDispatch> parent_doc_dispatch;
      candidate_wrapper->element()->get_document(&parent_doc_dispatch);

      if (focused_doc.IsEqualObject(parent_doc_dispatch)) {
        *element_wrapper = candidate_wrapper;
        return SUCCESS;
      } else {
        LOG(WARN) << "Found managed element's document is not currently focused";
      }
    }
  }

  return EOBSOLETEELEMENT;
}
void ReleaseActionsCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get current browser");
    return;
  }
  response->SetSuccessResponse(Json::Value::null);
}
void GetAllWindowHandlesCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  Json::Value handles(Json::arrayValue);
  std::vector<std::string> handle_list;
  executor.GetManagedBrowserHandles(&handle_list);
  for (unsigned int i = 0; i < handle_list.size(); ++i) {
    handles.append(handle_list[i]);
  }

  response->SetSuccessResponse(handles);
}
void GetElementTextCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
    return;
  } else {
    std::string element_id = id_parameter_iterator->second.asString();

    BrowserHandle browser_wrapper;
    int status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code, "Unable to get browser");
      return;
    }

    ElementHandle element_wrapper;
    status_code = this->GetElement(executor, element_id, &element_wrapper);
    if (status_code == WD_SUCCESS) {
      // The atom is just the definition of an anonymous
      // function: "function() {...}"; Wrap it in another function so
      // we can invoke it with our arguments without polluting the
      // current namespace.
      std::wstring script_source = L"(function() { return (";
      script_source += atoms::asString(atoms::GET_TEXT);
      script_source += L")})();";

      CComPtr<IHTMLDocument2> doc;
      browser_wrapper->GetDocument(&doc);
      Script script_wrapper(doc, script_source, 1);
      script_wrapper.AddArgument(element_wrapper->element());
      status_code = script_wrapper.Execute();

      if (status_code == WD_SUCCESS) {
        std::string text = "";
        bool is_null = script_wrapper.ConvertResultToString(&text);
        response->SetSuccessResponse(text);
        return;
      } else {
        response->SetErrorResponse(status_code,
                                    "Unable to get element text");
        return;
      }
    } else {
      response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE, "Element is no longer valid");
      return;
    }
  }
}
void GetElementPropertyCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
  ParametersMap::const_iterator name_parameter_iterator = command_parameters.find("name");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
    return;
  } else if (name_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: name");
    return;
  } else {
    std::string element_id = id_parameter_iterator->second.asString();
    std::string name = name_parameter_iterator->second.asString();

    BrowserHandle browser_wrapper;
    int status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");
      return;
    }

    ElementHandle element_wrapper;
    status_code = this->GetElement(executor, element_id, &element_wrapper);
    if (status_code == WD_SUCCESS) {
      std::string value = "";
      bool is_null;
      status_code = element_wrapper->GetPropertyValue(name,
                                                      &value,
                                                      &is_null);
      if (status_code != WD_SUCCESS) {
        response->SetErrorResponse(status_code, "Unable to get property");
        return;
      } else {
        if (is_null) {
          response->SetSuccessResponse(Json::Value::null);
          return;
        } else {
          response->SetSuccessResponse(value);
          return;
        }
      }
    } else {
      response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE, "Element is no longer valid");
      return;
    }
  }
}
示例#15
0
int ElementFinder::FindElementUsingSizzle(const IECommandExecutor& executor,
                                          const ElementHandle parent_wrapper,
                                          const std::wstring& criteria,
                                          Json::Value* found_element) {
  LOG(TRACE) << "Entering ElementFinder::FindElementUsingSizzle";

  int result;

  BrowserHandle browser;
  result = executor.GetCurrentBrowser(&browser);
  if (result != WD_SUCCESS) {
    LOG(WARN) << "Unable to get browser";
    return result;
  }

  std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {");
  script_source += atoms::asString(atoms::SIZZLE);
  script_source += L"}\n";
  script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
  script_source += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } ";
  script_source += L"var results = []; Sizzle(arguments[0], root, results);";
  script_source += L"return results.length > 0 ? results[0] : null;";
  script_source += L"};})();";

  CComPtr<IHTMLDocument2> doc;
  browser->GetDocument(&doc);
  Script script_wrapper(doc, script_source, 2);
  script_wrapper.AddArgument(criteria);
  if (parent_wrapper) {
    CComPtr<IHTMLElement> parent(parent_wrapper->element());
    script_wrapper.AddArgument(parent);
  }
  result = script_wrapper.Execute();

  if (result == WD_SUCCESS) {
    if (!script_wrapper.ResultIsElement()) {
      LOG(WARN) << "Found result is not element";
      result = ENOSUCHELEMENT;
    } else {
      result = script_wrapper.ConvertResultToJsonValue(executor,
                                                       found_element);
    }
  } else {
    LOG(WARN) << "Unable to find elements";
    result = ENOSUCHELEMENT;
  }

  return result;
}
示例#16
0
bool ElementFinder::HasNativeCssSelectorEngine(const IECommandExecutor& executor) {
  LOG(TRACE) << "Entering ElementFinder::HasNativeCssSelectorEngine";

  BrowserHandle browser;
  executor.GetCurrentBrowser(&browser);

  std::wstring script_source(L"(function() { return function(){");
  script_source += L"var root = document.documentElement;";
  script_source += L"if (root['querySelectorAll']) { return true; } ";
  script_source += L"return false;";
  script_source += L"};})();";

  CComPtr<IHTMLDocument2> doc;
  browser->GetDocument(&doc);

  Script script_wrapper(doc, script_source, 0);
  script_wrapper.Execute();
  return script_wrapper.result().boolVal == VARIANT_TRUE;
}
void ExecuteAsyncScriptCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator script_parameter_iterator = command_parameters.find("script");
  ParametersMap::const_iterator args_parameter_iterator = command_parameters.find("args");
  if (script_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: script");
    return;
  } else if (args_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: args");
    return;
  } else {
    wchar_t page_id_buffer[GUID_STRING_LEN] = {0};
    GUID page_id_guid;
    ::CoCreateGuid(&page_id_guid);
    ::StringFromGUID2(page_id_guid, page_id_buffer, GUID_STRING_LEN);
    std::wstring page_id = &page_id_buffer[0];

    wchar_t pending_id_buffer[GUID_STRING_LEN] = {0};
    GUID pending_id_guid;
    ::CoCreateGuid(&pending_id_guid);
    ::StringFromGUID2(pending_id_guid, pending_id_buffer, GUID_STRING_LEN);
    std::wstring pending_id = &pending_id_buffer[0];

    Json::Value json_args = args_parameter_iterator->second;

    int timeout_value = executor.async_script_timeout();
    std::wstring timeout = std::to_wstring(static_cast<long long>(timeout_value));

    std::wstring script_body = StringUtilities::ToWString(script_parameter_iterator->second.asString());

    std::wstring async_script = L"(function() { return function(){\n";
    async_script += L"document.__$webdriverAsyncExecutor = {\n";
    async_script += L"  pageId: '" + page_id + L"',\n";
    async_script += L"  asyncTimeout: 0\n";
    async_script += L"};\n";
    async_script += L"var timeoutId = window.setTimeout(function() {\n";
    async_script += L"  window.setTimeout(function() {\n";
    async_script += L"    document.__$webdriverAsyncExecutor.asyncTimeout = 1;\n";
    async_script += L"  }, 0);\n";
    async_script += L"}," + timeout + L");\n";
    async_script += L"var callback = function(value) {\n";
    async_script += L"  document.__$webdriverAsyncExecutor.asyncTimeout = 0;\n";
    async_script += L"  document.__$webdriverAsyncExecutor.asyncScriptResult = value;\n";
    async_script += L"  window.clearTimeout(timeoutId);\n";
    async_script += L"};\n";
    async_script += L"var argsArray = Array.prototype.slice.call(arguments);\n";
    async_script += L"argsArray.push(callback);\n";
    async_script += L"if (document.__$webdriverAsyncExecutor.asyncScriptResult !== undefined) {\n";
    async_script += L"  delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
    async_script += L"}\n";
    async_script += L"(function() {" + script_body + L"}).apply(null, argsArray);\n";
    async_script += L"};})();";

    std::wstring polling_script = L"(function() { return function(){\n";
    polling_script += L"var pendingId = '" + pending_id + L"';\n";
    polling_script += L"if ('__$webdriverAsyncExecutor' in document) {\n";
    polling_script += L"  if (document.__$webdriverAsyncExecutor.pageId != '" + page_id + L"') {\n";
    polling_script += L"    return [pendingId, -1];\n";
    polling_script += L"  } else if ('asyncScriptResult' in document.__$webdriverAsyncExecutor) {\n";
    polling_script += L"    var value = document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
    polling_script += L"    delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
    polling_script += L"    return value;\n";
    polling_script += L"  } else {\n";
    polling_script += L"    return [pendingId, document.__$webdriverAsyncExecutor.asyncTimeout];\n";
    polling_script += L"  }\n";
    polling_script += L"} else {\n";
    polling_script += L"  return [pendingId, -1];\n";
    polling_script += L"}\n";
    polling_script += L"};})();";

    BrowserHandle browser_wrapper;
    int status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code, "Unable to get browser");
      return;
    }

    CComPtr<IHTMLDocument2> doc;
    browser_wrapper->GetDocument(&doc);
    Script async_script_wrapper(doc, async_script, json_args.size());
    status_code = this->PopulateArgumentArray(executor,
                                              async_script_wrapper,
                                              json_args);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code,
                                  "Error setting arguments for script");
      return;
    }

    status_code = async_script_wrapper.Execute();

    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code,
                                  "JavaScript error in async script.");
      return;
    } else {
      Script polling_script_wrapper(doc, polling_script, 0);
      while (true) {
        Json::Value polling_result;
        status_code = polling_script_wrapper.Execute();
        if (status_code != WD_SUCCESS) {
          // Assume that if the polling script errors, it's because
          // of a page reload. Note that experience shows this to
          // happen most frequently when a refresh occurs, since
          // the document object is not yet ready for accessing.
          // However, this is still a big assumption,and could be faulty.
          response->SetErrorResponse(EUNEXPECTEDJSERROR,
                                      "Page reload detected during async script");
          break;
        }

        polling_script_wrapper.ConvertResultToJsonValue(executor, &polling_result);
          
        Json::UInt index = 0;
        std::string narrow_pending_id = StringUtilities::ToString(pending_id);
        if (polling_result.isArray() &&
            polling_result.size() == 2 && 
            polling_result[index].isString() && 
            polling_result[index].asString() == narrow_pending_id) {
          int timeout_flag = polling_result[1].asInt();
          if (timeout_flag < 0) {
            response->SetErrorResponse(EUNEXPECTEDJSERROR,
                                        "Page reload detected during async script");
            break;
          }
          if (timeout_flag > 0) {
            response->SetErrorResponse(ESCRIPTTIMEOUT,
                                        "Timeout expired waiting for async script");
            break;
          }
        } else {
          response->SetSuccessResponse(polling_result);
          break;
        }
      }

      return;
    }
  }
}
void FindElementCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");
  ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");
  if (using_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");
    return;
  }
  if (!using_parameter_iterator->second.isString()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");
    return;
  }
  if (value_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");
    return;
  }
  if (!value_parameter_iterator->second.isString()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");
    return;
  }

  std::string mechanism = using_parameter_iterator->second.asString();
  std::string value = value_parameter_iterator->second.asString();

  if (mechanism != "css selector" &&
      mechanism != "tag name" &&
      mechanism != "link text" &&
      mechanism != "partial link text" &&
      mechanism != "xpath") {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");
    return;
  }

  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Currently focused window has been closed.");
    return;
  }

  int timeout = static_cast<int>(executor.implicit_wait_timeout());
  clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);
  if (timeout > 0 && timeout < 1000) {
    end += 1 * CLOCKS_PER_SEC;
  }

  status_code = WD_SUCCESS;
  Json::Value found_element;
  do {
    status_code = executor.LocateElement(ElementHandle(),
                                         mechanism,
                                         value,
                                         &found_element);
    if (status_code == WD_SUCCESS) {
      response->SetSuccessResponse(found_element);
      return;
    }
    if (status_code == ENOSUCHWINDOW) {
      response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to find element on closed window");
      return;
    }
    if (status_code != ENOSUCHELEMENT) {
      response->SetErrorResponse(status_code, found_element.asString());
      return;
    }

    // Release the thread so that the browser doesn't starve.
    ::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
  } while (clock() < end);

  response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, 
      "Unable to find element with " + mechanism + " == " + value);
  return;
}
void ExecuteAsyncScriptCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator script_parameter_iterator = command_parameters.find("script");
  ParametersMap::const_iterator args_parameter_iterator = command_parameters.find("args");
  if (script_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
                               "Missing parameter: script");
    return;
  }

  if (!script_parameter_iterator->second.isString()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
                               "script parameter must be a string");
    return;
  }

  if (args_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
                               "Missing parameter: args");
    return;
  }

  if (!args_parameter_iterator->second.isArray()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
                               "args parameter must be an array");
    return;
  }

  std::vector<wchar_t> page_id_buffer(GUID_STRING_LEN);
  GUID page_id_guid;
  ::CoCreateGuid(&page_id_guid);
  ::StringFromGUID2(page_id_guid, &page_id_buffer[0], GUID_STRING_LEN);
  std::wstring page_id = &page_id_buffer[0];

  std::vector<wchar_t> pending_id_buffer(GUID_STRING_LEN);
  GUID pending_id_guid;
  ::CoCreateGuid(&pending_id_guid);
  ::StringFromGUID2(pending_id_guid, &pending_id_buffer[0], GUID_STRING_LEN);
  std::wstring pending_id = &pending_id_buffer[0];

  Json::Value json_args = args_parameter_iterator->second;

  unsigned long long timeout_value = executor.async_script_timeout();
  std::wstring timeout = std::to_wstring(static_cast<long long>(timeout_value));

  std::wstring script_body = StringUtilities::ToWString(script_parameter_iterator->second.asString());

  std::wstring async_script = L"(function() { return function(){\n";
  async_script += L"document.__$webdriverAsyncExecutor = {\n";
  async_script += L"  pageId: '" + page_id + L"',\n";
  async_script += L"  asyncTimeout: 0\n";
  async_script += L"};\n";
  async_script += L"var timeoutId = window.setTimeout(function() {\n";
  async_script += L"  window.setTimeout(function() {\n";
  async_script += L"    document.__$webdriverAsyncExecutor.asyncTimeout = 1;\n";
  async_script += L"  }, 0);\n";
  async_script += L"}," + timeout + L");\n";
  async_script += L"var callback = function(value) {\n";
  async_script += L"  document.__$webdriverAsyncExecutor.asyncTimeout = 0;\n";
  async_script += L"  document.__$webdriverAsyncExecutor.asyncScriptResult = value;\n";
  async_script += L"  window.clearTimeout(timeoutId);\n";
  async_script += L"};\n";
  async_script += L"var argsArray = Array.prototype.slice.call(arguments);\n";
  async_script += L"argsArray.push(callback);\n";
  async_script += L"if (document.__$webdriverAsyncExecutor.asyncScriptResult !== undefined) {\n";
  async_script += L"  delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
  async_script += L"}\n";
  async_script += L"(function() {\n" + script_body + L"\n}).apply(null, argsArray);\n";
  async_script += L"};})();";

  std::wstring polling_script = L"(function() { return function(){\n";
  polling_script += L"var pendingId = '" + pending_id + L"';\n";
  polling_script += L"if ('__$webdriverAsyncExecutor' in document) {\n";
  polling_script += L"  if (document.__$webdriverAsyncExecutor.pageId != '" + page_id + L"') {\n";
  polling_script += L"    return {'status': 'reload', 'id': pendingId, 'value': -1};\n";
  polling_script += L"  } else if ('asyncScriptResult' in document.__$webdriverAsyncExecutor) {\n";
  polling_script += L"    var value = document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
  polling_script += L"    delete document.__$webdriverAsyncExecutor.asyncScriptResult;\n";
  polling_script += L"    return {'status': 'complete', 'id': pendingId, 'value': value};\n";
  polling_script += L"  } else if (document.__$webdriverAsyncExecutor.asyncTimeout == 0) {\n";
  polling_script += L"    return {'status': 'pending', 'id': pendingId, 'value': document.__$webdriverAsyncExecutor.asyncTimeout};\n";
  polling_script += L"  } else {\n";
  polling_script += L"    return {'status': 'timeout', 'id': pendingId, 'value': document.__$webdriverAsyncExecutor.asyncTimeout};\n";
  polling_script += L"  }\n";
  polling_script += L"} else {\n";
  polling_script += L"  return {'status': 'reload', 'id': pendingId, 'value': -1};\n";
  polling_script += L"}\n";
  polling_script += L"};})();";

  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Unable to get browser");
    return;
  }

  CComPtr<IHTMLDocument2> doc;
  browser_wrapper->GetDocument(&doc);

  HWND async_executor_handle;
  Script async_script_wrapper(doc, async_script);
  async_script_wrapper.set_polling_source_code(polling_script);
  status_code = async_script_wrapper.ExecuteAsync(executor,
                                                  json_args,
                                                  &async_executor_handle);
  browser_wrapper->set_script_executor_handle(async_executor_handle);

  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "JavaScript error");
  }
}
示例#20
0
int ElementFinder::FindElements(const IECommandExecutor& executor,
                                const ElementHandle parent_wrapper,
                                const std::wstring& mechanism,
                                const std::wstring& criteria,
                                Json::Value* found_elements) {
  LOG(TRACE) << "Entering ElementFinder::FindElements";

  BrowserHandle browser;
  int status_code = executor.GetCurrentBrowser(&browser);
  if (status_code == WD_SUCCESS) {
    if (mechanism == L"css") {
      if (!this->HasNativeCssSelectorEngine(executor)) {
        LOG(DEBUG) << "Element location strategy is CSS selectors, but "
                   << "document does not support CSS selectors. Falling back "
                   << "to using the Sizzle JavaScript CSS selector engine.";
        return this->FindElementsUsingSizzle(executor,
                                             parent_wrapper,
                                             criteria,
                                             found_elements);
      }
    }

    LOG(DEBUG) << "Using FindElements atom to locate element having "
               << LOGWSTRING(mechanism) << " = "
               << LOGWSTRING(criteria);
    CComPtr<IHTMLDocument2> doc;
    browser->GetDocument(&doc);

    std::wstring script_source(L"(function() { return (");
    script_source += atoms::asString(atoms::FIND_ELEMENTS);
    script_source += L")})();";

    Script script_wrapper(doc, script_source, 3);
    script_wrapper.AddArgument(mechanism);
    script_wrapper.AddArgument(criteria);
    if (parent_wrapper) {
      script_wrapper.AddArgument(parent_wrapper->element());
    }

    status_code = script_wrapper.Execute();
    if (status_code == WD_SUCCESS) {
      if (script_wrapper.ResultIsArray() || 
          script_wrapper.ResultIsElementCollection()) {
        script_wrapper.ConvertResultToJsonValue(executor, found_elements);
      } else {
        LOG(WARN) << "Returned value is not an array or element collection";
        status_code = ENOSUCHELEMENT;
      }
    } else {
      // An error in the execution of the FindElement atom for XPath is assumed
      // to be a syntactically invalid XPath.
      if (mechanism == L"xpath") {
        LOG(WARN) << "Attempted to find elements using invalid xpath: "
                  << LOGWSTRING(criteria);
        status_code = EINVALIDSELECTOR;
      } else {
        LOG(WARN) << "Unexpected error attempting to find element by mechanism "
                  << LOGWSTRING(mechanism) << " and criteria "
                  << LOGWSTRING(criteria);
        status_code = ENOSUCHELEMENT;
      }
    }
  } else {
    LOG(WARN) << "Unable to get browser";
  }
  return status_code;
}
void ClickElementCommandHandler::ExecuteInternal(const IECommandExecutor& executor,
                      const ParametersMap& command_parameters,
                      Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
    return;
  } else {
    int status_code = WD_SUCCESS;
    std::string element_id = id_parameter_iterator->second.asString();

    BrowserHandle browser_wrapper;
    status_code = executor.GetCurrentBrowser(&browser_wrapper);
    if (status_code != WD_SUCCESS) {
      response->SetErrorResponse(status_code, "Unable to get browser");
      return;
    }

    ElementHandle element_wrapper;
    status_code = this->GetElement(executor, element_id, &element_wrapper);
    if (status_code == WD_SUCCESS) {
      if (executor.input_manager()->enable_native_events()) {
        if (this->IsOptionElement(element_wrapper)) {
          std::string option_click_error = "";
          status_code = this->ExecuteAtom(this->GetClickAtom(),
                                          browser_wrapper,
                                          element_wrapper,
                                          &option_click_error);
          if (status_code != WD_SUCCESS) {
            response->SetErrorResponse(status_code, "Cannot click on option element. " + option_click_error);
            return;
          }
        } else {
          Json::Value move_action;
          move_action["type"] = "pointerMove";
          move_action["origin"] = element_wrapper->ConvertToJson();
          move_action["duration"] = 0;

          Json::Value down_action;
          down_action["type"] = "pointerDown";
          down_action["button"] = 0;
            
          Json::Value up_action;
          up_action["type"] = "pointerUp";
          up_action["button"] = 0;

          Json::Value action_array(Json::arrayValue);
          action_array.append(move_action);
          action_array.append(down_action);
          action_array.append(up_action);
            
          // Check to make sure we're not within the double-click time for this element
          // since the last click.
          int double_click_time = ::GetDoubleClickTime();

          Json::Value parameters_value;
          parameters_value["pointerType"] = "mouse";

          Json::Value value;
          value["type"] = "pointer";
          value["id"] = "click action mouse";
          value["parameters"] = parameters_value;
          value["actions"] = action_array;

          Json::Value actions(Json::arrayValue);
          actions.append(value);

          IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
          status_code = mutable_executor.input_manager()->PerformInputSequence(browser_wrapper, actions);
          browser_wrapper->set_wait_required(true);
          ::Sleep(double_click_time + 10);
          if (status_code != WD_SUCCESS) {
            if (status_code == EELEMENTCLICKPOINTNOTSCROLLED) {
              // We hard-code the error code here to be "Element not visible"
              // to maintain compatibility with previous behavior.
              response->SetErrorResponse(ERROR_ELEMENT_NOT_INTERACTABLE, "The point at which the driver is attempting to click on the element was not scrolled into the viewport.");
            } else {
              response->SetErrorResponse(status_code, "Cannot click on element");
            }
            return;
          }
        }
      } else {
        bool displayed;
        status_code = element_wrapper->IsDisplayed(true, &displayed);
        if (status_code != WD_SUCCESS) {
          response->SetErrorResponse(status_code, "Unable to determine element is displayed");
          return;
        } 

        if (!displayed) {
          response->SetErrorResponse(ERROR_ELEMENT_NOT_INTERACTABLE, "Element is not displayed");
          return;
        }
        std::string synthetic_click_error = "";
        status_code = this->ExecuteAtom(this->GetSyntheticClickAtom(),
                                        browser_wrapper,
                                        element_wrapper,
                                        &synthetic_click_error);
        if (status_code != WD_SUCCESS) {
          // This is a hack. We should change this when we can get proper error
          // codes back from the atoms. We'll assume the script failed because
          // the element isn't visible.
          response->SetErrorResponse(ERROR_ELEMENT_NOT_INTERACTABLE,
              "Received a JavaScript error attempting to click on the element using synthetic events. We are assuming this is because the element isn't displayed, but it may be due to other problems with executing JavaScript.");
          return;
        }
        browser_wrapper->set_wait_required(true);
      }
    } else if (status_code == ENOSUCHELEMENT) {
      response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "Invalid internal element ID requested: " + element_id);
      return;
    } else {
      response->SetErrorResponse(status_code, "Element is no longer valid");
      return;
    }

    response->SetSuccessResponse(Json::Value::null);
  }
}
示例#22
0
int ElementFinder::FindElement(const IECommandExecutor& executor,
                               const ElementHandle parent_wrapper,
                               const std::wstring& mechanism,
                               const std::wstring& criteria,
                               Json::Value* found_element) {
  LOG(TRACE) << "Entering ElementFinder::FindElement";

  BrowserHandle browser;
  int status_code = executor.GetCurrentBrowser(&browser);
  if (status_code == WD_SUCCESS) {
    if (mechanism == L"css") {
      if (!this->HasNativeCssSelectorEngine(executor)) {
        LOG(DEBUG) << "Element location strategy is CSS selectors, but "
                   << "document does not support CSS selectors. Falling back "
                   << "to using the Sizzle JavaScript CSS selector engine.";
        return this->FindElementUsingSizzle(executor,
                                            parent_wrapper,
                                            criteria,
                                            found_element);
      }
    }

    LOG(DEBUG) << L"Using FindElement atom to locate element having "
               << LOGWSTRING(mechanism) << " = "
               << LOGWSTRING(criteria);
    std::wstring sanitized_criteria = criteria;
    this->SanitizeCriteria(mechanism, &sanitized_criteria);
    std::wstring criteria_object_script = L"(function() { return function(){ return  { \"" + 
                                          mechanism + 
                                          L"\" : \"" +
                                          sanitized_criteria + L"\" }; };})();";
    CComPtr<IHTMLDocument2> doc;
    browser->GetDocument(&doc);

    Script criteria_wrapper(doc, criteria_object_script, 0);
    status_code = criteria_wrapper.Execute();
    if (status_code == WD_SUCCESS) {
      CComVariant criteria_object;
      criteria_object.Copy(&criteria_wrapper.result());

      // The atom is just the definition of an anonymous
      // function: "function() {...}"; Wrap it in another function so we can
      // invoke it with our arguments without polluting the current namespace.
      std::wstring script_source(L"(function() { return (");
      script_source += atoms::asString(atoms::FIND_ELEMENT);
      script_source += L")})();";

      Script script_wrapper(doc, script_source, 2);
      script_wrapper.AddArgument(criteria_object);
      if (parent_wrapper) {
        script_wrapper.AddArgument(parent_wrapper->element());
      }

      status_code = script_wrapper.Execute();
      if (status_code == WD_SUCCESS) {
        if (script_wrapper.ResultIsElement()) {
          script_wrapper.ConvertResultToJsonValue(executor, found_element);
        } else {
          LOG(WARN) << "Unable to find element by mechanism "
                    << LOGWSTRING(mechanism) << " and criteria " 
                    << LOGWSTRING(sanitized_criteria);
          status_code = ENOSUCHELEMENT;
        }
      } else {
        // An error in the execution of the FindElement atom for XPath is assumed
        // to be a syntactically invalid XPath.
        if (mechanism == L"xpath") {
          LOG(WARN) << "Attempted to find element using invalid xpath: "
                    << LOGWSTRING(sanitized_criteria);
          status_code = EINVALIDSELECTOR;
        } else {
          LOG(WARN) << "Unexpected error attempting to find element by mechanism "
                    << LOGWSTRING(mechanism) << " with criteria "
                    << LOGWSTRING(sanitized_criteria);
          status_code = ENOSUCHELEMENT;
        }
      }
    } else {
      LOG(WARN) << "Unable to create criteria object for mechanism "
                << LOGWSTRING(mechanism) << " and criteria " 
                << LOGWSTRING(sanitized_criteria);
      status_code = ENOSUCHELEMENT;
    }
  } else {
    LOG(WARN) << "Unable to get browser";
  }
  return status_code;
}
示例#23
0
int ElementFinder::FindElements(const IECommandExecutor& executor,
                                const ElementHandle parent_wrapper,
                                const std::wstring& mechanism,
                                const std::wstring& criteria,
                                Json::Value* found_elements) {
  LOG(TRACE) << "Entering ElementFinder::FindElements";

  BrowserHandle browser;
  int status_code = executor.GetCurrentBrowser(&browser);
  if (status_code == WD_SUCCESS) {
    if (mechanism == L"css") {
      if (!this->HasNativeCssSelectorEngine(executor)) {
        LOG(DEBUG) << "Element location strategy is CSS selectors, but "
                   << "document does not support CSS selectors. Falling back "
                   << "to using the Sizzle JavaScript CSS selector engine.";
        return this->FindElementsUsingSizzle(executor,
                                             parent_wrapper,
                                             criteria,
                                             found_elements);
      }
    }

    LOG(DEBUG) << "Using FindElements atom to locate element having "
               << LOGWSTRING(mechanism) << " = "
               << LOGWSTRING(criteria);
    CComPtr<IHTMLDocument2> doc;
    browser->GetDocument(&doc);

    std::wstring script_source(L"(function() { return (");
    script_source += atoms::asString(atoms::FIND_ELEMENTS);
    script_source += L")})();";

    Script script_wrapper(doc, script_source, 3);
    script_wrapper.AddArgument(mechanism);
    script_wrapper.AddArgument(criteria);
    if (parent_wrapper) {
      script_wrapper.AddArgument(parent_wrapper->element());
    }

    status_code = script_wrapper.Execute();
    if (status_code == WD_SUCCESS) {
      Json::Value atom_result;
      script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
      int atom_status_code = atom_result["status"].asInt();
      Json::Value atom_value = atom_result["value"];
      status_code = atom_status_code;
      *found_elements = atom_result["value"];
    } else {
      // Hitting a JavaScript error with the atom is an unrecoverable
      // error. The most common case of this for IE is when there is a
      // page refresh, navigation, or similar, and the driver is polling
      // for element presence. The calling code can't do anything about
      // it, so we might as well just log and return In the common case,
      // this means that the error will be transitory, and will sort
      // itself out once the DOM returns to normal after the page transition
      // is completed.
      LOG(WARN) << "A JavaScript error was encountered executing the findElements atom.";
    }
  } else {
    LOG(WARN) << "Unable to get browser";
  }
  return status_code;
}
示例#24
0
int ElementFinder::FindElementsUsingSizzle(const IECommandExecutor& executor,
                                           const ElementHandle parent_wrapper,
                                           const std::wstring& criteria,
                                           Json::Value* found_elements) {
  LOG(TRACE) << "Entering ElementFinder::FindElementsUsingSizzle";

  int result;

  if (criteria == L"") {
    // Apparently, Sizzle will happily return an empty array for an empty
    // string as the selector. We do not want this.
    return ENOSUCHELEMENT;
  }

  BrowserHandle browser;
  result = executor.GetCurrentBrowser(&browser);
  if (result != WD_SUCCESS) {
    LOG(WARN) << "Unable to get browser";
    return result;
  }

  std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {");
  script_source += atoms::asString(atoms::SIZZLE);
  script_source += L"}\n";
  script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
  script_source += L"if (root['querySelectorAll']) { return root.querySelectorAll(arguments[0]); } ";
  script_source += L"var results = []; try { Sizzle(arguments[0], root, results); } catch(ex) { results = null; }";
  script_source += L"return results;";
  script_source += L"};})();";

  CComPtr<IHTMLDocument2> doc;
  browser->GetDocument(&doc);

  Script script_wrapper(doc, script_source, 2);
  script_wrapper.AddArgument(criteria);
  if (parent_wrapper) {
    // Use a copy for the parent element?
    CComPtr<IHTMLElement> parent(parent_wrapper->element());
    script_wrapper.AddArgument(parent);
  }

  result = script_wrapper.Execute();
  if (result == WD_SUCCESS) {
    CComVariant snapshot = script_wrapper.result();
    if (snapshot.vt == VT_NULL || snapshot.vt == VT_EMPTY) {
      // We explicitly caught an error from Sizzle. Return ENOSUCHELEMENT.
      return ENOSUCHELEMENT;
    }
    std::wstring get_element_count_script = L"(function(){return function() {return arguments[0].length;}})();";
    Script get_element_count_script_wrapper(doc, get_element_count_script, 1);
    get_element_count_script_wrapper.AddArgument(snapshot);
    result = get_element_count_script_wrapper.Execute();
    if (result == WD_SUCCESS) {
      *found_elements = Json::Value(Json::arrayValue);
      if (!get_element_count_script_wrapper.ResultIsInteger()) {
        LOG(WARN) << "Found elements count is not integer";
        result = EUNEXPECTEDJSERROR;
      } else {
        long length = get_element_count_script_wrapper.result().lVal;
        std::wstring get_next_element_script = L"(function(){return function() {return arguments[0][arguments[1]];}})();";
        for (long i = 0; i < length; ++i) {
          Script get_element_script_wrapper(doc, get_next_element_script, 2);
          get_element_script_wrapper.AddArgument(snapshot);
          get_element_script_wrapper.AddArgument(i);
          result = get_element_script_wrapper.Execute();
          if (result == WD_SUCCESS) {
            Json::Value json_element;
            get_element_script_wrapper.ConvertResultToJsonValue(executor,
                                                            &json_element);
            found_elements->append(json_element);
          } else {
            LOG(WARN) << "Unable to get " << i << " found element";
          }
        }
      }
    } else {
      LOG(WARN) << "Unable to get count of found elements";
      result = EUNEXPECTEDJSERROR;
    }

  } else {
    LOG(WARN) << "Execution returned error";
  }

  return result;
}
void SetWindowRectCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  int width = -1;
  int height = -1;
  int x = -1;
  int y = -1;

  std::string argument_error_message = "";
  if (!GetNumericParameter(command_parameters, "width", &width, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "height", &height, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "x", &x, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  if (!GetNumericParameter(command_parameters, "y", &y, &argument_error_message)) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
    return;
  }

  int status_code = WD_SUCCESS;

  BrowserHandle browser_wrapper;
  status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Error retrieving current window");
    return;
  }

  // If the window is minimized, maximized, or full screen,
  // the window needs to be restored.
  browser_wrapper->Restore();

  HWND window_handle = browser_wrapper->GetTopLevelWindowHandle();
  if (x >= 0 && y >= 0) {
    BOOL set_window_pos_result = ::SetWindowPos(window_handle, NULL, x, y, 0, 0, SWP_NOSIZE);
    if (!set_window_pos_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  if (width >= 0 && height >= 0) {
    BOOL set_window_size_result = ::SetWindowPos(window_handle, NULL, 0, 0, width, height, SWP_NOMOVE);
    if (!set_window_size_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                 "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  HWND browser_window_handle = browser_wrapper->GetTopLevelWindowHandle();
  RECT window_rect;
  ::GetWindowRect(browser_window_handle, &window_rect);
  Json::Value returned_rect;
  returned_rect["width"] = window_rect.right - window_rect.left;
  returned_rect["height"] = window_rect.bottom - window_rect.top;
  returned_rect["x"] = window_rect.left;
  returned_rect["y"] = window_rect.top;
  response->SetSuccessResponse(returned_rect);
}
void FindChildElementsCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
  ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");
  ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");
  if (id_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
    return;
  }
  if (using_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");
    return;
  }
  if (!using_parameter_iterator->second.isString()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");
    return;
  }
  if (value_parameter_iterator == command_parameters.end()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");
    return;
  }
  if (!value_parameter_iterator->second.isString()) {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");
    return;
  }

  std::string mechanism = using_parameter_iterator->second.asString();
  std::string value = value_parameter_iterator->second.asString();
  std::string element_id = id_parameter_iterator->second.asString();

  if (mechanism != "css selector" &&
      mechanism != "tag name" &&
      mechanism != "link text" &&
      mechanism != "partial link text" &&
      mechanism != "xpath") {
    response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");
    return;
  }

  BrowserHandle browser_wrapper;
  int status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(status_code, "Currently focused window has been closed.");
    return;
  }

  ElementHandle parent_element_wrapper;
  status_code = this->GetElement(executor,
                                 element_id,
                                 &parent_element_wrapper);

  if (status_code == WD_SUCCESS) {
    Json::Value found_elements(Json::arrayValue);

    int timeout = static_cast<int>(executor.implicit_wait_timeout());
    clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);
    if (timeout > 0 && timeout < 1000) {
      end += 1 * CLOCKS_PER_SEC;
    }

    do {
      status_code = executor.LocateElements(parent_element_wrapper,
                                            mechanism,
                                            value,
                                            &found_elements);
      if (status_code == WD_SUCCESS) {
        if (found_elements.isArray() && found_elements.size() > 0) {
          response->SetSuccessResponse(found_elements);
          return;
        }
      } else if (status_code == ENOSUCHWINDOW) {
        response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, 
                                   "Unable to find elements on closed window");
        return;
      } else {
        response->SetErrorResponse(status_code, found_elements.asString());
        return;
      }

      // Release the thread so that the browser doesn't starve.
      ::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
    } while (clock() < end);

    // This code is executed when no elements where found and no errors occurred.
    if (status_code == WD_SUCCESS) {
      response->SetSuccessResponse(found_elements);
    } else {
      response->SetErrorResponse(status_code, 
          "Finding elements with " + mechanism + " == " + value +
          "returned an unexpected error");
    }
  } else {
    if (status_code == EOBSOLETEELEMENT) {
      response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE,
                                 "Specified parent element is no longer attached to the DOM");
    } else {
      response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
                                 "Element is no longer valid");
    }
  }
}
void SetWindowRectCommandHandler::ExecuteInternal(
    const IECommandExecutor& executor,
    const ParametersMap& command_parameters,
    Response* response) {
  int width = -1;
  int height = -1;
  int x = -1;
  int y = -1;

  ParametersMap::const_iterator width_parameter_iterator = command_parameters.find("width");
  ParametersMap::const_iterator height_parameter_iterator = command_parameters.find("height");
  ParametersMap::const_iterator x_parameter_iterator = command_parameters.find("x");
  ParametersMap::const_iterator y_parameter_iterator = command_parameters.find("y");
  
  if (width_parameter_iterator != command_parameters.end()) {
    width = width_parameter_iterator->second.asInt();
  }
  
  if (height_parameter_iterator != command_parameters.end()) {
    height = height_parameter_iterator->second.asInt();
  }
  
  if (x_parameter_iterator != command_parameters.end()) {
    x = x_parameter_iterator->second.asInt();
  }
  
  if (y_parameter_iterator != command_parameters.end()) {
    y = y_parameter_iterator->second.asInt();
  } 
  
  int status_code = WD_SUCCESS;

  BrowserHandle browser_wrapper;
  status_code = executor.GetCurrentBrowser(&browser_wrapper);
  if (status_code != WD_SUCCESS) {
    response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Error retrieving current window");
    return;
  }

  // If the window is maximized, the window needs to be restored.
  HWND window_handle = browser_wrapper->GetTopLevelWindowHandle();
  if (::IsZoomed(window_handle) || ::IsIconic(window_handle)) {
    ::ShowWindow(window_handle, SW_RESTORE);
  }

  if (x >= 0 && y >= 0) {
    BOOL set_window_pos_result = ::SetWindowPos(window_handle, NULL, x, y, 0, 0, SWP_NOSIZE);
    if (!set_window_pos_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  if (width >= 0 && height >= 0) {
    BOOL set_window_size_result = ::SetWindowPos(window_handle, NULL, 0, 0, width, height, SWP_NOMOVE);
    if (!set_window_size_result) {
      response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
                                 "Unexpected error setting window size (SetWindowPos API failed)");
      return;
    }
  }

  Json::Value returned_rect;
  returned_rect["x"] = x;
  returned_rect["y"] = y;
  returned_rect["width"] = width;
  returned_rect["height"] = height;
  response->SetSuccessResponse(returned_rect);
}