Best Capybara code snippet using Capybara.close
become_closed_spec.rb
Source:become_closed_spec.rb
1# frozen_string_literal: true2Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do3 let!(:window) { @session.current_window }4 let(:other_window) do5 @session.window_opened_by do6 @session.find(:css, '#openWindow').click7 end8 end9 before do10 @session.visit('/with_windows')11 end12 after do13 @session.document.synchronize(5, errors: [Capybara::CapybaraError]) do14 raise Capybara::CapybaraError if @session.windows.size != 115 end16 @session.switch_to_window(window)17 end18 context 'with :wait option' do19 it 'should wait if value of :wait is more than timeout' do20 @session.within_window other_window do21 @session.execute_script('setTimeout(function(){ window.close(); }, 500);')22 end23 Capybara.using_wait_time 0.1 do24 expect(other_window).to become_closed(wait: 5)25 end26 end27 it 'should raise error if value of :wait is less than timeout' do28 @session.within_window other_window do29 @session.execute_script('setTimeout(function(){ window.close(); }, 1000);')30 end31 Capybara.using_wait_time 2 do32 expect do33 expect(other_window).to become_closed(wait: 0.2)34 end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.2 seconds\Z/)35 end36 end37 end38 context 'without :wait option' do39 it 'should wait if value of default_max_wait_time is more than timeout' do40 @session.within_window other_window do41 @session.execute_script('setTimeout(function(){ window.close(); }, 500);')42 end43 Capybara.using_wait_time 5 do44 expect(other_window).to become_closed45 end46 end47 it 'should raise error if value of default_max_wait_time is less than timeout' do48 @session.within_window other_window do49 @session.execute_script('setTimeout(function(){ window.close(); }, 900);')50 end51 Capybara.using_wait_time 0.4 do52 expect do53 expect(other_window).to become_closed54 end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.4 seconds\Z/)55 end56 end57 end58 context 'with not_to' do59 it "should not raise error if window doesn't close before default_max_wait_time" do60 @session.within_window other_window do61 @session.execute_script('setTimeout(function(){ window.close(); }, 1000);')62 end63 Capybara.using_wait_time 0.3 do64 expect do65 expect(other_window).not_to become_closed66 end.not_to raise_error67 end68 end69 it 'should raise error if window closes before default_max_wait_time' do70 @session.within_window other_window do71 @session.execute_script('setTimeout(function(){ window.close(); }, 700);')72 end73 Capybara.using_wait_time 3.1 do74 expect do75 expect(other_window).not_to become_closed76 end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> not to become closed after 3.1 seconds\Z/)77 end78 end79 end80end
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!