Chromium Code Reviews| Index: chrome/test/webdriver/session.cc |
| =================================================================== |
| --- chrome/test/webdriver/session.cc (revision 76851) |
| +++ chrome/test/webdriver/session.cc (working copy) |
| @@ -162,18 +162,41 @@ |
| } |
| ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { |
| - ListValue args; |
| - args.Append(element.ToValue()); |
| - // TODO(jleyba): Update this to use the correct atom. |
| - std::string script = "document.activeElement.blur();arguments[0].focus();"; |
| - Value* unscoped_result = NULL; |
| - ErrorCode code = ExecuteScript(script, &args, &unscoped_result); |
| - scoped_ptr<Value> result(unscoped_result); |
| - if (code != kSuccess) { |
| - LOG(ERROR) << "Failed to focus element before sending keys"; |
| - return code; |
| + // This method will first check if the element we want to send the keys to is |
| + // already focused, if not it will try to focus on it first. |
| + scoped_ptr<ListValue> nullArgs(new ListValue); |
|
Jason Leyba
2011/03/04 19:25:12
This would be much simpler if you did it all with
timothe faudot
2011/03/07 02:45:26
Done.
|
| + Value* active_element = NULL; |
| + ErrorCode status = ExecuteScript( |
| + "return document.activeElement;", nullArgs.get(), &active_element); |
| + if (status != kSuccess) { |
| + LOG(ERROR) << "Failed to get which element was focused before sending keys"; |
| + return status; |
| } |
| - |
| + if (active_element == NULL || |
| + active_element->GetType() != Value::TYPE_DICTIONARY) { |
| + LOG(ERROR) << "Expected Japascript to return a dictionary"; |
| + return kUnknownError; |
| + } |
| + std::string active_elem_str; |
| + static_cast<DictionaryValue*>(active_element)->GetString("ELEMENT", |
| + &active_elem_str); |
| + std::string wanted_element; |
| + DictionaryValue* element_id = |
| + static_cast<DictionaryValue*>(element.ToValue()); |
| + element_id->GetString("ELEMENT", &wanted_element); |
| + // We focus on the element only if necessary. |
| + if (active_elem_str != wanted_element) { |
| + scoped_ptr<ListValue> args(new ListValue); |
| + args->Append(element.ToValue()); |
| + // TODO(jleyba): Update this to use the correct atom. |
| + std::string script = "document.activeElement.blur();arguments[0].focus();"; |
| + Value* unscoped_result = NULL; |
| + ErrorCode code = ExecuteScript(script, args.get(), &unscoped_result); |
| + if (code != kSuccess) { |
| + LOG(ERROR) << "Failed to focus element before sending keys"; |
| + return code; |
| + } |
| + } |
| bool success = false; |
| RunSessionTask(NewRunnableMethod( |
| this, |
| @@ -611,8 +634,9 @@ |
| for (size_t i = 0; i < key_events.size(); ++i) { |
| bool key_success = false; |
| automation_->SendWebKeyEvent( |
| - current_window_id_, key_events[i], &key_success); |
| - if (!key_success) { |
| + current_window_id_, key_events[i], |
| + SessionManager::GetInstance()->use_native_events(), &key_success); |
| + if (!key_success) { |
| LOG(ERROR) << "Failed to send key event. Event details:\n" |
| << "Type: " << key_events[i].type << "\n" |
| << "KeyCode: " << key_events[i].key_code << "\n" |