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?(method, selector) ⇒ Boolean
- #iframe(method, selector) ⇒ Object
-
#initialize(config) ⇒ 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(config) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/selenium_standalone_dsl/base.rb', line 10 def initialize(config) # Initialize instance variables @config = config @logger = Logger.new(@config[:log_path]) # 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'] = @config[:user_agent] profile.native_events = true if @config[:headless] headless = Headless.new(reuse: false, destroy_at_exit: true) headless.start end @driver = Selenium::WebDriver::Driver.for(:firefox, :http_client => client, :profile => profile) @logger.info '=' * 150 @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.
37 38 39 40 41 42 43 |
# File 'lib/selenium_standalone_dsl/base.rb', line 37 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
51 52 53 54 55 56 57 |
# File 'lib/selenium_standalone_dsl/base.rb', line 51 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?(method, selector) ⇒ Boolean
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/selenium_standalone_dsl/base.rb', line 74 def has_element?(method, selector) if method == :text @driver.page_source.match(selector) != nil else begin @driver.find_element(method, selector).is_a? Selenium::WebDriver::Element rescue Selenium::WebDriver::Error::NoSuchElementError false end end end |
#iframe(method, selector) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/selenium_standalone_dsl/base.rb', line 92 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
45 46 47 48 49 |
# File 'lib/selenium_standalone_dsl/base.rb', line 45 def popup @driver.switch_to.window @driver.window_handles.last yield @driver.switch_to.window @driver.window_handles.first end |
#prompt_ok ⇒ Object
59 60 61 |
# File 'lib/selenium_standalone_dsl/base.rb', line 59 def prompt_ok @driver.switch_to.alert.accept end |
#quit ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/selenium_standalone_dsl/base.rb', line 117 def quit @driver.quit if @config[:headless] headless = Headless.new(reuse: false, destroy_at_exit: true) headless.start end end |
#search(selector) ⇒ Object
Provide jQuery-like search using Nokogiri::HTML
88 89 90 |
# File 'lib/selenium_standalone_dsl/base.rb', line 88 def search(selector) Nokogiri::HTML.parse(@driver.page_source).search selector end |
#select(text, from: nil, find_by: :name) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/selenium_standalone_dsl/base.rb', line 63 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
113 114 115 |
# File 'lib/selenium_standalone_dsl/base.rb', line 113 def visit(url) @driver.get url end |
#with_frame(&block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/selenium_standalone_dsl/base.rb', line 100 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 |