Best Active_mocker_ruby code snippet using Mock.save
saver_spec.rb
Source:saver_spec.rb
...25 double(Capybara).as_null_object.tap do |m|26 allow(m).to receive(:current_driver).and_return(:default)27 end28 }29 let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }30 context 'html filename with Capybara Version 1' do31 before do32 stub_const("Capybara::VERSION", '1')33 end34 it 'has a default format of "screenshot_Y-M-D-H-M-S.ms.html"' do35 expect(capybara_mock).to receive(:save_page).with('body', File.join(capybara_root, "#{file_basename}.html"))36 saver.save37 end38 it 'uses name argument as prefix' do39 saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')40 expect(capybara_mock).to receive(:save_page).with('body', File.join(capybara_root, "custom-prefix_#{timestamp}.html"))41 saver.save42 end43 end44 context 'html filename with Capybara Version 2' do45 before do46 stub_const("Capybara::VERSION", '2')47 end48 it 'has a default format of "screenshot_Y-M-D-H-M-S.ms.html"' do49 expect(capybara_mock).to receive(:save_page).with(File.join(capybara_root, "#{file_basename}.html"))50 saver.save51 end52 it 'uses name argument as prefix' do53 saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')54 expect(capybara_mock).to receive(:save_page).with(File.join(capybara_root, "custom-prefix_#{timestamp}.html"))55 saver.save56 end57 end58 context 'screenshot image path' do59 it 'is in capybara root output' do60 expect(driver_mock).to receive(:render).with(/^#{capybara_root}\//)61 saver.save62 end63 it 'has a default filename format of "screenshot_Y-M-D-H-M-S.ms.png"' do64 expect(driver_mock).to receive(:render).with(/#{file_basename}\.png$/)65 saver.save66 end67 it "does not append timestamp if append_timestamp is false " do68 default_config = Capybara::Screenshot.append_timestamp69 Capybara::Screenshot.append_timestamp = false70 expect(driver_mock).to receive(:render).with(/screenshot.png$/)71 saver.save72 Capybara::Screenshot.append_timestamp = default_config73 end74 it 'uses filename prefix argument as basename prefix' do75 saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, true, 'custom-prefix')76 expect(driver_mock).to receive(:render).with(/#{capybara_root}\/custom-prefix_#{timestamp}\.png$/)77 saver.save78 end79 end80 it 'does not save html if false passed as html argument' do81 saver = Capybara::Screenshot::Saver.new(capybara_mock, page_mock, false)82 expect(capybara_mock).to_not receive(:save_page)83 saver.save84 expect(saver).to_not be_html_saved85 end86 context 'the current_path is empty' do87 before(:each) do88 allow(page_mock).to receive(:current_path).and_return(nil)89 end90 it 'does not save' do91 expect(capybara_mock).to_not receive(:save_page)92 expect(driver_mock).to_not receive(:render)93 saver.save94 expect(saver).to_not be_screenshot_saved95 expect(saver).to_not be_html_saved96 end97 it 'prints a warning' do98 expect(saver).to receive(:warn).with(99 'WARN: Screenshot could not be saved. `page.current_path` is empty.',100 )101 saver.save102 end103 end104 context 'when save_html raises' do105 before(:each) do106 allow(saver).to receive(:save_html).and_raise(NoMethodError.new('some error'))107 end108 it 'prints warning message' do109 expect(saver).to receive(:warn).with(110 'WARN: HTML source could not be saved. An exception is raised: #<NoMethodError: some error>.',111 )112 saver.save113 end114 it 'tries to save screenshot' do115 expect(saver).to receive(:save_screenshot)116 saver.save117 end118 end119 context 'when save_screenshot raises' do120 before(:each) do121 allow(saver).to receive(:save_screenshot).and_raise(NoMethodError.new('some error'))122 end123 it 'prints warning message' do124 expect(saver).to receive(:warn).with(125 'WARN: Screenshot could not be saved. An exception is raised: #<NoMethodError: some error>.',126 )127 saver.save128 end129 it 'tries to save screenshot' do130 expect(saver).to receive(:save_html)131 saver.save132 end133 end134 context 'when current_path raises' do135 before(:each) do136 allow(page_mock).to receive(:current_path).and_raise(NoMethodError.new('some error'))137 end138 it 'prints warning message' do139 expect(saver).to receive(:warn).with(140 'WARN: Screenshot could not be saved. `page.current_path` raised exception: #<NoMethodError: some error>.',141 )142 saver.save143 end144 it 'does not print extra warning message' do145 expect(saver).not_to receive(:warn).with(/is empty/)146 saver.save147 end148 it 'still restores the original value of Capybara.save_and_open_page_path' do149 Capybara::Screenshot.capybara_tmp_path = 'tmp/bananas'150 allow(page_mock).to receive(:current_path).and_raise151 saver.save152 if Capybara.respond_to?(:save_path)153 expect(Capybara.save_path).to eq('tmp/bananas')154 else155 expect(Capybara.save_and_open_page_path).to eq('tmp/bananas')156 end157 end158 end159 describe '#output_screenshot_path' do160 let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }161 before do162 allow(saver).to receive(:html_path) { 'page.html' }163 allow(saver).to receive(:screenshot_path) { 'screenshot.png' }164 end165 it 'outputs the path for the HTML screenshot' do166 allow(saver).to receive(:html_saved?).and_return(true)167 expect(saver).to receive(:output).with("HTML screenshot: page.html")168 saver.output_screenshot_path169 end170 it 'outputs the path for the Image screenshot' do171 allow(saver).to receive(:screenshot_saved?).and_return(true)172 expect(saver).to receive(:output).with("Image screenshot: screenshot.png")173 saver.output_screenshot_path174 end175 end176 describe 'callbacks' do177 let(:saver) { Capybara::Screenshot::Saver.new(capybara_mock, page_mock) }178 before do179 allow(saver).to receive(:html_path) { 'page.html' }180 allow(saver).to receive(:screenshot_path) { 'screenshot.png' }181 end182 before :all do183 Capybara::Screenshot.after_save_html do |path|184 puts "after_save_html ran with #{path}"185 end186 Capybara::Screenshot.after_save_screenshot do |path|187 puts "after_save_screenshot ran with #{path}"188 end189 end190 after :all do191 Capybara::Screenshot::Saver.instance_eval { @callbacks = nil }192 end193 it 'runs after_save_html callbacks' do194 expect do195 saver.save196 end.to output(/after_save_html ran with page\.html/).to_stdout197 end198 it 'runs after_save_screenshot callbacks' do199 expect do200 saver.save201 end.to output(/after_save_screenshot ran with screenshot\.png/).to_stdout202 end203 end204 describe "with selenium driver" do205 before do206 allow(capybara_mock).to receive(:current_driver).and_return(:selenium)207 end208 it 'saves via browser' do209 browser_mock = double('browser')210 expect(driver_mock).to receive(:browser).and_return(browser_mock)211 expect(browser_mock).to receive(:save_screenshot).with(screenshot_path)212 saver.save213 expect(saver).to be_screenshot_saved214 end215 end216 describe "with poltergeist driver" do217 before do218 allow(capybara_mock).to receive(:current_driver).and_return(:poltergeist)219 end220 it 'saves driver render with :full => true' do221 expect(driver_mock).to receive(:render).with(screenshot_path, {:full => true})222 saver.save223 expect(saver).to be_screenshot_saved224 end225 end226 describe "with poltergeist_billy driver" do227 before do228 allow(capybara_mock).to receive(:current_driver).and_return(:poltergeist_billy)229 end230 it 'saves driver render with :full => true' do231 expect(driver_mock).to receive(:render).with(screenshot_path, {:full => true})232 saver.save233 expect(saver).to be_screenshot_saved234 end235 end236 describe "with cuprite driver" do237 before do238 allow(capybara_mock).to receive(:current_driver).and_return(:cuprite)239 end240 it 'saves driver render with :full => true' do241 expect(driver_mock).to receive(:render).with(screenshot_path, {:full => true})242 saver.save243 expect(saver).to be_screenshot_saved244 end245 end246 describe "with webkit driver" do247 before do248 allow(capybara_mock).to receive(:current_driver).and_return(:webkit)249 end250 context 'has render method' do251 before do252 allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(false)253 end254 it 'saves driver render' do255 expect(driver_mock).to receive(:render).with(screenshot_path)256 saver.save257 expect(saver).to be_screenshot_saved258 end259 end260 context 'has save_screenshot method' do261 let(:webkit_options){ {width: 800, height: 600} }262 before do263 allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(true)264 end265 it 'saves driver render' do266 expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, {})267 saver.save268 expect(saver).to be_screenshot_saved269 end270 it 'passes webkit_options to driver' do271 allow(Capybara::Screenshot).to receive(:webkit_options).and_return( webkit_options )272 expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, webkit_options)273 saver.save274 expect(saver).to be_screenshot_saved275 end276 end277 end278 describe "with webkit debug driver" do279 before do280 allow(capybara_mock).to receive(:current_driver).and_return(:webkit_debug)281 end282 context 'has render method' do283 before do284 allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(false)285 end286 it 'saves driver render' do287 expect(driver_mock).to receive(:render).with(screenshot_path)288 saver.save289 expect(saver).to be_screenshot_saved290 end291 end292 context 'has save_screenshot method' do293 let(:webkit_options){ {width: 800, height: 600} }294 before do295 allow(driver_mock).to receive(:respond_to?).with(:'save_screenshot').and_return(true)296 end297 it 'saves driver render' do298 expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, {})299 saver.save300 expect(saver).to be_screenshot_saved301 end302 it 'passes webkit_options to driver' do303 allow(Capybara::Screenshot).to receive(:webkit_options).and_return( webkit_options )304 expect(driver_mock).to receive(:save_screenshot).with(screenshot_path, webkit_options)305 saver.save306 expect(saver).to be_screenshot_saved307 end308 end309 end310 describe "with unknown driver" do311 before do312 allow(capybara_mock).to receive(:current_driver).and_return(:unknown)313 allow(saver).to receive(:warn).and_return(nil)314 end315 it 'saves driver render' do316 expect(driver_mock).to receive(:render).with(screenshot_path)317 saver.save318 expect(saver).to be_screenshot_saved319 end320 it 'outputs warning about unknown results' do321 # Not pure mock testing322 expect(saver).to receive(:warn).with(/screenshot driver for 'unknown'.*unknown results/).and_return(nil)323 saver.save324 expect(saver).to be_screenshot_saved325 end326 describe "with rack_test driver" do327 before do328 allow(capybara_mock).to receive(:current_driver).and_return(:rack_test)329 end330 it 'indicates that a screenshot could not be saved' do331 saver.save332 expect(saver).to_not be_screenshot_saved333 end334 end335 describe "with mechanize driver" do336 before do337 allow(capybara_mock).to receive(:current_driver).and_return(:mechanize)338 end339 it 'indicates that a screenshot could not be saved' do340 saver.save341 expect(saver).to_not be_screenshot_saved342 end343 end344 end345end...
save
Using AI Code Generation
1 @m.add('a')2 @m.add('b')3 @m.add('c')4 @m.add('d')5 @m.add('e')6 @m.add('f')7 @m.save('test.txt')
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!!