From 4b8e9492d9003ca357a4402f831112dd72efd2f8 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Thu, 3 Feb 2022 08:33:26 +0100 Subject: [PATCH] Refs #32559 -- Added selenium test for FloatField client-side validation. step="any" is required for non-integer values. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#step Covers behaviour added in 7ec2a21be15af5b2c7513482c3bcfdd1e12782ed. --- .../field_tests/test_floatfield.py | 23 +++++++++++++++++++ tests/forms_tests/urls.py | 3 ++- tests/forms_tests/views.py | 11 +++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/forms_tests/field_tests/test_floatfield.py b/tests/forms_tests/field_tests/test_floatfield.py index d6dffaccb1..7330c33503 100644 --- a/tests/forms_tests/field_tests/test_floatfield.py +++ b/tests/forms_tests/field_tests/test_floatfield.py @@ -1,7 +1,9 @@ from django.core.exceptions import ValidationError from django.forms import FloatField, NumberInput from django.test import SimpleTestCase +from django.test.selenium import SeleniumTestCase from django.test.utils import ignore_warnings, override_settings +from django.urls import reverse from django.utils import formats, translation from django.utils.deprecation import RemovedInDjango50Warning @@ -111,3 +113,24 @@ class FloatFieldTest(FormFieldAssertionsMixin, SimpleTestCase): msg = "'Enter a number.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('1,001.1') + + +@override_settings(ROOT_URLCONF='forms_tests.urls') +class FloatFieldHTMLTest(SeleniumTestCase): + + available_apps = ['forms_tests'] + + def test_float_field_rendering_passes_client_side_validation(self): + """ + Rendered widget allows non-integer value with the client-side + validation. + """ + from selenium.webdriver.common.by import By + + self.selenium.get(self.live_server_url + reverse('form_view')) + number_input = self.selenium.find_element(By.ID, 'id_number') + number_input.send_keys('0.5') + is_valid = self.selenium.execute_script( + "return document.getElementById('id_number').checkValidity()" + ) + self.assertTrue(is_valid) diff --git a/tests/forms_tests/urls.py b/tests/forms_tests/urls.py index dda75b52b4..7e27927099 100644 --- a/tests/forms_tests/urls.py +++ b/tests/forms_tests/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from .views import ArticleFormView +from .views import ArticleFormView, form_view urlpatterns = [ + path('form_view/', form_view, name='form_view'), path('model_form//', ArticleFormView.as_view(), name='article_form'), ] diff --git a/tests/forms_tests/views.py b/tests/forms_tests/views.py index 44e4c84344..20f1bf161d 100644 --- a/tests/forms_tests/views.py +++ b/tests/forms_tests/views.py @@ -1,4 +1,6 @@ from django import forms +from django.http import HttpResponse +from django.template import Context, Template from django.views.generic.edit import UpdateView from .models import Article @@ -16,3 +18,12 @@ class ArticleFormView(UpdateView): model = Article success_url = '/' form_class = ArticleForm + + +def form_view(request): + class Form(forms.Form): + number = forms.FloatField() + + template = Template('{{ form }}') + context = Context({'form': Form()}) + return HttpResponse(template.render(context))