Dduvall has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/222346

Change subject: WIP: Attempting video recording with headless
......................................................................

WIP: Attempting video recording with headless

Moved headless initialization to `Environment` so it's independent of
Cucumber and can be tested more easily.

Implemented setup and teardown tasks for started, stopping, saving
video.

Recording is currently broken as ffmpeg wasn't available for
installation.

Paired with Zeljko.

Change-Id: I56f47884adde3b201a5cffc6e35915243db33661
---
M Gemfile
A features/recording.feature
M features/step_definitions/environment_steps.rb
A features/step_definitions/headless_steps.rb
M lib/mediawiki_selenium/environment.rb
M lib/mediawiki_selenium/support/hooks.rb
6 files changed, 83 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/selenium 
refs/changes/46/222346/1

diff --git a/Gemfile b/Gemfile
index fa75df1..9fd8c21 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,3 +1,5 @@
 source 'https://rubygems.org'
 
 gemspec
+
+gem 'pry-byebug'
diff --git a/features/recording.feature b/features/recording.feature
new file mode 100644
index 0000000..dfccb89
--- /dev/null
+++ b/features/recording.feature
@@ -0,0 +1,18 @@
+Feature: Recording of headless session
+
+  As a developer writing and running headless tests, it would be helpful to
+  have a video recording of the session so that troubleshooting/debugging
+  failures can be done more easily.
+
+  Background:
+    Given I have ffmpeg installed
+      And I have configured my environment with:
+      """
+      headless: true
+      """
+      And I have started a headless session
+
+  Scenario: A video file is saved upon teardown
+    Given the environment has been setup
+    When the environment is torn down
+    Then a file called "video.mov" exists
diff --git a/features/step_definitions/environment_steps.rb 
b/features/step_definitions/environment_steps.rb
index c599cfa..cf4ca72 100644
--- a/features/step_definitions/environment_steps.rb
+++ b/features/step_definitions/environment_steps.rb
@@ -17,6 +17,25 @@
   end
 end
 
+Given(/^I have started a headless session$/) do
+  MediawikiSelenium::Environment.start_headless(@env)
+end
+
+Given(/^the environment has been setup$/) do
+  @env.setup
+end
+
+When(/^the environment is torn down$/) do
+  require 'pry-byebug'
+  binding.pry
+  @env.teardown
+  @env = nil
+end
+
+Then(/^a file called "(.*?)" exists$/) do |file|
+  expect(Pathname.new(file)).to exist
+end
+
 After do
   @env.teardown unless @env.nil?
 end
diff --git a/features/step_definitions/headless_steps.rb 
b/features/step_definitions/headless_steps.rb
new file mode 100644
index 0000000..a317d98
--- /dev/null
+++ b/features/step_definitions/headless_steps.rb
@@ -0,0 +1,3 @@
+Given(/^I have ffmpeg installed$/) do
+  expect(system('which ffmpeg')).to be(true)
+end
diff --git a/lib/mediawiki_selenium/environment.rb 
b/lib/mediawiki_selenium/environment.rb
index 936f25e..7e8c42f 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -64,6 +64,33 @@
 
     class << self
       attr_accessor :default_configuration
+      attr_reader :headless
+
+      # Starts a new headless session.
+      #
+      # @param env [Environment] Environment for which to start headless.
+      #
+      def start_headless(env)
+        require 'headless'
+
+        headless_options = {}.tap do |options|
+          display = env.lookup(:headless_display, default: nil)
+          options[:display] = display unless display.nil?
+          options[:reuse] = false if env.lookup(:headless_reuse, default: 
true) == 'false'
+          options[:destroy_at_exit] = false if 
env.lookup(:headless_destroy_at_exit, default: true) == 'false'
+        end
+
+        @headless = Headless.new(headless_options)
+        @headless.start
+      end
+
+      # Whether a headless session has been started.
+      #
+      # @return [true, false]
+      #
+      def headless?
+        [email protected]?
+      end
 
       # Instantiates a new environment using the given set of default
       # configuration from `environments.yml` in the current working
@@ -335,6 +362,13 @@
       RemoteBrowserFactory::REQUIRED_CONFIG.all? { |name| lookup(name, 
default: false) }
     end
 
+    # Executes setup tasks. If a headless session is active, starts video
+    # recording.
+    #
+    def setup
+      self.class.headless.video.start_capture if self.class.headless?
+    end
+
     # Executes teardown tasks including instructing all browser factories to
     # close any open browsers and perform their own teardown tasks.
     #
@@ -357,6 +391,8 @@
 
         factory.teardown(self, status)
       end
+
+      self.class.headless.video.stop_and_save("video.mov") if 
self.class.headless?
     end
 
     # Returns a name from the given scenario.
diff --git a/lib/mediawiki_selenium/support/hooks.rb 
b/lib/mediawiki_selenium/support/hooks.rb
index a36d975..e3b3ca9 100644
--- a/lib/mediawiki_selenium/support/hooks.rb
+++ b/lib/mediawiki_selenium/support/hooks.rb
@@ -18,17 +18,8 @@
   end
 
   # Initiate headless mode
-  if ENV['HEADLESS'] == 'true' && ENV['BROWSER'] != 'phantomjs'
-    require 'headless'
-
-    headless_options = {}.tap do |options|
-      options[:display] = ENV['HEADLESS_DISPLAY'] if 
ENV.include?('HEADLESS_DISPLAY')
-      options[:reuse] = false if ENV['HEADLESS_REUSE'] == 'false'
-      options[:destroy_at_exit] = false if ENV['HEADLESS_DESTROY_AT_EXIT'] == 
'false'
-    end
-
-    headless = Headless.new(headless_options)
-    headless.start
+  if env.lookup(:headless, default: false) == 'true' && env.browser_name != 
:phantomjs
+    MediawikiSelenium::Environment.start_headless(env)
   end
 end
 
@@ -76,6 +67,9 @@
   browser_factory.configure(:build_number) do |build, options|
     options[:desired_capabilities][:name] += "##{build}"
   end
+
+  # Perform environment setup tasks
+  setup
 end
 
 After do |scenario|

-- 
To view, visit https://gerrit.wikimedia.org/r/222346
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I56f47884adde3b201a5cffc6e35915243db33661
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: master
Gerrit-Owner: Dduvall <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to