Implementation:Teamcapybara Capybara Spec Has Field
| Knowledge Sources | |
|---|---|
| Domains | Testing, Test_Specification |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
Shared RSpec test suite that validates the has_field?, has_no_field?, has_checked_field?, has_no_checked_field?, has_unchecked_field?, and has_no_unchecked_field? predicate methods across all Capybara drivers.
Description
Capybara::SpecHelper provides shared test specifications that driver implementations must pass. This file contains the shared examples for the #has_field?, #has_checked_field?, and #has_unchecked_field? family of methods, exercising field type matching (text, email, tel, textarea, select, hidden), value validation with strings and regular expressions, checked/unchecked state for checkboxes and radio buttons, disabled state handling, multiple attribute detection, focus state (requires: [:active_element]), HTML5 validation messages (requires: [:html_validation]), and validity state (requires: [:js]).
Usage
Include these shared examples in driver-specific test suites to verify compliance with Capybara's expected behavior contract.
Code Reference
Source Location
- Repository: Teamcapybara_Capybara
- File: lib/capybara/spec/session/has_field_spec.rb
- Lines: 1-389
Signature
Capybara::SpecHelper.spec '#has_field' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_no_field' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_checked_field?' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_no_checked_field?' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_unchecked_field?' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_no_unchecked_field?' do
# shared test examples
end
Import
require 'capybara/spec/spec_helper'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| session | Capybara::Session | Yes | Active session to test against |
Outputs
| Name | Type | Description |
|---|---|---|
| Test results | RSpec::Core::Example | Pass/fail assertions |
Key Test Scenarios
#has_field / #has_no_field
- Presence and absence -- detecting fields by label text, name, or id, including symbol locators
- Value matching -- :with option using exact strings and regular expressions, before and after fill_in
- Filter error messages -- descriptive error output when a single element matches the selector but fails a filter (e.g., wrong value, wrong readonly state, wrong checked state)
- Type filtering -- :type option matching text, email, tel, textarea, select, and hidden field types
- Multiple attribute -- :multiple option for multi-value email inputs
- Focus state (requires: [:active_element]) -- :focused option detecting active element via tab navigation
- HTML5 validation messages (requires: [:html_validation]) -- :validation_message with String and Regexp matching against browser-reported constraint validation
- Validity state (requires: [:js]) -- :valid option checking field validity after user input
#has_checked_field? / #has_no_checked_field?
- Checked state detection -- identifying checked radio buttons and checkboxes
- Disabled handling -- :disabled => true, :disabled => false, and :disabled => :all for including disabled fields
- State transitions -- verifying state after check, uncheck, and choose operations on checkboxes and radio buttons
#has_unchecked_field? / #has_no_unchecked_field?
- Unchecked state detection -- identifying unchecked checkboxes and radio buttons
- Disabled handling -- same disabled option matrix as checked field specs
- State transitions -- verifying state after check, uncheck, and choose operations
- Locator-less usage -- calling predicates without a locator, filtering by :disabled and :id options only