lib/smart_driver.rb in smart_driver-1.1.2 vs lib/smart_driver.rb in smart_driver-1.2.0

- old
+ new

@@ -10,54 +10,28 @@ def initialize(url=nil, browser=:chrome) @__driver__ = Selenium::WebDriver.for(browser) go(url) if url end - def find(selector) - logging :info, "find #{selector}..." - @__driver__.find_element(css: selector) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "#{selector} cannot be found" - nil - end - - def finds(selector) - logging :info, "finds #{selector}..." - @__driver__.find_elements(css: selector) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "#{selector} cannot be found" - nil - end - - def find_text(text) - logging :info, "find text '#{text}'..." - @__driver__.find_element({xpath: "//*[text()[contains(.,\"#{text}\")]]"}) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "text '#{text}' cannot be found" - nil - end - - def finds_text(text) - logging :info, "finds text '#{text}'..." - @__driver__.find_elements({xpath: "//*[text()[contains(.,\"#{text}\")]]"}) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "text #{text} cannot be found" - nil - end - def go(url) logging :info, "visiting #{url}..." @__driver__.navigate.to(url) end def reload @__driver__.navigate.refresh end - def submit + def submit(n=20) logging :info, "submit form ..." - $focus.submit if $focus + $focus.submit + if block_given? + n.times do + break if yield() + sleep 0.5 + end + end end def exec_js(js_code) @__driver__.execute_script js_code end @@ -75,44 +49,29 @@ end def switch_window(num) @__driver__.switch_to.window @__driver__.window_handles[num] end -end -class Selenium::WebDriver::Element - include SmartDriver::CommonInterface - - def find(selector) - logging :info, "find #{selector} in element..." - self.find_element(css: selector) + def maybe(&block) + block.call() rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "#{selector} cannot be found in element..." nil end +end - def finds(selector) - logging :info, "finds #{selector} in element..." - self.find_elements(css: selector) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "#{selector} cannot be found in element..." - nil - end +class Selenium::WebDriver::Element + include SmartDriver::CommonInterface + alias :origin_click :click - def find_text(text) - logging :info, "find text '#{text}'..." - self.find_element({xpath: "//*[text()[contains(.,\"#{text}\")]]"}) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "text '#{text}' cannot be found" - nil - end - - def finds_text(text) - logging :info, "finds text '#{text}'..." - self.find_elements({xpath: "//*[text()[contains(.,\"#{text}\")]]"}) - rescue Selenium::WebDriver::Error::NoSuchElementError - logging :fail, "text #{text} cannot be found" - nil + def click(n=20) + origin_click() + if block_given? + n.times do + break if yield() + sleep 0.5 + end + end end def fill(text) $focus = self logging :info, "fill '#{text}'"