Class: Aranha::Selenium::Session

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/aranha/selenium/session.rb

Defined Under Namespace

Classes: Downloads

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
16
17
18
# File 'lib/aranha/selenium/session.rb', line 10

def initialize(options = {})
  @downloads = Downloads.new
  @wait = ::Selenium::WebDriver::Wait.new(timeout: 15)
  super(
    ::Aranha::Selenium::DriverFactory.create_driver(
      options.merge(download_dir: @downloads.dir)
    )
  )
end

Instance Attribute Details

#downloadsObject (readonly)

Returns the value of attribute downloads.



8
9
10
# File 'lib/aranha/selenium/session.rb', line 8

def downloads
  @downloads
end

#waitObject (readonly)

Returns the value of attribute wait.



8
9
10
# File 'lib/aranha/selenium/session.rb', line 8

def wait
  @wait
end

Instance Method Details

#current_sourceObject



47
48
49
50
51
52
53
# File 'lib/aranha/selenium/session.rb', line 47

def current_source
  element = find_element(xpath: '/html[1]')
  raise 'Root element not found' unless element

  s = element.attribute('innerHTML')
  "<html>\n#{s}\n</html>\n"
end

#find_or_not_element(find_element_args) ⇒ Object



20
21
22
23
# File 'lib/aranha/selenium/session.rb', line 20

def find_or_not_element(find_element_args)
  r = find_elements(find_element_args)
  r.any? ? r.first : nil
end

#wait_for_click(find_element_args) ⇒ Object



25
26
27
28
29
30
# File 'lib/aranha/selenium/session.rb', line 25

def wait_for_click(find_element_args)
  wait.until do
    element = find_element(find_element_args)
    element ? element_click(element) : nil
  end
end

#wait_for_downloadObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/aranha/selenium/session.rb', line 36

def wait_for_download
  initial_downloads = downloads.current
  yield
  new_downloads = []
  wait.until do
    new_downloads = downloads.current - initial_downloads
    new_downloads.any?
  end
  new_downloads.first
end

#wait_for_element(find_element_args) ⇒ Object



32
33
34
# File 'lib/aranha/selenium/session.rb', line 32

def wait_for_element(find_element_args)
  wait.until { find_element(find_element_args) }
end