tree: be43f5f6e4a0da85148b9be36dd70b922bb192c0 [path history] [tgz]
  1. android/
  2. accessibility_notification_waiter.cc
  3. accessibility_notification_waiter.h
  4. attribution_config.h
  5. attribution_simulator.h
  6. attribution_simulator_environment.h
  7. audio_service_test_helper.cc
  8. audio_service_test_helper.h
  9. back_forward_cache_util.cc
  10. back_forward_cache_util.h
  11. background_color_change_waiter.cc
  12. background_color_change_waiter.h
  13. background_sync_test_util.cc
  14. background_sync_test_util.h
  15. blink_test_browser_support.h
  16. blink_test_environment.cc
  17. blink_test_environment.h
  18. bluetooth_test_utils.cc
  19. bluetooth_test_utils.h
  20. browser_task_environment.cc
  21. browser_task_environment.h
  22. browser_task_environment_unittest.cc
  23. browser_test.h
  24. browser_test_base.cc
  25. browser_test_base.h
  26. browser_test_utils.cc
  27. browser_test_utils.h
  28. browsing_data_remover_test_util.cc
  29. browsing_data_remover_test_util.h
  30. browsing_topics_test_util.cc
  31. browsing_topics_test_util.h
  32. commit_message_delayer.cc
  33. commit_message_delayer.h
  34. content_browser_test.cc
  35. content_browser_test.h
  36. content_browser_test.rs
  37. content_browser_test_shell_main_delegate.cc
  38. content_browser_test_shell_main_delegate.h
  39. content_browser_test_utils.cc
  40. content_browser_test_utils.h
  41. content_browser_test_utils_mac.mm
  42. content_cert_verifier_browser_test.cc
  43. content_cert_verifier_browser_test.h
  44. content_mock_cert_verifier.cc
  45. content_mock_cert_verifier.h
  46. content_public_browsertest_support.rs
  47. content_test_suite_base.cc
  48. content_test_suite_base.h
  49. DEPS
  50. devtools_permission_overrides_unittest.cc
  51. download_test_observer.cc
  52. download_test_observer.h
  53. fake_download_item.cc
  54. fake_download_item.h
  55. fake_frame_widget.cc
  56. fake_frame_widget.h
  57. fake_local_frame.cc
  58. fake_local_frame.h
  59. fake_remote_frame.cc
  60. fake_remote_frame.h
  61. fake_render_widget_host.cc
  62. fake_render_widget_host.h
  63. fake_service_worker_context.cc
  64. fake_service_worker_context.h
  65. fake_speech_recognition_manager.cc
  66. fake_speech_recognition_manager.h
  67. fenced_frame_test_util.cc
  68. fenced_frame_test_util.h
  69. find_test_utils.cc
  70. find_test_utils.h
  71. focus_changed_observer.cc
  72. focus_changed_observer.h
  73. frame_load_waiter.cc
  74. frame_load_waiter.h
  75. frame_test_utils.cc
  76. frame_test_utils.h
  77. hit_test_region_observer.cc
  78. hit_test_region_observer.h
  79. javascript_test_observer.cc
  80. javascript_test_observer.h
  81. local_frame_host_interceptor.cc
  82. local_frame_host_interceptor.h
  83. media_start_stop_observer.cc
  84. media_start_stop_observer.h
  85. mock_browsing_data_remover_delegate.cc
  86. mock_browsing_data_remover_delegate.h
  87. mock_client_hints_controller_delegate.h
  88. mock_download_manager.cc
  89. mock_download_manager.h
  90. mock_navigation_handle.cc
  91. mock_navigation_handle.h
  92. mock_notification_observer.cc
  93. mock_notification_observer.h
  94. mock_permission_controller.cc
  95. mock_permission_controller.h
  96. mock_permission_manager.cc
  97. mock_permission_manager.h
  98. mock_policy_container_host.cc
  99. mock_policy_container_host.h
  100. mock_render_process_host.cc
  101. mock_render_process_host.h
  102. mock_render_thread.cc
  103. mock_render_thread.h
  104. mock_resource_context.cc
  105. mock_resource_context.h
  106. mock_web_contents_observer.cc
  107. mock_web_contents_observer.h
  108. navigation_handle_observer.cc
  109. navigation_handle_observer.h
  110. navigation_simulator.h
  111. nested_message_pump_android.cc
  112. nested_message_pump_android.h
  113. network_connection_change_simulator.cc
  114. network_connection_change_simulator.h
  115. network_service_test_helper.cc
  116. network_service_test_helper.h
  117. no_renderer_crashes_assertion.cc
  118. no_renderer_crashes_assertion.h
  119. no_renderer_crashes_assertion_unittest.cc
  120. OWNERS
  121. permission_type_unittest.cc
  122. permissions_test_utils.cc
  123. permissions_test_utils.h
  124. policy_container_utils.cc
  125. policy_container_utils.h
  126. ppapi_test_utils.cc
  127. ppapi_test_utils.h
  128. preloading_test_util.cc
  129. preloading_test_util.h
  130. prerender_test_util.cc
  131. prerender_test_util.h
  132. private_network_access_util.cc
  133. private_network_access_util.h
  134. README.md
  135. referrer_unittest.cc
  136. render_frame_host_test_support.cc
  137. render_frame_host_test_support.h
  138. render_view_test.cc
  139. render_view_test.h
  140. scoped_overscroll_modes.cc
  141. scoped_overscroll_modes.h
  142. scoped_page_focus_override.cc
  143. scoped_page_focus_override.h
  144. scoped_time_zone.cc
  145. scoped_time_zone.h
  146. scoped_web_ui_controller_factory_registration.cc
  147. scoped_web_ui_controller_factory_registration.h
  148. service_worker_host_interceptor.cc
  149. service_worker_host_interceptor.h
  150. service_worker_test_helpers.cc
  151. service_worker_test_helpers.h
  152. shared_storage_test_utils.cc
  153. shared_storage_test_utils.h
  154. signed_exchange_browser_test_helper.cc
  155. signed_exchange_browser_test_helper.h
  156. simple_url_loader_test_helper.cc
  157. simple_url_loader_test_helper.h
  158. slow_download_http_response.cc
  159. slow_download_http_response.h
  160. slow_http_response.cc
  161. slow_http_response.h
  162. test_aggregation_service.cc
  163. test_aggregation_service.h
  164. test_browser_context.cc
  165. test_browser_context.h
  166. test_cert_verifier_service_factory.cc
  167. test_cert_verifier_service_factory.h
  168. test_content_client_initializer.cc
  169. test_content_client_initializer.h
  170. test_devtools_protocol_client.cc
  171. test_devtools_protocol_client.h
  172. test_download_http_response.cc
  173. test_download_http_response.h
  174. test_file_error_injector.cc
  175. test_file_error_injector.h
  176. test_fileapi_operation_waiter.cc
  177. test_fileapi_operation_waiter.h
  178. test_frame_navigation_observer.cc
  179. test_frame_navigation_observer.h
  180. test_host_resolver.cc
  181. test_host_resolver.h
  182. test_launcher.cc
  183. test_launcher.h
  184. test_navigation_observer.cc
  185. test_navigation_observer.h
  186. test_navigation_throttle.cc
  187. test_navigation_throttle.h
  188. test_navigation_throttle_inserter.cc
  189. test_navigation_throttle_inserter.h
  190. test_navigation_ui_data.cc
  191. test_navigation_ui_data.h
  192. test_notification_tracker.cc
  193. test_notification_tracker.h
  194. test_renderer_host.cc
  195. test_renderer_host.h
  196. test_service.mojom
  197. test_storage_partition.cc
  198. test_storage_partition.h
  199. test_synchronous_compositor_android.cc
  200. test_synchronous_compositor_android.h
  201. test_utils.cc
  202. test_utils.h
  203. test_utils_unittest.cc
  204. test_web_contents_factory.h
  205. test_web_ui.cc
  206. test_web_ui.h
  207. test_web_ui_data_source.cc
  208. test_web_ui_data_source.h
  209. test_web_ui_listener_observer.cc
  210. test_web_ui_listener_observer.h
  211. text_input_test_utils.cc
  212. text_input_test_utils.h
  213. text_input_test_utils_mac.mm
  214. theme_change_waiter.cc
  215. theme_change_waiter.h
  216. unittest_test_suite.cc
  217. unittest_test_suite.h
  218. url_loader_interceptor.cc
  219. url_loader_interceptor.h
  220. url_loader_monitor.cc
  221. url_loader_monitor.h
  222. web_contents_tester.cc
  223. web_contents_tester.h
  224. web_transport_simple_test_server.cc
  225. web_transport_simple_test_server.h
  226. web_ui_browsertest_util.cc
  227. web_ui_browsertest_util.h
  228. xr_test_utils.cc
  229. xr_test_utils.h
content/public/test/README.md

Content Browser Test Tips

A random collection of useful things to know when writing browser tests.

Executing JavaScript

If the test needs to use the return value of the script, use EvalJs():

  // Works with numerical types...
  EXPECT_EQ(0, EvalJs(shell(), "1 * 0");

  // ... string types ...
  EXPECT_EQ("BODY", EvalJs(shell(), "document.body.tagName");

  // and booleans too. Note the explicit use of EXPECT_EQ() instead of
  // EXPECT_TRUE() or EXPECT_FALSE(); this is intentional, and the latter
  // will not compile.
  EXPECT_EQ(false, EvalJs(shell(), "2 + 2 == 5"));

Like many other test helpers (e.g. the navigation helpers), the first argument accepts RenderFrameHost, WebContents, and other types.

  // Executes in the main frame.
  EXPECT_EQ(true, EvalJs(shell()->GetWebContents(), "window.top == window"));

  // Also executes in the main frame.
  EXPECT_EQ(true, EvalJs(shell(), "window.top == window"));

  // Executes in the first child frame of the main frame.
  EXPECT_EQ(
      false,
      EvalJs(ChildFrameAt(shell()->GetWebContents()->GetPrimaryMainFrame(), 0),
             "window.top == window"));

Otherwise, simply use ExecJs():

  EXPECT_TRUE(ExecJs("console.log('Hello world!')"));

Note that these helpers block until the script completes. For async execution, use ExecuteScriptAsync().

Finally, JsReplace() provides a convenient way to build strings for script execution:

  EXPECT_EQ("00", EvalJs(JsReplace("$1 + $2", 0, "0")));

Simulating Input

A wide range of methods are provided to simulate input such as clicks, touch, mouse moves and so on. Many reside in https://source.chromium.org/chromium/chromium/src/+/main:content/public/test/browser_test_utils.h.

When using input in tests, be aware that the renderer drops all input received when the main frame is not being updated or rendered immediately after load. There are three ways, in order of preference, to ensure that the input will be processed. Use these when your test input seems to be having no effect:

  • Use the ‘WaitForHitTestData method’ from content/public/test/hit_test_region_observer.h

  • Include visible text in the web contents you are interacting with.

  • Add ‘blink::switches::kAllowPreCommitInput’ as a command line flag.

Cross-origin navigations

For cross-origin navigations, it is to simplest to configure all hostnames to resolve to 127.0.0.1 in tests, using a snippet like this:

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

After that, EmbeddedTestServer::GetURL() can be used to generate navigable URLs with the specific origin:

  const GURL& url_a = embedded_test_server()->GetURL("a.com", "/title1.html");
  const GURL& url_b = embedded_test_server()->GetURL("b.com", "/empty.html");

Most test resources are located in //content/test/data, e.g. navigating to GetURL("a.com", "/title1.html") will serve //content/test/data/title1.html as the content.

Browser-initiated navigation to a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

NavigateToURL() begins and waits for the navigation to complete, as if the navigation was browser-initiated, e.g. from the omnibox:

  GURL url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

Note: NavigateToURL() allows subframes to be targetted, but outside of history navigations, subframe navigations are generally renderer-initiated.

Renderer-initiated navigation to a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

NavigateToURLFromRenderer() begins and waits for the navigation to complete, as if the navigation was renderer-initiated, e.g. by setting window.location:

  // Navigates the main frame.
  GURL url_1(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(shell()->GetWebContents(), url_1));

  // Navigates the main frame too.
  GURL url_2(embedded_test_server()->GetURL("b.com", "/page_with_iframe.html"));
  EXPECT_TRUE(NavigateToURLFromRenderer(shell()->GetWebContents(), url_2));

  // Navigates the first child frame.
  GURL url_3(embedded_test_server()->GetURL("a.com", "/empty.html"));
  EXPECT_TRUE(
      NavigateToURLFromRenderer(
          ChildFrameAt(shell()->GetWebContents()->GetPrimaryMainFrame(), 0),
          url_3));

Dynamically generating a page with iframes

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

cross_site_iframe_factory.html is a helper that makes it easy to generate a page with an arbitrary frame tree by navigating to a URL. The query string to the URL allows configuration of the frame tree, the origin of each frame, and a number of other options:

  GURL url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b(a),c,example.com)"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

Will generate a page with:

Main frame with origin `a.com`
  ├─ Child frame #1 with origin `b.com`
  │    └─ Grandchild frame with origin `a.com`
  ├─ Child frame #2 with origin `c.com`
  └─ Child frame #3 with origin `example.com`

Embedding an <iframe> with a specific origin

Note: using arbitrary hostnames requires the host resolver to be correctly configured.

Sometimes, a test page may need to embed a cross-origin URL. This is problematic for pages that contain only static HTML, as the embedded test server runs on a randomly selected port. Instead, static HTML can use the cross-site redirector to generate a cross-origin frame:

<!-- static_iframe.html -->
<html>
  <body>
    <iframe src="/cross-site/b.com/title1.html">
  </body>
</iframe>

Important: the cross-site redirector is not enabled by default. Override SetUpOnMainThread() to configure it like this:

  void SetUpOnMainThread() override {
    ...
    SetupCrossSiteRedirector(embedded_test_server());
    ...
  }

Simulating a slow load

Navigates to a page that takes 60 seconds to load.

  GURL url(embedded_test_server()->GetURL("/slow?60");
  EXPECT_TRUE(NavigateToURL(shell(), url));

The embedded test server also registers other default handlers that may be useful.