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



61
62
63
64
65
66
67
# File 'lib/aranha/selenium/session.rb', line 61

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_element(*args, &block) ⇒ Selenium::WebDriver::Element?

Returns:

  • (Selenium::WebDriver::Element, nil)


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

def find_element(*args, &block)
  return args.first if args.count >= 1 && args.first.is_a?(::Selenium::WebDriver::Element)

  __getobj__.find_element(*args, &block)
end

#find_or_not_element(find_element_args) ⇒ Object



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

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



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

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/aranha/selenium/session.rb', line 43

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



39
40
41
# File 'lib/aranha/selenium/session.rb', line 39

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

#wait_for_url_change(&block) ⇒ Object



54
55
56
57
58
59
# File 'lib/aranha/selenium/session.rb', line 54

def wait_for_url_change(&block)
  previous_url = current_url
  block&.call

  wait.until { current_url != previous_url }
end