Sha256: b443b885f1f864ecb30bfa7b6fea9b10f219f7b41ea7aed9e9d9ef2984e2d419

Contents?: true

Size: 1.79 KB

Versions: 10

Compression:

Stored size: 1.79 KB

Contents

module Watir
  module WindowSwitching

    class NoMatchingWindowFoundException < StandardError
    end

    def windows(*args)
      all = @driver.window_handles.map { |id| Window.new(@driver, id) }

      if args.empty?
        all
      else
        filter_windows(args, all, :select)
      end
    end

    def window(*args, &blk)
      win = filter_windows(args, windows, :find)

      if win && block_given?
        win.use(&blk)
      end

      win or raise NoMatchingWindowFoundException, args.inspect
    end

    private

    def filter_windows(args, all, method)
      sel = extract_selector(args)

      if sel.empty?
        all.find { |w| w.current? }
      end

      unless sel.keys.all? { |k| [:title, :url].include? k }
        raise ArgumentError, "invalid window selector: #{sel.inspect}"
      end

      all.send(method) do |win|
        sel.all? { |key, value| value === win.send(key) }
      end
    end
  end # WindowSwitching

  class Window
    def initialize(driver, id)
      @driver, @id = driver, id
    end

    def inspect
      '#<%s:0x%x id=%s>' % [self.class, hash*2, @id.to_s]
    end

    def ==(other)
      return false unless other.kind_of?(self.class)

      @id == other.id
    end
    alias_method :eql?, :==

    def hash
      @id.hash ^ self.class.hash
    end

    def current?
      @driver.window_handle == @id
    end

    def close
      use { @driver.close }
    end

    def title
      title = nil
      use { title = @driver.title }

      title
    end

    def url
      url = nil
      use { url = @driver.current_url }

      url
    end

    def use(&blk)
      if current?
        yield if block_given?
        return self
      end

      @driver.switch_to.window(@id, &blk)
      self
    end

    protected

    def id
      @id
    end

  end # Window
end # Watir

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
watir-webdriver-0.1.8 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.7 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.6 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.5 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.4 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.3 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.2 lib/watir-webdriver/window_switching.rb
ayanko-watir-webdriver-0.1.1.1 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.1 lib/watir-webdriver/window_switching.rb
watir-webdriver-0.1.0 lib/watir-webdriver/window_switching.rb