headless-browser: Allow specifying the path to the saved screenshot

This is to help make creating Screenshot tests a bit easier.
This commit is contained in:
Timothy Flynn 2025-05-20 07:19:29 -04:00 committed by Tim Flynn
commit 141afbc63d
Notes: github-actions[bot] 2025-05-20 14:49:24 +00:00
3 changed files with 8 additions and 9 deletions

View file

@ -32,6 +32,7 @@ Application::~Application()
void Application::create_platform_arguments(Core::ArgsParser& args_parser)
{
args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n");
args_parser.add_option(screenshot_path, "Path the save the screenshot (default: 'output.png')", "screenshot-path", 'p', "path");
args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd');
args_parser.add_option(dump_text, "Dump text and exit", "dump-text", 'T');
args_parser.add_option(test_concurrency, "Maximum number of tests to run at once", "test-concurrency", 'j', "jobs");

View file

@ -49,6 +49,7 @@ public:
static constexpr u8 VERBOSITY_LEVEL_LOG_SKIPPED_TESTS = 3;
int screenshot_timeout { 1 };
ByteString screenshot_path { "output.png"sv };
ByteString resources_folder;
bool dump_failed_ref_tests { false };
bool dump_layout_tree { false };

View file

@ -26,13 +26,10 @@
#include <UI/Headless/HeadlessWebView.h>
#include <UI/Headless/Test.h>
static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, Ladybird::HeadlessWebView& view, URL::URL const& url, int screenshot_timeout)
static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, Ladybird::HeadlessWebView& view, URL::URL const& url, int screenshot_timeout, ByteString const& screenshot_path)
{
// FIXME: Allow passing the output path as an argument.
static constexpr auto output_file_path = "output.png"sv;
if (FileSystem::exists(output_file_path))
TRY(FileSystem::remove(output_file_path, FileSystem::RecursionMode::Disallowed));
if (FileSystem::exists(screenshot_path))
TRY(FileSystem::remove(screenshot_path, FileSystem::RecursionMode::Disallowed));
outln("Taking screenshot after {} seconds", screenshot_timeout);
@ -42,9 +39,9 @@ static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Cor
auto promise = view.take_screenshot();
if (auto screenshot = MUST(promise->await())) {
outln("Saving screenshot to {}", output_file_path);
outln("Saving screenshot to {}", screenshot_path);
auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
auto output_file = MUST(Core::File::open(screenshot_path, Core::File::OpenMode::Write));
auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
MUST(output_file->write_until_depleted(image_buffer.bytes()));
} else {
@ -96,7 +93,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
RefPtr<Core::Timer> timer;
if (!WebView::Application::browser_options().webdriver_content_ipc_path.has_value())
timer = TRY(load_page_for_screenshot_and_exit(Core::EventLoop::current(), view, url, app->screenshot_timeout));
timer = TRY(load_page_for_screenshot_and_exit(Core::EventLoop::current(), view, url, app->screenshot_timeout, app->screenshot_path));
return app->execute();
}