# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Dom actions' do
  let(:browser) { Chromate::Browser.new(browser_args) }

  before(:each) do
    browser.start
    @url = server_urls['dom_actions']
    browser.navigate_to(@url)
  end

  after(:each) do
    browser.stop
  end

  it 'clicks a button' do
    button = browser.find_element('#click-button')
    button.click

    result = browser.find_element('#click-result')
    expect(result.text).to eq('Button clicked!')
  end

  it 'hovers over an element' do
    hover_box = browser.find_element('#hover-box')

    hover_box.hover

    expect(hover_box.attributes['class']).to include('hover-highlight')
  end

  it 'types text into an input' do
    input = browser.find_element('#input-text')
    input.type('Testing Chromate')

    result = browser.find_element('#input-result')
    expect(result.text).to eq('No input yet')

    input.press_enter
    expect(result.text).to eq('Input submitted: Testing Chromate')
  end

  it 'presses the Enter key' do
    input = browser.find_element('#input-text')
    input.type('Pressing Enter Test')
    input.press_enter

    result = browser.find_element('#input-result')
    expect(result.text).to eq('Input submitted: Pressing Enter Test')
  end

  it 'selects an option from a dropdown' do
    browser.find_element('#test-select').select_option('option2')

    result = browser.find_element('#select-result')
    expect(result.text).to eq('Selected option: option2')
  end

  it 'evaluates a JavaScript expression' do
    result = browser.evaluate_script('document.title')
    expect(result).to eq('Chromate Actions Test Page')
  end

  it 'gets the source of the page' do
    source = browser.source
    expect(source).to include('<title>Chromate Actions Test Page</title>')
  end

  it 'captures a screenshot' do
    screenshot = browser.screenshot
    expect(screenshot).to match(hash_including(path: kind_of(String), base64: kind_of(String)))
  end

  it 'captures a screenshot of the full page' do
    screenshot = browser.screenshot(nil, full_page: true)
    expect(screenshot).to match(hash_including(path: kind_of(String), base64: kind_of(String)))
  end
end
