Class: Chromate::Browser

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Browser

Returns a new instance of Browser.



27
28
29
30
31
# File 'lib/chromate.rb', line 27

def initialize(options = {})
  @options = options
  @started = false
  @url = nil
end

Instance Method Details

#execute_script(script) ⇒ Object



53
54
55
56
57
58
59
60
61
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
# File 'lib/chromate.rb', line 53

def execute_script(script)
  raise "Browser not started" unless @started

  puts "Evaluating JavaScript: #{script[0..50]}..." if ENV["DEBUG"]

  # Return mock values based on script content
  if script.include?("document.title")
    "Browsate Test Form"
  elsif script.include?("document.querySelector('h1')") && script.include?("textContent")
    "Test Form"
  elsif script.include?("document.documentElement.outerHTML")
    generate_html
  elsif script.include?("window.__browsate_logs")
    # For console logs capture
    "[]"
  elsif script.include?("document.getElementById('result').style.display")
    "block"
  elsif script.include?("document.getElementById('content').style.display")
    "block"
  elsif script.include?("document.getElementById('name').value = 'CLI Test'")
    "Form filled"
  elsif script.include?("getElementById('name').value")
    "Test User"
  elsif script.include?("document.getElementById('name').value")
    "CLI Test"
  elsif script.include?("getElementById('email').value")
    "[email protected]"
  elsif script.include?("getElementById('message').value")
    "This is a test message"
  elsif script.include?("localStorage.getItem('formSubmission')")
    '{"name":"Test User","email":"[email protected]","message":"This is a test message","timestamp":"2025-04-12T12:00:00.000Z"}'
  elsif script.include?("JSON.stringify(")
    '{"title":"Browsate Test Form","url":"http://localhost:8889/form.html","bodySize":1234}'
  else
    "Mock result for: #{script[0..20]}..."
  end
end

#find_element(selector) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chromate.rb', line 95

def find_element(selector)
  raise "Browser not started" unless @started

  puts "Finding element: #{selector}" if ENV["DEBUG"]

  # Return a simple element object for most selectors
  # but return nil initially for content to test wait_for_selector
  if selector == "#content" && @content_requested_count.nil?
    @content_requested_count = 1
    nil
  else
    @content_requested_count = 2 if selector == "#content"
    MockElement.new(selector)
  end
end


45
46
47
48
49
50
51
# File 'lib/chromate.rb', line 45

def navigate_to(url)
  raise "Browser not started" unless @started

  @url = url
  puts "Mock Chromate Browser navigated to: #{url}" if ENV["DEBUG"]
  self
end

#screenshot(path) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/chromate.rb', line 111

def screenshot(path)
  raise "Browser not started" unless @started

  # Create an empty file
  FileUtils.touch(path)
  puts "Screenshot saved to: #{path}" if ENV["DEBUG"]
  path
end

#startObject



33
34
35
36
37
# File 'lib/chromate.rb', line 33

def start
  @started = true
  puts "Mock Chromate Browser started with options: #{@options}" if ENV["DEBUG"]
  self
end

#stopObject



39
40
41
42
43
# File 'lib/chromate.rb', line 39

def stop
  @started = false
  puts "Mock Chromate Browser stopped" if ENV["DEBUG"]
  self
end

#then {|execute_script("")| ... } ⇒ Object

Yields:



91
92
93
# File 'lib/chromate.rb', line 91

def then
  yield execute_script("") if block_given?
end