require "watirspec_helper"

describe 'Alert API' do
  not_compliant_on :safari do
    before do
      browser.goto WatirSpec.url_for("alerts.html")
    end

    after do
      browser.alert.ok if browser.alert.exists?
    end

    context 'alert' do
      describe '#text' do
        it 'returns text of alert' do
          browser.button(id: 'alert').click
          expect(browser.alert.text).to include('ok')
        end
      end

      describe '#exists?' do
        it 'returns false if alert is not present' do
          expect(browser.alert).to_not exist
        end

        it 'returns true if alert is present' do
          browser.button(id: 'alert').click
          browser.wait_until(timeout: 10) { browser.alert.exists? }
        end
      end

      describe '#ok' do
        it 'closes alert' do
          browser.button(id: 'alert').click
          browser.alert.ok
          expect(browser.alert).to_not exist
        end
      end

      bug "https://code.google.com/p/chromedriver/issues/detail?id=26", [:chrome, :macosx] do
        describe '#close' do
          it 'closes alert' do
            browser.button(id: 'alert').click
            browser.alert.close
            expect(browser.alert).to_not exist
          end
        end
      end

      not_compliant_on :relaxed_locate do
        describe 'wait_until_present' do
          it 'waits until alert is present and goes on' do
            browser.button(id: 'timeout-alert').click
            browser.alert.wait_until_present.ok

            expect(browser.alert).to_not exist
          end

          it 'raises error if alert is not present after timeout' do
            begin
              Watir.default_timeout = 2
              expect {
                browser.alert.wait_until_present.ok
              }.to raise_error(Watir::Wait::TimeoutError)
            ensure
              Watir.default_timeout = 30
            end
          end
        end
      end
    end

    context 'confirm' do
      describe '#ok' do
        it 'accepts confirm' do
          browser.button(id: 'confirm').click
          browser.alert.ok
          expect(browser.button(id: 'confirm').value).to eq "true"
        end
      end

      describe '#close' do
        it 'cancels confirm' do
          browser.button(id: 'confirm').click
          browser.alert.close
          expect(browser.button(id: 'confirm').value).to eq "false"
        end
      end
    end

    context 'prompt' do
      describe '#set' do
        bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1255906", :firefox do
          it 'enters text to prompt' do
            browser.button(id: 'prompt').click
            browser.alert.set 'My Name'
            browser.alert.ok
            expect(browser.button(id: 'prompt').value).to eq 'My Name'
          end
        end
      end
    end
  end
end