Skip to content

Commit

Permalink
headless-browser: Add a mode to dump the layout tree after load finish
Browse files Browse the repository at this point in the history
  • Loading branch information
trflynn89 authored and linusg committed Mar 13, 2023
1 parent 2845a8e commit 7090f74
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Userland/Utilities/headless-browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <AK/Badge.h>
#include <AK/DeprecatedString.h>
#include <AK/Function.h>
#include <AK/LexicalPath.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/String.h>
Expand Down Expand Up @@ -74,6 +75,13 @@ class HeadlessWebContentView final : public WebView::ViewImplementation {
return client().take_document_screenshot().bitmap();
}

ErrorOr<String> dump_layout_tree()
{
return String::from_deprecated_string(client().dump_layout_tree());
}

Function<void(const URL&)> on_load_finish;

private:
HeadlessWebContentView() = default;

Expand All @@ -93,7 +101,13 @@ class HeadlessWebContentView final : public WebView::ViewImplementation {
void notify_server_did_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { }
void notify_server_did_middle_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { }
void notify_server_did_start_loading(Badge<WebView::WebContentClient>, const URL&, bool) override { }
void notify_server_did_finish_loading(Badge<WebView::WebContentClient>, const URL&) override { }

void notify_server_did_finish_loading(Badge<WebView::WebContentClient>, const URL& url) override
{
if (on_load_finish)
on_load_finish(url);
}

void notify_server_did_request_navigate_back(Badge<WebView::WebContentClient>) override { }
void notify_server_did_request_navigate_forward(Badge<WebView::WebContentClient>) override { }
void notify_server_did_request_refresh(Badge<WebView::WebContentClient>) override { }
Expand Down Expand Up @@ -195,10 +209,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
StringView url;
auto resources_folder = "/res"sv;
StringView web_driver_ipc_path;
bool dump_layout_tree = false;

Core::ArgsParser args_parser;
args_parser.set_general_help("This utility runs the Browser in headless mode.");
args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n");
args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd');
args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path");
args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path");
args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::Yes);
Expand All @@ -218,11 +234,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
static constexpr Gfx::IntSize window_size { 800, 600 };

auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path));
view->load(TRY(format_url(url)));

RefPtr<Core::Timer> timer;
if (web_driver_ipc_path.is_empty())

if (dump_layout_tree) {
view->on_load_finish = [&](auto const&) {
auto layout_tree = view->dump_layout_tree().release_value_but_fixme_should_propagate_errors();

outln("{}", layout_tree);
fflush(stdout);

event_loop.quit(0);
};
} else if (web_driver_ipc_path.is_empty()) {
timer = TRY(load_page_for_screenshot_and_exit(event_loop, *view, screenshot_timeout));
}

view->load(TRY(format_url(url)));
return event_loop.exec();
}

0 comments on commit 7090f74

Please sign in to comment.