headless-browser.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  5. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Badge.h>
  10. #include <AK/ByteString.h>
  11. #include <AK/Function.h>
  12. #include <AK/JsonObject.h>
  13. #include <AK/JsonParser.h>
  14. #include <AK/LexicalPath.h>
  15. #include <AK/NonnullOwnPtr.h>
  16. #include <AK/Platform.h>
  17. #include <AK/String.h>
  18. #include <AK/URL.h>
  19. #include <AK/Vector.h>
  20. #include <Ladybird/Types.h>
  21. #include <LibCore/ArgsParser.h>
  22. #include <LibCore/ConfigFile.h>
  23. #include <LibCore/DirIterator.h>
  24. #include <LibCore/Directory.h>
  25. #include <LibCore/EventLoop.h>
  26. #include <LibCore/File.h>
  27. #include <LibCore/ResourceImplementationFile.h>
  28. #include <LibCore/Timer.h>
  29. #include <LibDiff/Format.h>
  30. #include <LibDiff/Generator.h>
  31. #include <LibFileSystem/FileSystem.h>
  32. #include <LibGfx/Bitmap.h>
  33. #include <LibGfx/Font/FontDatabase.h>
  34. #include <LibGfx/ImageFormats/PNGWriter.h>
  35. #include <LibGfx/Point.h>
  36. #include <LibGfx/Rect.h>
  37. #include <LibGfx/ShareableBitmap.h>
  38. #include <LibGfx/Size.h>
  39. #include <LibGfx/StandardCursor.h>
  40. #include <LibGfx/SystemTheme.h>
  41. #include <LibIPC/File.h>
  42. #include <LibWeb/Cookie/Cookie.h>
  43. #include <LibWeb/Cookie/ParsedCookie.h>
  44. #include <LibWeb/HTML/ActivateTab.h>
  45. #include <LibWebView/URL.h>
  46. #include <LibWebView/ViewImplementation.h>
  47. #include <LibWebView/WebContentClient.h>
  48. #if !defined(AK_OS_SERENITY)
  49. # include <Ladybird/HelperProcess.h>
  50. # include <Ladybird/Utilities.h>
  51. #endif
  52. constexpr int DEFAULT_TIMEOUT_MS = 30000; // 30sec
  53. static StringView s_current_test_path;
  54. class HeadlessWebContentView final : public WebView::ViewImplementation {
  55. public:
  56. static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path, Ladybird::IsLayoutTestMode is_layout_test_mode = Ladybird::IsLayoutTestMode::No)
  57. {
  58. auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView));
  59. #if defined(AK_OS_SERENITY)
  60. view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view));
  61. (void)is_layout_test_mode;
  62. #else
  63. Ladybird::WebContentOptions web_content_options { .is_layout_test_mode = is_layout_test_mode };
  64. auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv));
  65. view->m_client_state.client = TRY(launch_web_content_process(*view, candidate_web_content_paths, web_content_options));
  66. #endif
  67. view->client().async_update_system_theme(move(theme));
  68. view->client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  69. view->m_viewport_rect = { { 0, 0 }, window_size };
  70. view->client().async_set_viewport_rect(view->m_viewport_rect.to_type<Web::DevicePixels>());
  71. view->client().async_set_window_size(window_size.to_type<Web::DevicePixels>());
  72. if (!web_driver_ipc_path.is_empty())
  73. view->client().async_connect_to_webdriver(web_driver_ipc_path);
  74. view->m_client_state.client->on_web_content_process_crash = [] {
  75. warnln("\033[31;1mWebContent Crashed!!\033[0m");
  76. if (!s_current_test_path.is_empty()) {
  77. warnln(" Last started test: {}", s_current_test_path);
  78. }
  79. VERIFY_NOT_REACHED();
  80. };
  81. return view;
  82. }
  83. RefPtr<Gfx::Bitmap> take_screenshot()
  84. {
  85. return client().take_document_screenshot().bitmap();
  86. }
  87. ErrorOr<String> dump_layout_tree()
  88. {
  89. return String::from_byte_string(client().dump_layout_tree());
  90. }
  91. ErrorOr<String> dump_paint_tree()
  92. {
  93. return String::from_byte_string(client().dump_paint_tree());
  94. }
  95. ErrorOr<String> dump_text()
  96. {
  97. return String::from_byte_string(client().dump_text());
  98. }
  99. void clear_content_filters()
  100. {
  101. client().async_set_content_filters({});
  102. }
  103. private:
  104. HeadlessWebContentView()
  105. {
  106. on_scroll_to_point = [this](auto position) {
  107. m_viewport_rect.set_location(position);
  108. client().async_set_viewport_rect(m_viewport_rect.to_type<Web::DevicePixels>());
  109. };
  110. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  111. auto position = m_viewport_rect.location();
  112. position.set_x(position.x() + x_delta);
  113. position.set_y(position.y() + y_delta);
  114. if (on_scroll_to_point)
  115. on_scroll_to_point(position);
  116. };
  117. }
  118. void update_zoom() override { }
  119. void create_client() override { }
  120. virtual Web::DevicePixelRect viewport_rect() const override { return m_viewport_rect.to_type<Web::DevicePixels>(); }
  121. virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override { return widget_position; }
  122. virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override { return content_position; }
  123. private:
  124. Gfx::IntRect m_viewport_rect;
  125. };
  126. static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, HeadlessWebContentView& view, int screenshot_timeout)
  127. {
  128. // FIXME: Allow passing the output path as an argument.
  129. static constexpr auto output_file_path = "output.png"sv;
  130. if (FileSystem::exists(output_file_path))
  131. TRY(FileSystem::remove(output_file_path, FileSystem::RecursionMode::Disallowed));
  132. outln("Taking screenshot after {} seconds", screenshot_timeout);
  133. auto timer = TRY(Core::Timer::create_single_shot(
  134. screenshot_timeout * 1000,
  135. [&]() {
  136. if (auto screenshot = view.take_screenshot()) {
  137. outln("Saving screenshot to {}", output_file_path);
  138. auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
  139. auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
  140. MUST(output_file->write_until_depleted(image_buffer.bytes()));
  141. } else {
  142. warnln("No screenshot available");
  143. }
  144. event_loop.quit(0);
  145. }));
  146. timer->start();
  147. return timer;
  148. }
  149. enum class TestMode {
  150. Layout,
  151. Text,
  152. Ref,
  153. };
  154. enum class TestResult {
  155. Pass,
  156. Fail,
  157. Skipped,
  158. Timeout,
  159. };
  160. static StringView test_result_to_string(TestResult result)
  161. {
  162. switch (result) {
  163. case TestResult::Pass:
  164. return "Pass"sv;
  165. case TestResult::Fail:
  166. return "Fail"sv;
  167. case TestResult::Skipped:
  168. return "Skipped"sv;
  169. case TestResult::Timeout:
  170. return "Timeout"sv;
  171. }
  172. VERIFY_NOT_REACHED();
  173. }
  174. static ErrorOr<TestResult> run_dump_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS)
  175. {
  176. Core::EventLoop loop;
  177. bool did_timeout = false;
  178. auto timeout_timer = TRY(Core::Timer::create_single_shot(timeout_in_milliseconds, [&] {
  179. did_timeout = true;
  180. loop.quit(0);
  181. }));
  182. view.load(URL::create_with_file_scheme(TRY(FileSystem::real_path(input_path)).to_byte_string()));
  183. String result;
  184. if (mode == TestMode::Layout) {
  185. view.on_load_finish = [&](auto const&) {
  186. // NOTE: We take a screenshot here to force the lazy layout of SVG-as-image documents to happen.
  187. // It also causes a lot more code to run, which is good for finding bugs. :^)
  188. (void)view.take_screenshot();
  189. StringBuilder builder;
  190. builder.append(view.dump_layout_tree().release_value_but_fixme_should_propagate_errors());
  191. builder.append("\n"sv);
  192. builder.append(view.dump_paint_tree().release_value_but_fixme_should_propagate_errors());
  193. result = builder.to_string().release_value_but_fixme_should_propagate_errors();
  194. loop.quit(0);
  195. };
  196. view.on_text_test_finish = {};
  197. } else if (mode == TestMode::Text) {
  198. view.on_load_finish = {};
  199. view.on_text_test_finish = [&]() {
  200. result = view.dump_text().release_value_but_fixme_should_propagate_errors();
  201. loop.quit(0);
  202. };
  203. }
  204. timeout_timer->start();
  205. loop.exec();
  206. if (did_timeout)
  207. return TestResult::Timeout;
  208. auto expectation_file_or_error = Core::File::open(expectation_path, Core::File::OpenMode::Read);
  209. if (expectation_file_or_error.is_error()) {
  210. warnln("Failed opening '{}': {}", expectation_path, expectation_file_or_error.error());
  211. return expectation_file_or_error.release_error();
  212. }
  213. auto expectation_file = expectation_file_or_error.release_value();
  214. auto expectation = TRY(String::from_utf8(StringView(TRY(expectation_file->read_until_eof()).bytes())));
  215. auto actual = result;
  216. auto actual_trimmed = TRY(actual.trim("\n"sv, TrimMode::Right));
  217. auto expectation_trimmed = TRY(expectation.trim("\n"sv, TrimMode::Right));
  218. if (actual_trimmed == expectation_trimmed)
  219. return TestResult::Pass;
  220. auto const color_output = isatty(STDOUT_FILENO) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No;
  221. if (color_output == Diff::ColorOutput::Yes)
  222. outln("\n\033[33;1mTest failed\033[0m: {}", input_path);
  223. else
  224. outln("\nTest failed: {}", input_path);
  225. auto hunks = TRY(Diff::from_text(expectation, actual, 3));
  226. auto out = TRY(Core::File::standard_output());
  227. TRY(Diff::write_unified_header(expectation_path, expectation_path, *out));
  228. for (auto const& hunk : hunks)
  229. TRY(Diff::write_unified(hunk, *out, color_output));
  230. return TestResult::Fail;
  231. }
  232. static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, StringView input_path, bool dump_failed_ref_tests, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS)
  233. {
  234. Core::EventLoop loop;
  235. bool did_timeout = false;
  236. auto timeout_timer = TRY(Core::Timer::create_single_shot(timeout_in_milliseconds, [&] {
  237. did_timeout = true;
  238. loop.quit(0);
  239. }));
  240. view.load(URL::create_with_file_scheme(TRY(FileSystem::real_path(input_path)).to_byte_string()));
  241. RefPtr<Gfx::Bitmap> actual_screenshot, expectation_screenshot;
  242. view.on_load_finish = [&](auto const&) {
  243. if (actual_screenshot) {
  244. expectation_screenshot = view.take_screenshot();
  245. loop.quit(0);
  246. } else {
  247. actual_screenshot = view.take_screenshot();
  248. view.debug_request("load-reference-page");
  249. }
  250. };
  251. timeout_timer->start();
  252. loop.exec();
  253. if (did_timeout)
  254. return TestResult::Timeout;
  255. VERIFY(actual_screenshot);
  256. VERIFY(expectation_screenshot);
  257. if (actual_screenshot->visually_equals(*expectation_screenshot))
  258. return TestResult::Pass;
  259. if (dump_failed_ref_tests) {
  260. warnln("\033[33;1mRef test {} failed; dumping screenshots\033[0m", input_path);
  261. auto title = LexicalPath::title(input_path);
  262. auto dump_screenshot = [&](Gfx::Bitmap& bitmap, StringView path) -> ErrorOr<void> {
  263. auto screenshot_file = TRY(Core::File::open(path, Core::File::OpenMode::Write));
  264. auto encoded_data = TRY(Gfx::PNGWriter::encode(bitmap));
  265. TRY(screenshot_file->write_until_depleted(encoded_data));
  266. warnln("\033[33;1mDumped {}\033[0m", TRY(FileSystem::real_path(path)));
  267. return {};
  268. };
  269. auto mkdir_result = Core::System::mkdir("test-dumps"sv, 0755);
  270. if (mkdir_result.is_error() && mkdir_result.error().code() != EEXIST)
  271. return mkdir_result.release_error();
  272. TRY(dump_screenshot(*actual_screenshot, TRY(String::formatted("test-dumps/{}.png", title))));
  273. TRY(dump_screenshot(*expectation_screenshot, TRY(String::formatted("test-dumps/{}-ref.png", title))));
  274. }
  275. return TestResult::Fail;
  276. }
  277. static ErrorOr<TestResult> run_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode, bool dump_failed_ref_tests)
  278. {
  279. s_current_test_path = input_path;
  280. switch (mode) {
  281. case TestMode::Text:
  282. case TestMode::Layout:
  283. return run_dump_test(view, input_path, expectation_path, mode);
  284. case TestMode::Ref:
  285. return run_ref_test(view, input_path, dump_failed_ref_tests);
  286. default:
  287. VERIFY_NOT_REACHED();
  288. }
  289. }
  290. struct Test {
  291. String input_path;
  292. String expectation_path;
  293. TestMode mode;
  294. Optional<TestResult> result;
  295. };
  296. static Vector<ByteString> s_skipped_tests;
  297. static ErrorOr<void> load_test_config(StringView test_root_path)
  298. {
  299. auto config_path = LexicalPath::join(test_root_path, "TestConfig.ini"sv);
  300. auto config_or_error = Core::ConfigFile::open(config_path.string());
  301. if (config_or_error.is_error()) {
  302. if (config_or_error.error().code() == ENOENT)
  303. return {};
  304. dbgln("Unable to open test config {}", config_path);
  305. return config_or_error.release_error();
  306. }
  307. auto config = config_or_error.release_value();
  308. for (auto const& group : config->groups()) {
  309. if (group == "Skipped"sv) {
  310. for (auto& key : config->keys(group))
  311. s_skipped_tests.append(LexicalPath::join(test_root_path, key).string());
  312. } else {
  313. warnln("Unknown group '{}' in config {}", group, config_path);
  314. }
  315. }
  316. return {};
  317. }
  318. static ErrorOr<void> collect_dump_tests(Vector<Test>& tests, StringView path, StringView trail, TestMode mode)
  319. {
  320. Core::DirIterator it(TRY(String::formatted("{}/input/{}", path, trail)).to_byte_string(), Core::DirIterator::Flags::SkipDots);
  321. while (it.has_next()) {
  322. auto name = it.next_path();
  323. auto input_path = TRY(FileSystem::real_path(TRY(String::formatted("{}/input/{}/{}", path, trail, name))));
  324. if (FileSystem::is_directory(input_path)) {
  325. TRY(collect_dump_tests(tests, path, TRY(String::formatted("{}/{}", trail, name)), mode));
  326. continue;
  327. }
  328. if (!name.ends_with(".html"sv))
  329. continue;
  330. auto basename = LexicalPath::title(name);
  331. auto expectation_path = TRY(String::formatted("{}/expected/{}/{}.txt", path, trail, basename));
  332. tests.append({ move(input_path), move(expectation_path), mode, {} });
  333. }
  334. return {};
  335. }
  336. static ErrorOr<void> collect_ref_tests(Vector<Test>& tests, StringView path)
  337. {
  338. TRY(Core::Directory::for_each_entry(path, Core::DirIterator::SkipDots, [&](Core::DirectoryEntry const& entry, Core::Directory const&) -> ErrorOr<IterationDecision> {
  339. if (entry.type == Core::DirectoryEntry::Type::Directory)
  340. return IterationDecision::Continue;
  341. auto input_path = TRY(FileSystem::real_path(TRY(String::formatted("{}/{}", path, entry.name))));
  342. tests.append({ move(input_path), {}, TestMode::Ref, {} });
  343. return IterationDecision::Continue;
  344. }));
  345. return {};
  346. }
  347. static ErrorOr<int> run_tests(HeadlessWebContentView& view, StringView test_root_path, bool dump_failed_ref_tests)
  348. {
  349. view.clear_content_filters();
  350. TRY(load_test_config(test_root_path));
  351. Vector<Test> tests;
  352. TRY(collect_dump_tests(tests, TRY(String::formatted("{}/Layout", test_root_path)), "."sv, TestMode::Layout));
  353. TRY(collect_dump_tests(tests, TRY(String::formatted("{}/Text", test_root_path)), "."sv, TestMode::Text));
  354. TRY(collect_ref_tests(tests, TRY(String::formatted("{}/Ref", test_root_path))));
  355. size_t pass_count = 0;
  356. size_t fail_count = 0;
  357. size_t timeout_count = 0;
  358. size_t skipped_count = 0;
  359. bool is_tty = isatty(STDOUT_FILENO);
  360. outln("Running {} tests...", tests.size());
  361. for (size_t i = 0; i < tests.size(); ++i) {
  362. auto& test = tests[i];
  363. if (is_tty) {
  364. // Keep clearing and reusing the same line if stdout is a TTY.
  365. out("\33[2K\r");
  366. }
  367. out("{}/{}: {}", i + 1, tests.size(), LexicalPath::relative_path(test.input_path, test_root_path));
  368. if (is_tty)
  369. fflush(stdout);
  370. else
  371. outln("");
  372. if (s_skipped_tests.contains_slow(test.input_path.bytes_as_string_view())) {
  373. test.result = TestResult::Skipped;
  374. ++skipped_count;
  375. continue;
  376. }
  377. test.result = TRY(run_test(view, test.input_path, test.expectation_path, test.mode, dump_failed_ref_tests));
  378. switch (*test.result) {
  379. case TestResult::Pass:
  380. ++pass_count;
  381. break;
  382. case TestResult::Fail:
  383. ++fail_count;
  384. break;
  385. case TestResult::Timeout:
  386. ++timeout_count;
  387. break;
  388. case TestResult::Skipped:
  389. VERIFY_NOT_REACHED();
  390. break;
  391. }
  392. }
  393. if (is_tty)
  394. outln("\33[2K\rDone!");
  395. outln("==================================================");
  396. outln("Pass: {}, Fail: {}, Skipped: {}, Timeout: {}", pass_count, fail_count, skipped_count, timeout_count);
  397. outln("==================================================");
  398. for (auto& test : tests) {
  399. if (*test.result == TestResult::Pass)
  400. continue;
  401. outln("{}: {}", test_result_to_string(*test.result), test.input_path);
  402. }
  403. if (timeout_count == 0 && fail_count == 0)
  404. return 0;
  405. return 1;
  406. }
  407. ErrorOr<int> serenity_main(Main::Arguments arguments)
  408. {
  409. Core::EventLoop event_loop;
  410. int screenshot_timeout = 1;
  411. StringView raw_url;
  412. auto resources_folder = "/res"sv;
  413. StringView web_driver_ipc_path;
  414. bool dump_failed_ref_tests = false;
  415. bool dump_layout_tree = false;
  416. bool dump_text = false;
  417. bool is_layout_test_mode = false;
  418. StringView test_root_path;
  419. Core::ArgsParser args_parser;
  420. args_parser.set_general_help("This utility runs the Browser in headless mode.");
  421. args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n");
  422. args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd');
  423. args_parser.add_option(dump_text, "Dump text and exit", "dump-text", 'T');
  424. args_parser.add_option(test_root_path, "Run tests in path", "run-tests", 'R', "test-root-path");
  425. args_parser.add_option(dump_failed_ref_tests, "Dump screenshots of failing ref tests", "dump-failed-ref-tests", 'D');
  426. args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path");
  427. args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path");
  428. args_parser.add_option(is_layout_test_mode, "Enable layout test mode", "layout-test-mode", 0);
  429. args_parser.add_positional_argument(raw_url, "URL to open", "url", Core::ArgsParser::Required::No);
  430. args_parser.parse(arguments);
  431. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  432. Gfx::FontDatabase::set_window_title_font_query("Katica 10 700 0");
  433. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  434. Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_utf8(resources_folder))));
  435. auto theme_path = LexicalPath::join(resources_folder, "themes"sv, "Default.ini"sv);
  436. auto theme = TRY(Gfx::load_system_theme(theme_path.string()));
  437. // FIXME: Allow passing the window size as an argument.
  438. static constexpr Gfx::IntSize window_size { 800, 600 };
  439. if (!test_root_path.is_empty()) {
  440. // --run-tests implies --layout-test-mode.
  441. is_layout_test_mode = true;
  442. }
  443. auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path, is_layout_test_mode ? Ladybird::IsLayoutTestMode::Yes : Ladybird::IsLayoutTestMode::No));
  444. RefPtr<Core::Timer> timer;
  445. if (!test_root_path.is_empty()) {
  446. return run_tests(*view, test_root_path, dump_failed_ref_tests);
  447. }
  448. if (dump_layout_tree) {
  449. view->on_load_finish = [&](auto const&) {
  450. (void)view->take_screenshot();
  451. auto layout_tree = view->dump_layout_tree().release_value_but_fixme_should_propagate_errors();
  452. auto paint_tree = view->dump_paint_tree().release_value_but_fixme_should_propagate_errors();
  453. out("{}\n{}", layout_tree, paint_tree);
  454. fflush(stdout);
  455. event_loop.quit(0);
  456. };
  457. } else if (dump_text) {
  458. view->on_load_finish = [&](auto const&) {
  459. auto text = view->dump_text().release_value_but_fixme_should_propagate_errors();
  460. out("{}", text);
  461. fflush(stdout);
  462. event_loop.quit(0);
  463. };
  464. } else if (web_driver_ipc_path.is_empty()) {
  465. timer = TRY(load_page_for_screenshot_and_exit(event_loop, *view, screenshot_timeout));
  466. }
  467. auto url = WebView::sanitize_url(raw_url);
  468. if (!url.has_value()) {
  469. warnln("Invalid URL: \"{}\"", raw_url);
  470. return Error::from_string_literal("Invalid URL");
  471. }
  472. view->load(*url);
  473. return event_loop.exec();
  474. }