# frozen_string_literal: true require 'watirspec_helper' module Watir describe Browser do describe '#exists?' do it 'returns true if we are at a page' do browser.goto(WatirSpec.url_for('non_control_elements.html')) expect(browser).to exist end it 'returns false if window is closed', except: {browser: :safari, reason: 'Clicking an Element that Closes a Window is returning NoMatchingWindowFoundException'}, exclude: {browser: :ie, reason: 'IE does not like switching windows'} do browser.goto WatirSpec.url_for('window_switching.html') browser.a(id: 'open').click browser.windows.wait_until(size: 2) browser.window(title: 'closeable window').use browser.a(id: 'close').click browser.windows.wait_until(size: 1) expect(browser.exists?).to be false ensure browser.windows.restore! end it 'returns false after Browser#close' do browser.close expect(browser).not_to exist end end describe '#closed?' do it 'returns false if not closed' do expect(browser).not_to be_closed end it 'returns false if window is closed but browser is not', except: {browser: :safari, reason: 'Clicking an Element that Closes a Window is returning NoMatchingWindowFoundException'}, exclude: {browser: :ie, reason: 'IE Mode does not like closed windows'} do browser.goto WatirSpec.url_for('window_switching.html') browser.a(id: 'open').click browser.windows.wait_until(size: 2) browser.window(title: 'closeable window').use browser.a(id: 'close').click browser.windows.wait_until(size: 1) expect(browser).not_to be_closed ensure browser.windows.restore! end it 'returns false after Browser#close' do browser.close expect(browser).to be_closed end end describe '#html' do it 'returns the DOM of the page as an HTML string' do browser.goto(WatirSpec.url_for('right_click.html')) html = browser.html.downcase # varies between browsers expect(html).to match(/^ 1, 'b' => 2}) expect(browser.execute_script('return [1, 2, "3"]')).to match_array([1, 2, '3']) expect(browser.execute_script('return 1.2 + 1.3')).to eq 2.5 expect(browser.execute_script('return 2 + 2')).to eq 4 expect(browser.execute_script('return "hello"')).to eq 'hello' expect(browser.execute_script('return')).to be_nil expect(browser.execute_script('return null')).to be_nil expect(browser.execute_script('return undefined')).to be_nil expect(browser.execute_script('return true')).to be true expect(browser.execute_script('return false')).to be false end it 'works correctly with multi-line strings and special characters' do expect(browser.execute_script("//multiline rocks! var a = 22; // comment on same line /* more comments */ var b = '33'; var c = \"44\"; return a + b + c")).to eq '223344' end it 'wraps elements as Watir objects' do returned = browser.execute_script('return document.body') expect(returned).to be_a(Watir::Body) end it 'wraps elements in an array' do list = browser.execute_script('return [document.body];') expect(list.size).to eq 1 expect(list.first).to be_a(Watir::Body) end it 'wraps elements in a Hash' do hash = browser.execute_script('return {element: document.body};') expect(hash['element']).to be_a(Watir::Body) end it 'wraps elements in a deep object' do hash = browser.execute_script('return {elements: [document.body], body: {element: document.body }}') expect(hash['elements'].first).to be_a(Watir::Body) expect(hash['body']['element']).to be_a(Watir::Body) end end describe '#back and #forward' do it 'goes to the previous page' do browser.goto WatirSpec.url_for('non_control_elements.html') orig_url = browser.url browser.goto WatirSpec.url_for('tables.html') new_url = browser.url expect(orig_url).not_to eq new_url browser.back expect(orig_url).to eq browser.url end it 'goes to the next page' do urls = [] browser.goto WatirSpec.url_for('non_control_elements.html') urls << browser.url browser.goto WatirSpec.url_for('tables.html') urls << browser.url browser.back expect(browser.url).to eq urls.first browser.forward expect(browser.url).to eq urls.last end it 'navigates between several history items' do urls = ['non_control_elements.html', 'tables.html', 'forms_with_input_elements.html', 'definition_lists.html'].map do |page| browser.goto WatirSpec.url_for(page) browser.url end 3.times { browser.back } expect(browser.url).to eq urls.first 2.times { browser.forward } expect(browser.url).to eq urls[2] end end it 'raises UnknownObjectException when trying to access DOM elements on plain/text-page' do browser.goto(WatirSpec.url_for('plain_text')) expect { browser.div(id: 'foo').id }.to raise_unknown_object_exception end it 'raises an error when trying to interact with a closed browser' do browser.goto WatirSpec.url_for 'definition_lists.html' browser.close expect { browser.dl(id: 'experience-list').id }.to raise_error(Watir::Exception::Error, 'browser was closed') end describe '#ready_state' do it "gets the document's readyState property" do allow(browser).to receive(:execute_script) browser.ready_state expect(browser).to have_received(:execute_script).with('return document.readyState') end end describe '#wait' do # The only way to get engage this method is with page load strategy set to "none" # This spec is both mocking out the response and demonstrating the necessary settings for it to be meaningful it 'waits for document ready state to be complete' do @original = WatirSpec.implementation.clone browser.close @opts = WatirSpec.implementation.browser_args.last @opts[:options] = {page_load_strategy: :none} browser = WatirSpec.new_browser start_time = ::Time.now allow(browser).to receive(:ready_state) { ::Time.now < start_time + 3 ? 'loading' : 'complete' } expect(browser.ready_state).to eq 'loading' browser.wait(20) expect(::Time.now - start_time).to be > 3 expect(browser.ready_state).to eq 'complete' browser.close ensure WatirSpec.implementation = @original.clone end end describe '#inspect' do it 'works even if browser is closed' do allow(browser).to receive(:url).and_raise(Errno::ECONNREFUSED) expect { browser.inspect }.not_to raise_error expect(browser).to have_received(:url).once end end describe '#screenshot' do it 'returns an instance of of Watir::Screenshot' do expect(browser.screenshot).to be_a(Watir::Screenshot) end end end end