Skip to content

Commit

Permalink
Add helpers API to normalize path using OS-specific directory separators
Browse files Browse the repository at this point in the history
* Platform#windows_path converts / to \\
* Platform#unix_path converts \\ to /
  • Loading branch information
p0deje committed Mar 9, 2019
1 parent 76af8b9 commit 79544d6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
12 changes: 10 additions & 2 deletions rb/lib/selenium/webdriver/common/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ def wrap_in_quotes_if_necessary(str)
windows? && !cygwin? ? %("#{str}") : str
end

def cygwin_path(path, opts = {})
def cygwin_path(path, **opts)
flags = []
opts.each { |k, v| flags << "--#{k}" if v }

`cygpath #{flags.join ' '} "#{path}"`.strip
end

def unix_path(path)
path.tr(File::ALT_SEPARATOR, File::SEPARATOR)
end

def windows_path(path)
path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
end

def make_writable(file)
File.chmod 0o766, file
end
Expand Down Expand Up @@ -153,7 +161,7 @@ def find_binary(*binary_names)
binary_names.each do |binary_name|
paths.each do |path|
full_path = File.join(path, binary_name)
full_path.tr!('\\', '/') if windows?
full_path = unix_path(full_path) if windows?
exe = Dir.glob(full_path).find { |f| File.executable?(f) }
return exe if exe
end
Expand Down
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/firefox/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start_with(profile, profile_path, *args)
if Platform.cygwin?
profile_path = Platform.cygwin_path(profile_path, windows: true)
elsif Platform.windows?
profile_path = profile_path.tr('/', '\\')
profile_path = Platform.windows_path(profile_path)
end

ENV['XRE_CONSOLE_LOG'] = profile.log_file if profile.log_file
Expand Down
5 changes: 2 additions & 3 deletions rb/spec/integration/selenium/webdriver/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ module WebDriver
element = driver.find_element(id: 'upload')
expect(element.attribute('value')).to be_empty

file = Tempfile.new('file-upload')
path = file.path
path.tr!('/', '\\') if WebDriver::Platform.windows?
path = Tempfile.new('file-upload').path
path = WebDriver::Platform.windows_path(path) if WebDriver::Platform.windows?

element.send_keys path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def url_for(filename)
end

def fix_windows_path(path)
return path unless WebDriver::Platform.os.windows?
return path unless WebDriver::Platform.windows?

if GlobalTestEnv.browser == :ie
path = path[%r{file://(.*)}, 1]
path.tr!('/', '\\')
path = WebDriver::Platform.windows_path(path)

"file://#{path}"
else
Expand Down

0 comments on commit 79544d6

Please sign in to comment.