Sha256: 2ab9b1ecfe635657c26f7a18a627688da423f3e8a514ce589c35a860f0baf06c

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

# encoding: utf-8
module Watir
  class Frame < HTMLElement

    VALID_LOCATORS = [:id, :name, :index]

    def initialize(*args)
      super
      @frame_id = nil
    end

    def locate
      @parent.assert_exists

      if @iframe
        return @iframe
      elsif @frame_id.nil?
        locate_iframe || locate_frame
      else
        switch!
        driver
      end
    end

    def assert_exists
      @parent.assert_exists
      # we always run locate(), to make sure the frame is switched
      @element = locate
    end

    def execute_script(*args)
      browser.execute_script(*args)
    end

    def element_by_xpath(*args)
      assert_exists
      super
    end

    def elements_by_xpath(*args)
      assert_exists
      super
    end

    private

    def locate_iframe
      # hack - frame doesn't have IFrame's attributes either
      @iframe = @element = IFrame.new(@parent, @selector).locate
    end

    def locate_frame
      loc = VALID_LOCATORS.find { |loc| @selector[loc] }

      unless loc
        raise MissingWayOfFindingObjectException, "can only switch frames by #{VALID_LOCATORS.inspect}"
      end

      @frame_id = @selector[loc]

      unless [String, Integer].any? { |e| @frame_id.kind_of?(e) }
        raise TypeError, "can't locate frame using #{@frame_id.inspect}:#{@frame_id.class}"
      end

      switch!

      driver
    end

    def switch!
      driver.switch_to.frame @frame_id
    rescue Selenium::WebDriver::Error::NoSuchFrameError => e
      raise Exception::UnknownFrameException, e.message
    end

  end # Frame
end # Watir

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
watir-webdriver-0.0.4 lib/watir-webdriver/elements/frame.rb
watir-webdriver-0.0.3 lib/watir-webdriver/elements/frame.rb
watir-webdriver-0.0.2 lib/watir-webdriver/elements/frame.rb
watir-webdriver-0.0.1 lib/watir-webdriver/elements/frame.rb
watir-webdriver-0.0.1.dev7 lib/watir-webdriver/elements/frame.rb