Skip to content

Commit

Permalink
Merge pull request #98 from zombocom/schneems/delayed-website
Browse files Browse the repository at this point in the history
Fix: Website commands now use lazy lookup
  • Loading branch information
schneems authored Dec 16, 2024
2 parents e2f570c + c7b013c commit c682c98
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## HEAD

- Fix: Internal error in `background.wait` introduced in 4.1.2 (https://github.com/zombocom/rundoc/pull/97)
- Fix: Website commands such as `:::>> website.visit(...)` now use lazy lookup (like Background tasks since 4.1.2). This allows `pre.erb` to be used with these commands (https://github.com/zombocom/rundoc/pull/98)

## 4.1.2

Expand Down
8 changes: 6 additions & 2 deletions lib/rundoc/code_command/website/navigate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ class Rundoc::CodeCommand::Website
class Navigate < Rundoc::CodeCommand
def initialize(name:)
@name = name
@driver = Rundoc::CodeCommand::Website::Driver.find(name)
@driver = nil
end

def driver
@driver ||= Rundoc::CodeCommand::Website::Driver.find(@name)
end

def to_md(env = {})
Expand All @@ -11,7 +15,7 @@ def to_md(env = {})

def call(env = {})
puts "website.navigate [#{@name}]: #{contents}"
@driver.safe_eval(contents, env)
driver.safe_eval(contents, env)
""
end

Expand Down
13 changes: 9 additions & 4 deletions lib/rundoc/code_command/website/screenshot.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
class Rundoc::CodeCommand::Website
class Screenshot < Rundoc::CodeCommand
def initialize(name:, upload: false)
@driver = Rundoc::CodeCommand::Website::Driver.find(name)
@name = name
@upload = upload
@driver = nil
end

def driver
@driver ||= Rundoc::CodeCommand::Website::Driver.find(@name)
end

def to_md(env = {})
""
end

def call(env = {})
puts "Taking screenshot: #{@driver.current_url}"
filename = @driver.screenshot(
puts "Taking screenshot: #{driver.current_url}"
filename = driver.screenshot(
upload: @upload,
screenshots_dir: env[:context].screenshots_dir
)

relative_filename = filename.relative_path_from(env[:context].output_dir)
env[:before] << "![Screenshot of #{@driver.current_url}](#{relative_filename})"
env[:before] << "![Screenshot of #{driver.current_url}](#{relative_filename})"
""
end

Expand Down
29 changes: 18 additions & 11 deletions lib/rundoc/code_command/website/visit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ def initialize(name:, url: nil, scroll: nil, height: 720, width: 1024, visible:
@name = name
@url = url
@scroll = scroll
@driver = Driver.new(
name: name,
url: url,
height: height,
width: width,
visible: visible
)
Driver.add(@name, @driver)
@height = height
@width = width
@visible = visible
end

def driver
@driver ||= Driver.new(
name: @name,
url: @url,
height: @height,
width: @width,
visible: @visible
).tap do |driver|
Driver.add(@name, driver)
end
end

def to_md(env = {})
Expand All @@ -26,11 +33,11 @@ def call(env = {})

puts message

@driver.visit(@url) if @url
@driver.scroll(@scroll) if @scroll
driver.visit(@url) if @url
driver.scroll(@scroll) if @scroll

return "" if contents.nil? || contents.empty?
@driver.safe_eval(contents, env)
driver.safe_eval(contents, env)

""
end
Expand Down
2 changes: 2 additions & 0 deletions test/integration/website_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def test_screenshot_command
```
:::>> website.visit(name: "example", url: "http://example.com")
:::>> website.screenshot(name: "example")
:::>> website.navigate(name: "example")
session.execute_script "window.scrollBy(0,10)"
```
RUBY

Expand Down

0 comments on commit c682c98

Please sign in to comment.