Skip to content

Commit

Permalink
Inject BROWSER env var at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
dersnek committed Mar 29, 2021
1 parent 13ee249 commit 152dfb5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ Then run `bundle install`.

Or `gem install capybara-wsl`, but then running it is a bit less straightforward.

### Important note
In order for it to work, you have to add `$BROWSER` env variable.
###### Example for Firefox
In your `.bashrc` add (notice the quotes):
```
export BROWSER="'/mnt/c/Program Files/Mozilla Firefox/firefox.exe'"
```

### Usage
Simply use
`save_and_open_page_wsl` or
Expand Down
1 change: 1 addition & 0 deletions capybara-wsl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Gem::Specification.new do |s|

s.add_dependency "capybara", ">= 2.0"
s.add_dependency "launchy", ">= 2.0"
s.add_dependency "dotenv"
end
1 change: 1 addition & 0 deletions lib/capybara/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export BROWSER="'/mnt/c/Windows/explorer.exe'"
14 changes: 9 additions & 5 deletions lib/capybara/wsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "capybara"
require "dotenv"
require "launchy"
require "capybara/dsl"
require_relative "wsl/dsl"

Expand All @@ -24,12 +25,15 @@ def self.save_and_open_screenshot(path = nil, **options)
private

def self.open_file(path)
Dotenv.overload!(File.expand_path("#{__FILE__}/../.env"))
wsl_path = modify_path(path)
Capybara.current_session.send(:open_file, wsl_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.prepend(detect_path)
path[1..-1].prepend(detect_path).gsub("/", "\\")
end

def self.detect_path
Expand All @@ -40,10 +44,10 @@ def self.detect_path
"Cannot detect WSL path. Are you sure you're running WSL?")
end

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

# Add WSL-relared methods to Capybara's DSL
# Add WSL-related methods to Capybara's DSL
Capybara::DSL.include Capybara::WSL::DSL

0 comments on commit 152dfb5

Please sign in to comment.