From e37f0bc1bd3a48a4416eaebbccaf8249f3ecfdbe Mon Sep 17 00:00:00 2001 From: Alexander Brandon Coles Date: Sun, 2 Feb 2025 00:25:10 -0300 Subject: [PATCH] Try to handle browser JSON responses more robustly Sticking to Capybara's API for getting document text should (in theory) make this less likely to break in the event of a browser changing how it renders plain text/JSON responses. [skip changeset] --- test/system/alpha/action_menu_test.rb | 26 +++++++++----------------- test/system/alpha/select_panel_test.rb | 9 +++------ 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/test/system/alpha/action_menu_test.rb b/test/system/alpha/action_menu_test.rb index 8ede5fe220..f213f4d72c 100644 --- a/test/system/alpha/action_menu_test.rb +++ b/test/system/alpha/action_menu_test.rb @@ -404,8 +404,7 @@ def test_single_select_form_submission find("input[type=submit]").click - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "fast_forward", response["value"] end @@ -417,8 +416,7 @@ def test_single_select_form_uses_label_if_no_value_provided find("input[type=submit]").click - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "Resolve", response["value"] end @@ -434,8 +432,7 @@ def test_multiple_select_form_submission find("input[type=submit]").click - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) # "ours" is pre-selected assert_equal %w[fast_forward recursive ours], response["value"] @@ -453,8 +450,7 @@ def test_multiple_select_form_uses_label_if_no_value_provided find("input[type=submit]").click - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) # "ours" is pre-selected assert_equal %w[fast_forward ours Resolve], response["value"] @@ -471,7 +467,7 @@ def test_individual_items_can_submit_post_requests_via_forms click_on_invoker_button click_on_fourth_item - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "bar", response["value"] end @@ -481,8 +477,7 @@ def test_single_select_items_can_submit_forms click_on_invoker_button click_on_first_item - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "group-by-repository", response["value"] end @@ -494,8 +489,7 @@ def test_single_select_items_can_submit_forms_on_enter # "click" first item keyboard.type(:enter) - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "group-by-repository", response["value"] end @@ -507,8 +501,7 @@ def test_single_select_items_can_submit_forms_on_keydown_space # "click" first item keyboard.type(:space) - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "group-by-repository", response["value"] end @@ -518,8 +511,7 @@ def test_single_select_items_can_submit_forms_with_multiple_fields click_on_invoker_button click_on_first_item - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "query", response.dig("other_params", "query") end diff --git a/test/system/alpha/select_panel_test.rb b/test/system/alpha/select_panel_test.rb index f6c3e8f750..4df2360062 100644 --- a/test/system/alpha/select_panel_test.rb +++ b/test/system/alpha/select_panel_test.rb @@ -1221,8 +1221,7 @@ def test_single_select_form click_on "Submit" - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "item2", response.dig(*%w(form_params item)) end @@ -1232,8 +1231,7 @@ def test_single_select_form_submits_pre_selected_item # the first item has been pre-selected, so there's no need to select any items click_on "Submit" - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) assert_equal "item1", response.dig(*%w(form_params item)) end @@ -1246,8 +1244,7 @@ def test_multi_select_form click_on "Submit" - # for some reason the JSON response is wrapped in HTML, I have no idea why - response = JSON.parse(find("pre").text) + response = JSON.parse(page.document.text) # first item is pre-selected assert_equal ["item1", "item2"], response.dig(*%w(form_params item))