Skip to content

Commit

Permalink
Fix save_and_open_screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
dersnek committed Nov 16, 2023
1 parent 7deea2b commit 9740acb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
56 changes: 29 additions & 27 deletions lib/capybara/wsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,46 @@ module WSL
class CapybaraWSLError < StandardError; end
class CannotDetectWSLPath < CapybaraWSLError; end

def self.save_and_open_page(path = nil)
Capybara.current_session.save_page(path).tap do |s_path|
open_file(s_path)
class << self
def save_and_open_page(path = nil)
Capybara.current_session.save_page(path).tap do |s_path|
open_file(s_path)
end
end
end

def self.save_and_open_screenshot(path = nil, **options)
Capybara.current_session.save_screenshot(path, options).tap do |s_path|
open_file(s_path)
def save_and_open_screenshot(path = nil, **options)
Capybara.current_session.save_screenshot(path, **options).tap do |s_path|
open_file(s_path)
end
end
end

private
private

def self.open_file(path)
Dotenv.overload!(File.expand_path("#{__FILE__}/../.env"))
wsl_path = modify_path(path)
Launchy.open(wsl_path)
rescue LoadError
warn "File saved to #{wsl_path}.\nPlease install the launchy gem to open the file automatically."
end
def open_file(path)
Dotenv.overload!(File.expand_path("#{__FILE__}/../.env"))
wsl_path = modify_path(path)
Launchy.open(wsl_path)
rescue LoadError
warn "File saved to #{wsl_path}.\nPlease install the launchy gem to open the file automatically."
end

def self.modify_path(path)
path[1..-1].prepend(detect_path).gsub("/", "\\")
end
def self.modify_path(path)
path[1..-1].prepend(detect_path).gsub("/", "\\")
end

def self.detect_path
path = `wslpath -m "/"`&.strip
def self.detect_path
path = `wslpath -m "/"`&.strip

if path.empty?
raise(Capybara::WSL::CannotDetectWSLPath,
"Cannot detect WSL path. Are you sure you're running WSL?")
end
if path.empty?
raise(Capybara::WSL::CannotDetectWSLPath,
"Cannot detect WSL path. Are you sure you're running WSL?")
end

"file:#{path}"
"file:#{path}"
end
end
end
end

# Add WSL-related methods to Capybara's DSL
Capybara::DSL.include Capybara::WSL::DSL
Capybara::DSL.include(Capybara::WSL::DSL)
2 changes: 1 addition & 1 deletion lib/capybara/wsl/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def save_and_open_page_wsl(path = nil)
end

def save_and_open_screenshot_wsl(path = nil, **options)
Capybara::WSL.save_and_open_screenshot(path, options)
Capybara::WSL.save_and_open_screenshot(path, **options)
end
end
end
Expand Down

0 comments on commit 9740acb

Please sign in to comment.