Skip to content

Commit

Permalink
Add ability to set proxy for selenium/phantomjs
Browse files Browse the repository at this point in the history
This change adds support to **Quke** to specify a proxy server when using the **PhantomJS** and **Selenium** based drivers (in **Quke's case *chrome* and *firefox*).

The reasoning is that connection to the internet may be been setup to go via a proxy server for security purposes in some build environments. With no way currently to tell **Quke** this is means it cannot be used if this is the case.

This new feature relates to issue #46.
  • Loading branch information
Cruikshanks committed Nov 28, 2016
1 parent 482c8c1 commit 981b0a3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/quke/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ def browserstack
@data['browserstack']
end

# Return the hash of +proxy+ server settings
#
# If your environment requires you to go via a proxy server you can
# configure Quke to use it by setting the +host+ and +port+ in your config
# file.
def proxy
@data['proxy']
end

# The hash returned from this method is intended to used in a call to
# Capybara::Poltergeist::Driver.new(app, options).
#
Expand Down Expand Up @@ -145,6 +154,7 @@ def phantomjs_options
def load_data
data = default_data!(load_yml_data)
data['browserstack'] = browserstack_data(data['browserstack'])
data['proxy'] = proxy_data(data['proxy'])
data
end

Expand Down Expand Up @@ -176,6 +186,14 @@ def browserstack_data(data)
end
# rubocop:enable Metrics/MethodLength

def proxy_data(data)
data = {} if data.nil?
data.merge(
'host' => (data['host'] || '').downcase.strip,
'port' => (data['port'] || '0').to_s.downcase.strip.to_i
)
end

def load_yml_data
if File.exist? self.class.file_location
# YAML.load_file returns false if the file exists but is empty. So
Expand Down
6 changes: 5 additions & 1 deletion spec/data/.as_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
# string Freakin::Configuration can still handle it
pause: '1'

stop_on_error: true
stop_on_error: 'true'

proxy:
host: 'localhost'
port: '9090'
4 changes: 4 additions & 0 deletions spec/data/.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ driver: chrome
pause: 1
stop_on_error: true

proxy:
host: 'localhost'
port: 9090

browserstack:
username: jdoe
auth_key: 123456789ABCDE
Expand Down
31 changes: 30 additions & 1 deletion spec/quke/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@
end
end

describe '#proxy' do
context 'when NOT specified in the config file' do
it 'defaults to a blank host and port' do
Quke::Configuration.file_location = data_path('.no_file.yml')
expect(subject.proxy).to eq('host' => '', 'port' => 0)
end
end

context 'when specified in the config file' do
it 'matches the config file' do
Quke::Configuration.file_location = data_path('.simple.yml')
expect(subject.proxy).to eq(
'host' => 'localhost',
'port' => 9090
)
end
end

context 'when port is specified in the config file as a string' do
it 'matches the config file' do
Quke::Configuration.file_location = data_path('.as_string.yml')
expect(subject.proxy).to eq(
'host' => 'localhost',
'port' => 9090
)
end
end
end

describe '#browserstack' do
context 'when NOT specified in the config file' do
it 'defaults to a blank username and auth_key' do
Expand Down Expand Up @@ -140,7 +169,7 @@
Quke::Configuration.file_location = data_path('.no_file.yml')
# rubocop:disable Style/StringLiterals
expect(subject.to_s).to eq(
"{\"features_folder\"=>\"features\", \"app_host\"=>\"\", \"driver\"=>\"phantomjs\", \"pause\"=>0, \"stop_on_error\"=>\"false\", \"browserstack\"=>{\"username\"=>\"\", \"auth_key\"=>\"\"}}"
"{\"features_folder\"=>\"features\", \"app_host\"=>\"\", \"driver\"=>\"phantomjs\", \"pause\"=>0, \"stop_on_error\"=>\"false\", \"browserstack\"=>{\"username\"=>\"\", \"auth_key\"=>\"\"}, \"proxy\"=>{\"host\"=>\"\", \"port\"=>0}}"
)
# rubocop:enable Style/StringLiterals
end
Expand Down

0 comments on commit 981b0a3

Please sign in to comment.