<commit_before>import pytest
import re

from capybara.exceptions import ElementNotFound
from capybara.tests.helpers import extract_results


class ClickButtonTestCase:
    @pytest.fixture(autouse=True)
    def setup_session(self, session):
        session.visit("/form")


class TestClickButton(ClickButtonTestCase):
    @pytest.mark.requires("js")
    def test_waits_for_asynchronous_load(self, session):
        session.visit("/with_js")
        session.click_link("Click me")
        session.click_button("New Here")

    def test_submits_the_latest_given_value_with_multiple_values_of_the_same_name(self, session):
        session.check("Terms of Use")
        session.click_button("awesome")
        assert extract_results(session).getlist("form[terms_of_use]")[-1] == "1"

    def test_submits_form_for_submit_button_by_id(self, session):
        session.click_button("awe123")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_submit_button_by_title(self, session):
        session.click_button("What an Awesome Button")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_submit_button_by_partial_title(self, session):
        session.click_button("What an Awesome")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_raises_an_error_for_partial_title_when_exact_is_true(self, session):
        with pytest.raises(ElementNotFound):
            session.click_button("What an Awesome", exact=True)

    def test_submits_form1_for_associated_button_located_within_form2(self, session):
        session.click_button("other_form_button")
        assert extract_results(session)["form[which_form]"] == "form1"

    def test_does_not_error_when_submit_button_not_associated_with_any_form_is_clicked(self, session):
        session.click_button("no_form_button")

    def test_submits_form_for_image_button_with_alt(self, session):
        session.click_button("oh hai thar")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_alt(self, session):
        session.click_button("hai")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_value(self, session):
        session.click_button("okay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_value(self, session):
        session.click_button("kay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_id(self, session):
        session.click_button("okay556")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_title(self, session):
        session.click_button("Okay 556 Image")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_title(self, session):
        session.click_button("Okay 556")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_text(self, session):
        session.click_button("Click me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_text(self, session):
        session.click_button("Click")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_id(self, session):
        session.click_button("click_me_123")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_value(self, session):
        session.click_button("click_me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_value(self, session):
        session.click_button("ck_me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_title(self, session):
        session.click_button("Click Title button")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_title(self, session):
        session.click_button("Click Title")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_descendant_image_alt(self, session):
        session.click_button("A horse eating hay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_descendant_image_alt(self, session):
        session.click_button("se eating h")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_raises_an_error_for_locator_that_does_not_exist(self, session):
        with pytest.raises(ElementNotFound) as excinfo:
            session.click_button("does not exist")
        assert "Unable to find button 'does not exist'" in str(excinfo.value)

    def test_serializes_and_sends_valueless_buttons_that_were_clicked(self, session):
        session.click_button("No Value!")
        assert extract_results(session)["form[no_value]"] is not None

    def test_does_not_send_image_buttons_that_were_not_clicked(self, session):
        session.click_button("awesome")
        assert extract_results(session).get("form[okay]") is None

    def test_follows_redirects(self, session):
        session.click_button("Go FAR")
        assert session.has_text("You landed")
        assert re.compile(r"/landed$").search(session.current_url)

    def test_posts_back_to_the_same_url_when_no_action_given(self, session):
        session.visit("/postback")
        session.click_button("With no action")
        assert session.has_text("Postback")

    def test_raises_an_error_for_disabled_buttons(self, session):
        with pytest.raises(ElementNotFound):
            session.click_button("Disabled button")

    def test_encodes_complex_field_names(self, session):
        session.fill_in("address1_city", value="Paris")
        session.fill_in("address1_street", value="CDG")

        session.fill_in("address2_city", value="Mikolaiv")
        session.fill_in("address2_street", value="PGS")

        session.click_button("awesome")<commit_msg>Add GET form method test<commit_after>import pytest
import re

from capybara.exceptions import ElementNotFound
from capybara.tests.helpers import extract_results


class ClickButtonTestCase:
    @pytest.fixture(autouse=True)
    def setup_session(self, session):
        session.visit("/form")


class TestClickButton(ClickButtonTestCase):
    @pytest.mark.requires("js")
    def test_waits_for_asynchronous_load(self, session):
        session.visit("/with_js")
        session.click_link("Click me")
        session.click_button("New Here")

    def test_submits_the_latest_given_value_with_multiple_values_of_the_same_name(self, session):
        session.check("Terms of Use")
        session.click_button("awesome")
        assert extract_results(session).getlist("form[terms_of_use]")[-1] == "1"

    def test_submits_form_for_submit_button_by_id(self, session):
        session.click_button("awe123")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_submit_button_by_title(self, session):
        session.click_button("What an Awesome Button")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_submit_button_by_partial_title(self, session):
        session.click_button("What an Awesome")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_raises_an_error_for_partial_title_when_exact_is_true(self, session):
        with pytest.raises(ElementNotFound):
            session.click_button("What an Awesome", exact=True)

    def test_submits_form1_for_associated_button_located_within_form2(self, session):
        session.click_button("other_form_button")
        assert extract_results(session)["form[which_form]"] == "form1"

    def test_does_not_error_when_submit_button_not_associated_with_any_form_is_clicked(self, session):
        session.click_button("no_form_button")

    def test_submits_form_for_image_button_with_alt(self, session):
        session.click_button("oh hai thar")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_alt(self, session):
        session.click_button("hai")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_value(self, session):
        session.click_button("okay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_value(self, session):
        session.click_button("kay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_id(self, session):
        session.click_button("okay556")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_title(self, session):
        session.click_button("Okay 556 Image")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_image_button_with_partial_title(self, session):
        session.click_button("Okay 556")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_text(self, session):
        session.click_button("Click me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_text(self, session):
        session.click_button("Click")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_id(self, session):
        session.click_button("click_me_123")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_value(self, session):
        session.click_button("click_me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_value(self, session):
        session.click_button("ck_me")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_title(self, session):
        session.click_button("Click Title button")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_title(self, session):
        session.click_button("Click Title")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_descendant_image_alt(self, session):
        session.click_button("A horse eating hay")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_submits_form_for_button_tag_with_partial_descendant_image_alt(self, session):
        session.click_button("se eating h")
        assert extract_results(session)["form[first_name]"] == "John"

    def test_raises_an_error_for_locator_that_does_not_exist(self, session):
        with pytest.raises(ElementNotFound) as excinfo:
            session.click_button("does not exist")
        assert "Unable to find button 'does not exist'" in str(excinfo.value)

    def test_serializes_and_sends_valueless_buttons_that_were_clicked(self, session):
        session.click_button("No Value!")
        assert extract_results(session)["form[no_value]"] is not None

    def test_does_not_send_image_buttons_that_were_not_clicked(self, session):
        session.click_button("awesome")
        assert extract_results(session).get("form[okay]") is None

    def test_serializes_and_sends_get_forms(self, session):
        session.visit("/form")
        session.click_button("med")
        results = extract_results(session)
        assert results["form[middle_name]"] == "Darren"
        assert results.get("form[foo]") is None

    def test_follows_redirects(self, session):
        session.click_button("Go FAR")
        assert session.has_text("You landed")
        assert re.compile(r"/landed$").search(session.current_url)

    def test_posts_back_to_the_same_url_when_no_action_given(self, session):
        session.visit("/postback")
        session.click_button("With no action")
        assert session.has_text("Postback")

    def test_raises_an_error_for_disabled_buttons(self, session):
        with pytest.raises(ElementNotFound):
            session.click_button("Disabled button")

    def test_encodes_complex_field_names(self, session):
        session.fill_in("address1_city", value="Paris")
        session.fill_in("address1_street", value="CDG")

        session.fill_in("address2_city", value="Mikolaiv")
        session.fill_in("address2_street", value="PGS")

        session.click_button("awesome")
{'fraction_non_alphanumeric': 0.10677914576683387, 'fraction_numerical': 0.00364797081623347, 'mean_word_length': 4.721304347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663420', 'n_tokens_mistral': 4221, 'n_tokens_neox': 4062, 'n_words': 593}