Commit 68655b3d authored by Shengfa Lin's avatar Shengfa Lin Committed by Commit Bot

[chromedriver] Check document.hasFocus() for IsElementFocus

document.activeElement could return body when no element has
focus. When user first navigate to url or switch frame, we
don't want to treat this body as focus. Therefore, we will
check document.hasFocus() first.

Bug: chromedriver:3502,b/157759309
Change-Id: I87e649423a6fde55a7b8a273c3500787045edac3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225521Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Commit-Queue: Shengfa Lin <shengfa@google.com>
Cr-Commit-Position: refs/heads/master@{#774266}
parent 2d660071
...@@ -422,13 +422,30 @@ Status GetActiveElement(Session* session, ...@@ -422,13 +422,30 @@ Status GetActiveElement(Session* session,
return status; return status;
} }
Status HasFocus(Session* session, WebView* web_view, bool* hasFocus) {
std::unique_ptr<base::Value> value;
Status status = web_view->EvaluateScript(
session->GetCurrentFrameId(), "document.hasFocus()", false, &value);
if (status.IsError())
return status;
if (!value->is_bool())
return Status(kUnknownError, "document.hasFocus() returns non-boolean");
*hasFocus = value->GetBool();
return Status(kOk);
}
Status IsElementFocused( Status IsElementFocused(
Session* session, Session* session,
WebView* web_view, WebView* web_view,
const std::string& element_id, const std::string& element_id,
bool* is_focused) { bool* is_focused) {
std::unique_ptr<base::Value> result; std::unique_ptr<base::Value> result;
Status status = GetActiveElement(session, web_view, &result); Status status = HasFocus(session, web_view, is_focused);
if (status.IsError())
return status;
if (!(*is_focused))
return status;
status = GetActiveElement(session, web_view, &result);
if (status.IsError()) if (status.IsError())
return status; return status;
std::unique_ptr<base::Value> element_dict(CreateElement(element_id)); std::unique_ptr<base::Value> element_dict(CreateElement(element_id));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment