Skip to content

Commit

Permalink
WebDriver+Friends: Add IPC and plumbing for Element-getting
Browse files Browse the repository at this point in the history
This extends the IPC calls `get_document_element` and
`query_selector_all` to be usable by the WebDriver. For this the
`WebDriverConnection` provides the same interfaces and takes care of
routing the data through the Browser.
  • Loading branch information
TobyAsE authored and linusg committed Oct 18, 2022
1 parent 2819910 commit 5d762bd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Userland/Applications/Browser/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ void BrowserWindow::create_new_tab(URL url, bool activate)
return active_tab().view().get_session_storage_entries();
};

new_tab.on_get_document_element = [this]() {
return active_tab().view().get_document_element();
};

new_tab.on_query_selector_all = [this](i32 start_node_id, String const& selector) {
return active_tab().view().query_selector_all(start_node_id, selector);
};

new_tab.load(url);

dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);
Expand Down
2 changes: 2 additions & 0 deletions Userland/Applications/Browser/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Tab final : public GUI::Widget {
Function<Vector<Web::Cookie::Cookie>()> on_get_cookies_entries;
Function<OrderedHashMap<String, String>()> on_get_local_storage_entries;
Function<OrderedHashMap<String, String>()> on_get_session_storage_entries;
Function<Optional<i32>()> on_get_document_element;
Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all;

enum class InspectorTarget {
Document,
Expand Down
22 changes: 22 additions & 0 deletions Userland/Applications/Browser/WebDriverConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,26 @@ void WebDriverConnection::update_cookie(Web::Cookie::Cookie const& cookie)
}
}

Messages::WebDriverSessionClient::GetDocumentElementResponse WebDriverConnection::get_document_element()
{
dbgln("WebDriverConnection: get_document_element");
if (auto browser_window = m_browser_window.strong_ref()) {
auto& tab = browser_window->active_tab();
if (tab.on_get_document_element)
return { tab.on_get_document_element() };
}
return { {} };
}

Messages::WebDriverSessionClient::QuerySelectorAllResponse WebDriverConnection::query_selector_all(i32 start_node_id, String const& selector)
{
dbgln("WebDriverConnection: query_selector_all");
if (auto browser_window = m_browser_window.strong_ref()) {
auto& tab = browser_window->active_tab();
if (tab.on_query_selector_all)
return { tab.on_query_selector_all(start_node_id, selector) };
}
return { {} };
}

}
2 changes: 2 additions & 0 deletions Userland/Applications/Browser/WebDriverConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class WebDriverConnection final
virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override;
virtual void add_cookie(Web::Cookie::ParsedCookie const&) override;
virtual void update_cookie(Web::Cookie::Cookie const&) override;
virtual Messages::WebDriverSessionClient::GetDocumentElementResponse get_document_element() override;
virtual Messages::WebDriverSessionClient::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override;

private:
WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);
Expand Down
2 changes: 2 additions & 0 deletions Userland/Applications/Browser/WebDriverSessionClient.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ endpoint WebDriverSessionClient {
get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)
add_cookie(Web::Cookie::ParsedCookie cookie) =|
update_cookie(Web::Cookie::Cookie cookie) =|
get_document_element() => (Optional<i32> document_element_id)
query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids)

}
10 changes: 10 additions & 0 deletions Userland/Libraries/LibWebView/OutOfProcessWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ OrderedHashMap<String, String> OutOfProcessWebView::get_session_storage_entries(
return client().get_session_storage_entries();
}

Optional<i32> OutOfProcessWebView::get_document_element()
{
return client().get_document_element();
}

Optional<Vector<i32>> OutOfProcessWebView::query_selector_all(i32 start_node_id, String const& selector)
{
return client().query_selector_all(start_node_id, selector);
}

void OutOfProcessWebView::set_content_filters(Vector<String> filters)
{
client().async_set_content_filters(filters);
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWebView/OutOfProcessWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class OutOfProcessWebView final
OrderedHashMap<String, String> get_local_storage_entries();
OrderedHashMap<String, String> get_session_storage_entries();

Optional<i32> get_document_element();
Optional<Vector<i32>> query_selector_all(i32 start_node_id, String const& selector);

void set_content_filters(Vector<String>);
void set_proxy_mappings(Vector<String> proxies, HashMap<String, size_t> mappings);
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
Expand Down

0 comments on commit 5d762bd

Please sign in to comment.