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