Skip to content

Commit

Permalink
Fix: Website commands now use lazy lookup
Browse files Browse the repository at this point in the history
Supporting `pre.erb` means that any lookups need to happen at the last possible moment. Websites have a name lookup feature similar to background tasks. Background tasks adopted a lazy lookup model in `4.1.2`.

Ensuring these are all covered under test:

- `website.screenshot` is covered:

```
  1) Error:
SystemsCliTest#test_screenshots_dir:
  2) Error:
IntegrationWebsiteTest#test_screenshot_command:
```

- `website.visit` is covered

```
  1) Error:
SystemsCliTest#test_screenshots_dir:
  2) Error:
IntegrationWebsiteTest#test_screenshot_command:
```

- `website.navigate` is not covered. Adding a call to the integration test so it's at last exercised
  • Loading branch information
schneems committed Dec 16, 2024
1 parent 500fd85 commit d702e3f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

- 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 ()

## 4.1.2

- Fix: Background task name lookup is now lazy, this fixes a bug when using `:::>- pre.erb background.start(...)` (https://github.com/zombocom/rundoc/pull/95)
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 d702e3f

Please sign in to comment.