diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dad8b2..6417f5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rundoc/code_command/website/navigate.rb b/lib/rundoc/code_command/website/navigate.rb index 502b3d6..ef518b0 100644 --- a/lib/rundoc/code_command/website/navigate.rb +++ b/lib/rundoc/code_command/website/navigate.rb @@ -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 = {}) @@ -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 diff --git a/lib/rundoc/code_command/website/screenshot.rb b/lib/rundoc/code_command/website/screenshot.rb index 2548bbb..d614a12 100644 --- a/lib/rundoc/code_command/website/screenshot.rb +++ b/lib/rundoc/code_command/website/screenshot.rb @@ -1,8 +1,13 @@ 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 = {}) @@ -10,14 +15,14 @@ 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 diff --git a/lib/rundoc/code_command/website/visit.rb b/lib/rundoc/code_command/website/visit.rb index 4675f9e..043e585 100644 --- a/lib/rundoc/code_command/website/visit.rb +++ b/lib/rundoc/code_command/website/visit.rb @@ -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 = {}) @@ -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 diff --git a/test/integration/website_test.rb b/test/integration/website_test.rb index e6b0aa6..64e359d 100644 --- a/test/integration/website_test.rb +++ b/test/integration/website_test.rb @@ -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