Class: SeleniumStandaloneDsl::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium_standalone_dsl/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.

Parameters:

  • Hash

    config config =

    log_path: '/path/to/logs',
    user_agent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36',
    



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

#driverObject (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

Returns:

  • (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


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_okObject



59
60
61
# File 'lib/selenium_standalone_dsl/base.rb', line 59

def prompt_ok
  @driver.switch_to.alert.accept
end

#quitObject



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

Returns:

  • Array of [Selenium::WebDriver::Element]



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