{"record":{"id":"dcd7749319652d8d","repo":"teamcapybara/capybara","slug":"query-failure-message","errorCode":null,"errorMessage":"query.failure_message","messagePattern":"query\\.failure_message","errorType":"exception","errorClass":"Capybara::ExpectationNotMet","httpStatus":null,"severity":"error","filePath":"lib/capybara/node/matchers.rb","lineNumber":131,"sourceCode":"          end\n        end\n      end\n\n      ##\n      #\n      # Asserts that an element has the specified CSS styles.\n      #\n      #     element.assert_matches_style( 'color' => 'rgb(0,0,255)', 'font-size' => /px/ )\n      #\n      # @param styles [Hash]\n      # @raise [Capybara::ExpectationNotMet]    If the element doesn't have the specified styles\n      #\n      def assert_matches_style(styles = nil, **options)\n        styles, options = options, {} if styles.nil?\n        query_args, query_opts = _set_query_session_options(styles, options)\n        query = Capybara::Queries::StyleQuery.new(*query_args, **query_opts)\n        synchronize(query.wait) do\n          raise Capybara::ExpectationNotMet, query.failure_message unless query.resolves_for?(self)\n        end\n        true\n      end\n\n      ##\n      # @deprecated Use {#assert_matches_style} instead.\n      #\n      def assert_style(styles = nil, **options)\n        warn 'assert_style is deprecated, please use assert_matches_style instead'\n        assert_matches_style(styles, **options)\n      end\n\n      # Asserts that all of the provided selectors are present on the given page\n      # or descendants of the current node.  If options are provided, the assertion\n      # will check that each locator is present with those options as well (other than `:wait`).\n      #\n      #     page.assert_all_of_selectors(:custom, 'Tom', 'Joe', visible: all)\n      #     page.assert_all_of_selectors(:css, '#my_div', 'a.not_clicked')","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/node/matchers.rb#L113-L149","documentation":"assert_matches_style builds a Capybara::Queries::StyleQuery and raises ExpectationNotMet with query.failure_message when the element's computed styles do not include the requested style values within the wait time. The query re-evaluates computed styles on each retry inside synchronize, so transient differences (CSS transitions, late stylesheet application) resolve themselves if they finish before the deadline.","triggerScenarios":"element.assert_matches_style('color' => 'rgb(0,0,255)') when the computed color is 'rgb(0,0,254)' or a different color function; asserting 'opacity' => '1' immediately after triggering a fade-in transition; regex value '/px/' against a font-size computed in rem/pt; styles applied by a stylesheet that loads after first assert.","commonSituations":"Animations/transitions making computed values time-dependent (assert fires mid-transition), browser-dependent computed-value normalization (colors as rgb vs rgba vs color(), font shorthand expansion), asserting shorthand properties that browsers do not report as set, headless driver default styles differing from desktop.","solutions":["Compare like-for-like computed values: use 'rgb(...)' strings or Regex values ('font-size' => /px$/)","Give transitions time: assert_matches_style('opacity' => '1', wait: 2)","Assert on long-lived end-state styles only, not mid-animation values; disable animations in the test env when possible","Check the element actually receives the CSS class/rule first (assert_selector on the class) before asserting styles"],"exampleFix":"# before\nel.assert_matches_style('opacity' => '1')  # fires mid fade-in\n\n# after\nel.assert_matches_style('opacity' => '1', wait: 3)\n# or match loosely\nel.assert_matches_style('font-size' => /px/)","handlingStrategy":"retry","validationCode":"transitioning = %w[transition animation].any? { |p| el[:style].to_s.include?(p) }\nskip unless transitioning # or add wait below","typeGuard":null,"tryCatchPattern":"begin\n  el.assert_matches_style('opacity' => '1', wait: 3)\nrescue Capybara::ExpectationNotMet => e\n  puts el.evaluate_script('getComputedStyle(arguments[0]).opacity')\n  raise\nend","preventionTips":["Assert end-state styles with wait: >= transition duration","Use regex values for values browsers normalize differently (px, rgb)","Disable CSS animations in the test environment where possible"],"tags":["capybara","style","css","assertion","computed-styles"],"backgroundTag":"css-style-mismatch","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}