Skip to content

Commit

Permalink
[rb] Add helper method to launch Chrome in headless mode.
Browse files Browse the repository at this point in the history
This makes it consistent with Firefox::Options#headless!

Signed-off-by: Alex Rodionov <p0deje@gmail.com>
  • Loading branch information
pulkitsharma07 authored and p0deje committed Feb 13, 2018
1 parent 6d4b920 commit 4429fcd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rb/lib/selenium/webdriver/chrome/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def add_encoded_extension(encoded)
#

def add_argument(arg)
return if @args.include?(arg)

@args << arg
end

Expand Down Expand Up @@ -122,6 +124,18 @@ def add_preference(name, value)
prefs[name] = value
end

#
# Run Chrome in headless mode.
#
# @example Enable headless mode
# options = Selenium::WebDriver::Chrome::Options.new
# options.headless!
#

def headless!
add_argument '--headless'
end

#
# Add an emulation device name
#
Expand Down
20 changes: 20 additions & 0 deletions rb/spec/integration/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ module Chrome
expect(ua).to eq('foo;bar')
end
end

context 'headless mode' do
it 'should be able to run in headless mode with #headless!' do
subject.headless!

create_driver!(options: subject) do |driver|
ua = driver.execute_script 'return window.navigator.userAgent'
expect(ua).to match(/HeadlessChrome/)
end
end

it 'should be able to run in headless mode with #add_argument' do
subject.add_argument('--headless')

create_driver!(options: subject) do |driver|
ua = driver.execute_script 'return window.navigator.userAgent'
expect(ua).to match(/HeadlessChrome/)
end
end
end
end
end # Chrome
end # WebDriver
Expand Down
29 changes: 29 additions & 0 deletions rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ module Chrome
subject.add_argument('foo')
expect(subject.args).to include('foo')
end

context 'multiple arguments' do
it 'should add only unique arguments' do
subject.add_argument('foo')
subject.add_argument('bar')

expect(subject.args).to eq %w[foo bar]
end

it 'should not add same argument more than once' do
subject.add_argument('foo')
subject.add_argument('foo')

expect(subject.args).to eq ['foo']
end
end
end

describe '#headless!' do
it 'should add the --headless command-line argument' do
subject.headless!
expect(subject.args).to eql ['--headless']
end

it 'should not add the --headless command-line argument if already present' do
subject.add_argument('--headless')
subject.headless!
expect(subject.args).to eql ['--headless']
end
end

describe '#add_option' do
Expand Down

0 comments on commit 4429fcd

Please sign in to comment.