From 1857cb1e0fb47044ceb888b4e1307dd095b8b863 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 26 Jan 2024 16:25:34 -0500 Subject: [PATCH] Delegate `Capybara::Result#to_ary` (#2733) Delegate to `Capybara::Result#full_results` in order to support Ruby's multiple assignment: ```ruby node = Capybara.string <<~HTML HTML first, *rest, last = node.all("//li", minimum: 0) expect(first).to have_text "Alpha" expect(rest.first).to have_text "Beta" expect(rest.last).to have_text "Gamma" expect(last).to have_text "Delta" ``` --- History.md | 4 ++++ lib/capybara/result.rb | 2 +- spec/result_spec.rb | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index ce0ff426a..73ea77fbd 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,10 @@ Release date: unreleased * Dropped support for Ruby 2.7, 3.0+ is now required * Dropped support for Selenium < 4.8 +### Added + +* Add `Capybara::Result#to_ary` to support multiple assignment + # Version 3.39.2 Release date: 2023-06-10 diff --git a/lib/capybara/result.rb b/lib/capybara/result.rb index 5967f10dc..d7b025879 100644 --- a/lib/capybara/result.rb +++ b/lib/capybara/result.rb @@ -34,7 +34,7 @@ def initialize(elements, query) @allow_reload = false end - def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample + def_delegators :full_results, :size, :length, :last, :values_at, :inspect, :sample, :to_ary alias index find_index diff --git a/spec/result_spec.rb b/spec/result_spec.rb index e0ae100f7..01995edee 100644 --- a/spec/result_spec.rb +++ b/spec/result_spec.rb @@ -30,6 +30,15 @@ result.last.text == 'Delta' end + it 'splats into multiple assignment' do + first, *rest, last = result + + expect(first).to have_text 'Alpha' + expect(rest.first).to have_text 'Beta' + expect(rest.last).to have_text 'Gamma' + expect(last).to have_text 'Delta' + end + it 'can supports values_at method' do expect(result.values_at(0, 2).map(&:text)).to eq(%w[Alpha Gamma]) end