Sha256: 57283e6d60ca5e5552ab86b94cea6c648fe5839026d6435bdd48c73a5575c627

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

module Webdrone
  class Browser
    def form
      @form ||= Form.new self
    end
  end

  class Form
    attr_accessor :a0

    def initialize(a0)
      @a0 = a0
    end

    def with_xpath(xpath, &block)
      @xpath = xpath
      instance_eval &block
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def set(key, val)
      item = self.find_item(key)
      if item.tag_name == 'select'
        option = item.find_element :xpath, XPath::HTML.option(val).to_s
        option.click
      else
        item.clear
        item.send_keys(val)
      end
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def get(key)
      self.find_item(key)[:value]
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def clic(key)
      self.find_item(key).click
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def mark(key, color: 'red')
      @a0.mark.flash self.find_item(key), color: color
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def submit(key = nil)
      self.find_item(key) if key
      @lastitem.submit
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    def xlsx(sheet: nil, filename: nil)
      @a0.xlsx.dict(sheet: sheet, filename: filename).each do |k, v|
        self.set k, v
      end
    rescue => exception
      Webdrone.report_error(@a0, exception, Kernel.caller_locations)
    end

    protected
      def find_item(key)
        if @xpath
          @lastitem = @a0.driver.find_element :xpath, sprintf(@xpath, key)
        else
          @lastitem = @a0.find.xpath XPath::HTML.field(key).to_s
        end
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
webdrone-0.9.8 lib/webdrone/form.rb
webdrone-0.9.2 lib/webdrone/form.rb
webdrone-0.9.0 lib/webdrone/form.rb
webdrone-0.8.4 lib/webdrone/form.rb
webdrone-0.8.2 lib/webdrone/form.rb
webdrone-0.8.0 lib/webdrone/form.rb