Class: Rundoc::CodeCommand::Website::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rundoc/code_command/website/driver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, url:, width: 1024, height: 720, visible: false) ⇒ Driver

Returns a new instance of Driver.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rundoc/code_command/website/driver.rb', line 9

def initialize(name: , url: , width: 1024, height: 720, visible: false)
  browser_options = ::Selenium::WebDriver::Chrome::Options.new
  browser_options.args << '--headless' unless visible
  browser_options.args << '--disable-gpu' if Gem.win_platform?
  browser_options.args << '--hide-scrollbars'
  # browser_options.args << "--window-size=#{width},#{height}"
  @width = width
  @height = height

  @driver = Capybara::Selenium::Driver.new(nil, browser: :chrome, options: browser_options)
  driver_name = "rundoc_driver_#{name}".to_sym
  Capybara.register_driver(driver_name) do |app|
    @driver
  end

  @session = Capybara::Session.new(driver_name)
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



7
8
9
# File 'lib/rundoc/code_command/website/driver.rb', line 7

def session
  @session
end

Class Method Details

.add(name, value) ⇒ Object



95
96
97
98
# File 'lib/rundoc/code_command/website/driver.rb', line 95

def self.add(name, value)
  raise "Task named #{name.inspect} is already started, choose a different name" if @tasks[name]
  @tasks[name] = value
end

.find(name) ⇒ Object



100
101
102
103
# File 'lib/rundoc/code_command/website/driver.rb', line 100

def self.find(name)
  raise "Could not find task with name #{name.inspect}, known task names: #{@tasks.keys.inspect}" unless @tasks[name]
  @tasks[name]
end

.next_screenshot_nameObject



105
106
107
108
109
# File 'lib/rundoc/code_command/website/driver.rb', line 105

def self.next_screenshot_name
  @count ||= 0
  @count += 1
  return "screenshot_#{@count}.png"
end

.tasksObject



47
48
49
# File 'lib/rundoc/code_command/website/driver.rb', line 47

def self.tasks
  @tasks
end

Instance Method Details

#current_urlObject



35
36
37
# File 'lib/rundoc/code_command/website/driver.rb', line 35

def current_url
  session.current_url
end

#safe_eval(code) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rundoc/code_command/website/driver.rb', line 51

def safe_eval(code)
  @driver.send(:eval, code)
rescue => e
  msg = String.new
  msg << "Error running code #{code.inspect} at #{current_url.inspect}\n"
  msg << "saving a screenshot to: `tmp/error.png`"
  puts msg
  session.save_screenshot("tmp/error.png")
  raise e
end

#screenshot(upload: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rundoc/code_command/website/driver.rb', line 62

def screenshot(upload: false)
  @driver.resize_window_to(@driver.current_window_handle, @width, @height)
  FileUtils.mkdir_p("tmp/rundoc_screenshots")
  file_name = self.class.next_screenshot_name
  file_path = "tmp/rundoc_screenshots/#{file_name}"
  session.save_screenshot(file_path)

  return file_path unless upload

  case upload
  when 's3', 'aws'
    puts "Uploading screenshot to S3"
    require 'aws-sdk-s3'
    ENV.fetch('AWS_ACCESS_KEY_ID')
    s3 = Aws::S3::Resource.new(region: ENV.fetch('AWS_REGION'))

    key = "#{timestamp}/#{file_name}"
    obj = s3.bucket(ENV.fetch('AWS_BUCKET_NAME')).object(key)
    obj.upload_file(file_path)

    obj.client.put_object_acl(
      acl: 'public-read' ,
      bucket: ENV.fetch('AWS_BUCKET_NAME'),
      key: key
    )

    obj.public_url
  else
    raise "Upload #{upload.inspect} is not valid, use 's3' instead"
  end
end

#scroll(value = 100) ⇒ Object



39
40
41
# File 'lib/rundoc/code_command/website/driver.rb', line 39

def scroll(value = 100)
  session.execute_script "window.scrollBy(0,#{value})"
end

#teardownObject



43
44
45
# File 'lib/rundoc/code_command/website/driver.rb', line 43

def teardown
  session.reset_session!
end

#timestampObject



31
32
33
# File 'lib/rundoc/code_command/website/driver.rb', line 31

def timestamp
  Time.now.utc.strftime("%Y%m%d%H%M%S%L%N")
end

#visit(url) ⇒ Object



27
28
29
# File 'lib/rundoc/code_command/website/driver.rb', line 27

def visit(url)
  @session.visit(url)
end