From 4429fcd4d32b663f8d08483c0e3f296e44c2ab4a Mon Sep 17 00:00:00 2001 From: Pulkit Sharma Date: Sat, 10 Feb 2018 16:59:02 +0530 Subject: [PATCH] [rb] Add helper method to launch Chrome in headless mode. This makes it consistent with Firefox::Options#headless! Signed-off-by: Alex Rodionov --- rb/lib/selenium/webdriver/chrome/options.rb | 14 +++++++++ .../selenium/webdriver/chrome/options_spec.rb | 20 +++++++++++++ .../selenium/webdriver/chrome/options_spec.rb | 29 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/rb/lib/selenium/webdriver/chrome/options.rb b/rb/lib/selenium/webdriver/chrome/options.rb index b48525e77aec4..87d681b6a8d94 100755 --- a/rb/lib/selenium/webdriver/chrome/options.rb +++ b/rb/lib/selenium/webdriver/chrome/options.rb @@ -89,6 +89,8 @@ def add_encoded_extension(encoded) # def add_argument(arg) + return if @args.include?(arg) + @args << arg end @@ -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 # diff --git a/rb/spec/integration/selenium/webdriver/chrome/options_spec.rb b/rb/spec/integration/selenium/webdriver/chrome/options_spec.rb index 66e5b0ec66efb..bbc0e929e0962 100644 --- a/rb/spec/integration/selenium/webdriver/chrome/options_spec.rb +++ b/rb/spec/integration/selenium/webdriver/chrome/options_spec.rb @@ -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 diff --git a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb index 38dec969d1a02..3cbec472a470d 100644 --- a/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb +++ b/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb @@ -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