Class: WebPilot
- Inherits:
-
Object
- Object
- WebPilot
- Extended by:
- Forwardable
- Includes:
- Log4r
- Defined in:
- lib/webpilot.rb,
lib/webpilot/version.rb
Constant Summary collapse
- Version =
Gem version
"0.1.2"
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#close_blank_windows ⇒ Object
Close all windows that have a current url of “about:blank”.
-
#enlargen(method, locator) ⇒ Object
Enlargens the text of an element, using ‘method` and `locator`, by changing the `font-size` in the style to be `3em`.
- #find_element(method, selector, options = {}) ⇒ Object
- #highlight(method, locator, ancestors = 0) ⇒ Object
-
#initialize(options = {}) ⇒ WebPilot
constructor
A new instance of WebPilot.
- #pause(time = nil) ⇒ Object
- #quit ⇒ Object
-
#safe_deselect_all(el) ⇒ Object
Deselect all ‘<option>s` within a `<select>`, suppressing any `UnsupportedOperationError` that Selenium may throw.
-
#select_frame(id) ⇒ Object
Select a frame by its ‘id`.
-
#wait_for(method, locator) ⇒ Object
Create and execute a ‘Selenium::WebDriver::Wait` for finding an element by `method` and `selector`.
Constructor Details
#initialize(options = {}) ⇒ WebPilot
Returns a new instance of WebPilot.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/webpilot.rb', line 17 def initialize(={}) @browser = [:browser] || :firefox @is_headless = [:is_headless] @pause_time = [:pause_time] || 0.3 @screenshot_dir = [:screenshot_dir] @timeout = [:timeout] || 8 case when [:logger].nil? @logger = Logger.new 'debug_log' null_outputter = IOOutputter.new 'null', File.open('/dev/null', 'w') when [:logger].is_a?(Logger) @logger = [:logger] when [:logger] == 'STDOUT' @logger = Logger.new 'debug_log' @logger.outputters = Outputter.stdout @logger.level = DEBUG when [:logger] == 'STDERR' @logger = Logger.new 'debug_log' @logger.outputters = Outputter.stderr @logger.level = DEBUG end if @is_headless @headless = Headless.new @headless.start end @driver = Selenium::WebDriver.for @browser end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
10 11 12 |
# File 'lib/webpilot.rb', line 10 def driver @driver end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/webpilot.rb', line 10 def logger @logger end |
Instance Method Details
#close_blank_windows ⇒ Object
Close all windows that have a current url of “about:blank”. Must follow this with a call to ‘#switch_to`, so that you know what window you’re on.
50 51 52 53 54 55 |
# File 'lib/webpilot.rb', line 50 def close_blank_windows @driver.window_handles.each do |handle| @driver.switch_to.window(handle) @driver.close if @driver.current_url == 'about:blank' end end |
#enlargen(method, locator) ⇒ Object
Enlargens the text of an element, using ‘method` and `locator`, by changing the `font-size` in the style to be `3em`. It uses the following Javascript:
hlt = function(c) { c.style.fontSize='3em'; }; return hlt(arguments[0]);
61 62 63 64 65 66 67 |
# File 'lib/webpilot.rb', line 61 def enlargen(method, locator) @logger.debug " enlargen: Waiting up to #{@timeout} seconds to find_element(#{method}, #{locator})..." wait = Selenium::WebDriver::Wait.new(:timeout => @timeout) wait.until { find_element(method, locator) } element = find_element(method, locator) execute_script("hlt = function(c) { c.style.fontSize='3em'; }; return hlt(arguments[0]);", element) end |
#find_element(method, selector, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/webpilot.rb', line 69 def find_element(method, selector, ={}) retries = 4 sleep 0.1 # based on http://groups.google.com/group/ruby-capybara/browse_thread/thread/5e182835a8293def fixes "NS_ERROR_ILLEGAL_VALUE" begin @driver.find_element(method, selector) rescue Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::TimeOutError => e return nil if [:no_raise] raise e rescue Selenium::WebDriver::Error::InvalidSelectorError => e raise e if retries == 0 @logger.info "Caught a Selenium::WebDriver::Error::InvalidSelectorError: #{e}" @logger.info "Retrying..." pause 2 retries -= 1 retry end end |
#highlight(method, locator, ancestors = 0) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/webpilot.rb', line 87 def highlight(method, locator, ancestors=0) wait = Selenium::WebDriver::Wait.new(:timeout => @timeout) wait.until { find_element(method, locator) } element = find_element(method, locator) execute_script("hlt = function(c) { c.style.border='solid 1px rgb(255, 16, 16)'; }; return hlt(arguments[0]);", element) parents = "" red = 255 ancestors.times do parents << ".parentNode" red -= (12*8 / ancestors) execute_script("hlt = function(c) { c#{parents}.style.border='solid 1px rgb(#{red}, 0, 0)'; }; return hlt(arguments[0]);", element) end pause end |
#pause(time = nil) ⇒ Object
103 104 105 106 |
# File 'lib/webpilot.rb', line 103 def pause(time = nil) @logger.debug " breathing..." sleep (time || @pause_time) end |
#quit ⇒ Object
108 109 110 111 |
# File 'lib/webpilot.rb', line 108 def quit @driver.quit @headless.destroy if @is_headless end |
#safe_deselect_all(el) ⇒ Object
Deselect all ‘<option>s` within a `<select>`, suppressing any `UnsupportedOperationError` that Selenium may throw
115 116 117 118 |
# File 'lib/webpilot.rb', line 115 def safe_deselect_all(el) el.deselect_all rescue Selenium::WebDriver::Error::UnsupportedOperationError end |
#select_frame(id) ⇒ Object
Select a frame by its ‘id`
121 122 123 124 |
# File 'lib/webpilot.rb', line 121 def select_frame(id) @driver.switch_to().frame(id) pause end |
#wait_for(method, locator) ⇒ Object
Create and execute a ‘Selenium::WebDriver::Wait` for finding an element by `method` and `selector`
127 128 129 130 131 132 |
# File 'lib/webpilot.rb', line 127 def wait_for(method, locator) @logger.debug " wait_for: Waiting up to #{@timeout} seconds to find_element(#{method}, #{locator})..." wait = Selenium::WebDriver::Wait.new(:timeout => @timeout) sleep 0.1 # based on http://groups.google.com/group/ruby-capybara/browse_thread/thread/5e182835a8293def fixes "NS_ERROR_ILLEGAL_VALUE" wait.until { driver.find_element(method, locator) } end |