Class: Zenrows::JsInstructions

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

Overview

DSL for building JavaScript instructions

JavaScript instructions enable dynamic interaction with web pages by automating user actions like clicking, filling forms, scrolling, and executing custom JavaScript.

Examples:

Building instructions with DSL

instructions = Zenrows::JsInstructions.build do
  click '.load-more'
  wait 2000
  fill 'input#email', '[email protected]'
  scroll_to :bottom
  wait_for '.results'
end

Using with client

client = Zenrows::Client.new
http = client.http(
  js_render: true,
  js_instructions: instructions
)

Author:

  • Ernest Bursa

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsInstructions

Returns a new instance of JsInstructions.

Since:

  • 0.1.0



35
36
37
# File 'lib/zenrows/js_instructions.rb', line 35

def initialize
  @instructions = []
end

Instance Attribute Details

#instructionsArray<Hash> (readonly)

Returns List of instructions.

Returns:

  • (Array<Hash>)

    List of instructions

Since:

  • 0.1.0



33
34
35
# File 'lib/zenrows/js_instructions.rb', line 33

def instructions
  @instructions
end

Class Method Details

.build {|JsInstructions| ... } ⇒ JsInstructions

Build instructions using DSL block

Examples:

Zenrows::JsInstructions.build do
  click '.button'
  wait 1000
end

Yields:

Returns:

Since:

  • 0.1.0



49
50
51
52
53
# File 'lib/zenrows/js_instructions.rb', line 49

def self.build(&block)
  builder = new
  builder.instance_eval(&block) if block
  builder
end

Instance Method Details

#check(selector) ⇒ self

Check a checkbox

Parameters:

  • selector (String)

    CSS selector

Returns:

  • (self)

Since:

  • 0.1.0



123
124
125
126
# File 'lib/zenrows/js_instructions.rb', line 123

def check(selector)
  @instructions << {check: selector}
  self
end

#click(selector) ⇒ self

Click an element

Examples:

click '.submit-button'
click '#load-more'

Parameters:

  • selector (String)

    CSS selector

Returns:

  • (self)

Since:

  • 0.1.0



63
64
65
66
# File 'lib/zenrows/js_instructions.rb', line 63

def click(selector)
  @instructions << {click: selector}
  self
end

#empty?Boolean

Check if there are any instructions

Returns:

  • (Boolean)

Since:

  • 0.1.0



256
257
258
# File 'lib/zenrows/js_instructions.rb', line 256

def empty?
  @instructions.empty?
end

#evaluate(code) ⇒ self

Execute custom JavaScript

Examples:

evaluate "window.scrollTo(0, document.body.scrollHeight)"
evaluate "document.querySelector('.modal').remove()"

Parameters:

  • code (String)

    JavaScript code to execute

Returns:

  • (self)

Since:

  • 0.1.0



193
194
195
196
# File 'lib/zenrows/js_instructions.rb', line 193

def evaluate(code)
  @instructions << {evaluate: code}
  self
end

#fill(selector, value) ⇒ self

Fill an input field

Examples:

fill 'input#email', '[email protected]'
fill 'input[name="password"]', 'secret123'

Parameters:

  • selector (String)

    CSS selector for input

  • value (String)

    Value to fill

Returns:

  • (self)

Since:

  • 0.1.0



114
115
116
117
# File 'lib/zenrows/js_instructions.rb', line 114

def fill(selector, value)
  @instructions << {fill: [selector, value]}
  self
end

#frame_click(iframe_selector, element_selector) ⇒ self

Click element inside iframe

Parameters:

  • iframe_selector (String)

    CSS selector for iframe

  • element_selector (String)

    CSS selector for element inside iframe

Returns:

  • (self)

Since:

  • 0.1.0



203
204
205
206
# File 'lib/zenrows/js_instructions.rb', line 203

def frame_click(iframe_selector, element_selector)
  @instructions << {frame_click: [iframe_selector, element_selector]}
  self
end

#frame_evaluate(iframe_name, code) ⇒ self

Execute JavaScript inside iframe

Parameters:

  • iframe_name (String)

    Iframe name or URL

  • code (String)

    JavaScript code

Returns:

  • (self)

Since:

  • 0.1.0



234
235
236
237
# File 'lib/zenrows/js_instructions.rb', line 234

def frame_evaluate(iframe_name, code)
  @instructions << {frame_evaluate: [iframe_name, code]}
  self
end

#frame_fill(iframe_selector, input_selector, value) ⇒ self

Fill input inside iframe

Parameters:

  • iframe_selector (String)

    CSS selector for iframe

  • input_selector (String)

    CSS selector for input

  • value (String)

    Value to fill

Returns:

  • (self)

Since:

  • 0.1.0



224
225
226
227
# File 'lib/zenrows/js_instructions.rb', line 224

def frame_fill(iframe_selector, input_selector, value)
  @instructions << {frame_fill: [iframe_selector, input_selector, value]}
  self
end

#frame_wait_for(iframe_selector, element_selector) ⇒ self

Wait for element inside iframe

Parameters:

  • iframe_selector (String)

    CSS selector for iframe

  • element_selector (String)

    CSS selector for element

Returns:

  • (self)

Since:

  • 0.1.0



213
214
215
216
# File 'lib/zenrows/js_instructions.rb', line 213

def frame_wait_for(iframe_selector, element_selector)
  @instructions << {frame_wait_for: [iframe_selector, element_selector]}
  self
end

#scroll_to(position) ⇒ self

Scroll to position

Examples:

scroll_to :bottom
scroll_to :top

Parameters:

  • position (String, Symbol)

    :bottom or :top

Returns:

  • (self)

Since:

  • 0.1.0



180
181
182
183
# File 'lib/zenrows/js_instructions.rb', line 180

def scroll_to(position)
  @instructions << {scroll_to: position.to_s}
  self
end

#scroll_x(pixels) ⇒ self

Scroll horizontally

Parameters:

  • pixels (Integer)

    Pixels to scroll (negative = left)

Returns:

  • (self)

Since:

  • 0.1.0



167
168
169
170
# File 'lib/zenrows/js_instructions.rb', line 167

def scroll_x(pixels)
  @instructions << {scroll_x: pixels}
  self
end

#scroll_y(pixels) ⇒ self

Scroll vertically

Examples:

scroll_y 1500   # Scroll down
scroll_y -500   # Scroll up

Parameters:

  • pixels (Integer)

    Pixels to scroll (negative = up)

Returns:

  • (self)

Since:

  • 0.1.0



158
159
160
161
# File 'lib/zenrows/js_instructions.rb', line 158

def scroll_y(pixels)
  @instructions << {scroll_y: pixels}
  self
end

#select_option(selector, value) ⇒ self

Select an option from dropdown

Examples:

select_option '#country', 'US'

Parameters:

  • selector (String)

    CSS selector for select element

  • value (String)

    Option value to select

Returns:

  • (self)

Since:

  • 0.1.0



145
146
147
148
# File 'lib/zenrows/js_instructions.rb', line 145

def select_option(selector, value)
  @instructions << {select_option: [selector, value]}
  self
end

#sizeInteger

Number of instructions

Returns:

  • (Integer)

Since:

  • 0.1.0



263
264
265
# File 'lib/zenrows/js_instructions.rb', line 263

def size
  @instructions.size
end

#to_aArray<Hash>

Convert to array

Returns:

  • (Array<Hash>)

    Instructions array

Since:

  • 0.1.0



249
250
251
# File 'lib/zenrows/js_instructions.rb', line 249

def to_a
  @instructions.dup
end

#to_json(*_args) ⇒ String

Convert to JSON string for API

Returns:

  • (String)

    JSON-encoded instructions

Since:

  • 0.1.0



242
243
244
# File 'lib/zenrows/js_instructions.rb', line 242

def to_json(*_args)
  @instructions.to_json
end

#uncheck(selector) ⇒ self

Uncheck a checkbox

Parameters:

  • selector (String)

    CSS selector

Returns:

  • (self)

Since:

  • 0.1.0



132
133
134
135
# File 'lib/zenrows/js_instructions.rb', line 132

def uncheck(selector)
  @instructions << {uncheck: selector}
  self
end

#wait(duration) ⇒ self

Wait for a duration in milliseconds

Examples:

wait 2000  # Wait 2 seconds

Parameters:

  • duration (Integer)

    Milliseconds to wait (max 10000)

Returns:

  • (self)

Since:

  • 0.1.0



75
76
77
78
# File 'lib/zenrows/js_instructions.rb', line 75

def wait(duration)
  @instructions << {wait: duration}
  self
end

#wait_event(event) ⇒ self

Wait for a browser event

Examples:

wait_event :networkidle

Parameters:

  • event (String, Symbol)

    Event name: networkidle, load, domcontentloaded

Returns:

  • (self)

Since:

  • 0.1.0



100
101
102
103
# File 'lib/zenrows/js_instructions.rb', line 100

def wait_event(event)
  @instructions << {wait_event: event.to_s}
  self
end

#wait_for(selector) ⇒ self

Wait for an element to appear

Examples:

wait_for '.dynamic-content'
wait_for '#results-loaded'

Parameters:

  • selector (String)

    CSS selector

Returns:

  • (self)

Since:

  • 0.1.0



88
89
90
91
# File 'lib/zenrows/js_instructions.rb', line 88

def wait_for(selector)
  @instructions << {wait_for: selector}
  self
end