lib/kirchhoff.rb in kirchhoff-0.0.1 vs lib/kirchhoff.rb in kirchhoff-0.0.2
- old
+ new
@@ -4,11 +4,11 @@
require 'kirchhoff/common_interface'
require 'kirchhoff/logger'
module Kirchhoff
class Driver
- attr_accessor :__driver__
+ attr_accessor :__driver__, :default_timeout
attr_reader :log_dir_path
include Kirchhoff::CommonInterface
def find_element selector
@__driver__.find_element selector
@@ -20,16 +20,20 @@
def current_url
@__driver__.current_url
end
- def initialize(url=nil, browser=:chrome)
- @__driver__ = Selenium::WebDriver.for(browser)
- go(url) if url
+ def quit
+ @__driver__.quit
end
- def go(url, &block)
+ def initialize(browser: :chrome, default_timeout: 6)
+ @__driver__ = Selenium::WebDriver.for(browser)
+ @default_timeout = default_timeout
+ end
+
+ def go url
@__driver__.navigate.to(url)
Kirchhoff::Logger.call :info, "visiting #{url}..."
end
def reload
@@ -55,17 +59,29 @@
def switch_window num
@__driver__.switch_to.window @__driver__.window_handles[num]
end
- def wait_element selector, t=10
- wait = Selenium::WebDriver::Wait.new(timeout: t)
+ def wait_element(selector, maybe: true, t: nil)
+ wait = Selenium::WebDriver::Wait.new(timeout: (t || @default_timeout))
wait.until { self.find_element(css: selector) }
+ rescue Selenium::WebDriver::Error::TimeOutError
+ unless maybe
+ raise Selenium::WebDriver::Error::TimeOutError, "selector: #{selector}"
+ end
end
- def wait_text text, t=10
- wait = Selenium::WebDriver::Wait.new(timeout: t)
+ def wait_text(text, maybe: true, t: nil)
+ wait = Selenium::WebDriver::Wait.new(timeout: (t || @default_timeout))
wait.until { self.find_element(xpath: "//*[text()[contains(.,\"#{text}\")]]") }
+ rescue Selenium::WebDriver::Error::TimeOutError
+ unless maybe
+ raise Selenium::WebDriver::Error::TimeOutError, "text: #{text}"
+ end
+ end
+
+ def to_html
+ self.__driver__.page_source
end
end
end
class Selenium::WebDriver::Element