Class: SeleniumStandaloneDSL::Base
- Inherits:
-
Object
- Object
- SeleniumStandaloneDSL::Base
- Defined in:
- lib/selenium_standalone_dsl/base.rb
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
-
#click(selector, find_by: :link_text) ⇒ Object
The following methods are utility methods for SeleniumStandaloneDsl-DSL.
- #fill_in(selector, find_by: :name, with: nil) ⇒ Object
- #has_element?(selector, find_by: :link_text) ⇒ Boolean
- #iframe(method, selector) ⇒ Object
-
#initialize(log_path: '/tmp/selenium_standalone_dsl', user_agent: 'Selenium Standalone DSL', headless: false) ⇒ Base
constructor
A new instance of Base.
- #popup ⇒ Object
- #prompt_ok ⇒ Object
- #quit ⇒ Object
-
#search(selector) ⇒ Object
Provide jQuery-like search using Nokogiri::HTML.
- #select(text, from: nil, find_by: :name) ⇒ Object
- #visit(url) ⇒ Object
- #with_frame(&block) ⇒ Object
Constructor Details
#initialize(log_path: '/tmp/selenium_standalone_dsl', user_agent: 'Selenium Standalone DSL', headless: false) ⇒ Base
Returns a new instance of Base.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/selenium_standalone_dsl/base.rb', line 5 def initialize(log_path: '/tmp/selenium_standalone_dsl', user_agent: 'Selenium Standalone DSL', headless: false) if headless @headless = Headless.new(reuse: false, destroy_at_exit: true) @headless.start end # Extends timeout client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 120 # Create profile so that each driver instance has its own cookie profile = Selenium::WebDriver::Firefox::Profile.new profile['general.useragent.override'] = user_agent profile.native_events = true @driver = Selenium::WebDriver::Driver.for(:firefox, :http_client => client, :profile => profile) @logger = Logger.new(log_path) @logger.info '=' * 100 @logger.info 'SeleniumStandaloneDsl started' end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
3 4 5 |
# File 'lib/selenium_standalone_dsl/base.rb', line 3 def driver @driver end |
Instance Method Details
#click(selector, find_by: :link_text) ⇒ Object
The following methods are utility methods for SeleniumStandaloneDsl-DSL. You can easily handle driver with this DSL.
31 32 33 34 35 36 37 |
# File 'lib/selenium_standalone_dsl/base.rb', line 31 def click(selector, find_by: :link_text) sleep Random.new.rand(1..2) with_frame do @driver.find_element(find_by, selector).click end sleep Random.new.rand(1..2) end |
#fill_in(selector, find_by: :name, with: nil) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/selenium_standalone_dsl/base.rb', line 45 def fill_in(selector, find_by: :name, with: nil) return if !with element = @driver.find_element(find_by, selector) 0.upto(100) { element.send_keys "\b" } element.send_keys with end |
#has_element?(selector, find_by: :link_text) ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/selenium_standalone_dsl/base.rb', line 68 def has_element?(selector, find_by: :link_text) if [:text, :link_text].include?(find_by) @driver.page_source.match(selector) != nil else begin @driver.find_element(find_by, selector).is_a? Selenium::WebDriver::Element rescue Selenium::WebDriver::Error::NoSuchElementError false end end end |
#iframe(method, selector) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/selenium_standalone_dsl/base.rb', line 86 def iframe(method, selector) current_frame = @driver.window_handles.first @driver.switch_to.frame @driver.find_element(method, selector) yield # Frame might has gone away @driver.switch_to.frame current_frame rescue Selenium::WebDriver::Error::NoSuchFrameError end |
#popup ⇒ Object
39 40 41 42 43 |
# File 'lib/selenium_standalone_dsl/base.rb', line 39 def popup @driver.switch_to.window @driver.window_handles.last yield @driver.switch_to.window @driver.window_handles.first end |
#prompt_ok ⇒ Object
53 54 55 |
# File 'lib/selenium_standalone_dsl/base.rb', line 53 def prompt_ok @driver.switch_to.alert.accept end |
#quit ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/selenium_standalone_dsl/base.rb', line 111 def quit @driver.quit if @headless @headless.destroy end end |
#search(selector) ⇒ Object
Provide jQuery-like search using Nokogiri::HTML
82 83 84 |
# File 'lib/selenium_standalone_dsl/base.rb', line 82 def search(selector) Nokogiri::HTML.parse(@driver.page_source).search selector end |
#select(text, from: nil, find_by: :name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/selenium_standalone_dsl/base.rb', line 57 def select(text, from: nil, find_by: :name) return if !from # For legacy sites using Frame element = with_frame do @driver.find_element(find_by, from) end select = Selenium::WebDriver::Support::Select.new(element) select.select_by :text, text end |
#visit(url) ⇒ Object
107 108 109 |
# File 'lib/selenium_standalone_dsl/base.rb', line 107 def visit(url) @driver.get url end |
#with_frame(&block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/selenium_standalone_dsl/base.rb', line 94 def with_frame(&block) begin block.call rescue Selenium::WebDriver::Error::NoSuchElementError begin @driver.switch_to.frame 1 rescue Selenium::WebDriver::Error::NoSuchFrameError raise Selenium::WebDriver::Error::NoSuchElementError end block.call end end |