@@ -4,7 +4,7 @@ module Upright::Playwright::VideoRecording
44 VIDEO_SIZE = { width : 1280 , height : 720 }
55
66 included do
7- attr_accessor :video_path , :video_object
7+ attr_accessor :video_artifacts , :pending_video_recording
88
99 set_callback :page_ready , :after , :capture_video_reference
1010 set_callback :page_close , :after , :finalize_video
@@ -33,28 +33,58 @@ def record_video?
3333 end
3434
3535 def capture_video_reference
36- self . video_object = page . video if record_video?
36+ return unless record_video?
37+ return unless ( video = page . video )
38+
39+ self . pending_video_recording = { label : current_recording_label , video : video }
3740 end
3841
3942 def save_video
40- if video_object
41- self . video_path = video_dir . join ( "#{ SecureRandom . hex } .webm" ) . to_s
42- video_object . save_as ( video_path )
43- end
43+ return unless pending_video_recording
44+
45+ video_path = video_dir . join ( "#{ SecureRandom . hex } .webm" ) . to_s
46+ pending_video_recording . fetch ( :video ) . save_as ( video_path )
47+
48+ self . video_artifacts ||= [ ]
49+ video_artifacts << pending_video_recording . merge ( path : video_path )
50+ ensure
51+ self . pending_video_recording = nil
4452 end
4553
4654 def attach_video ( probe_result )
47- if video_path
48- File . open ( video_path , "rb" ) do |file |
49- Upright ::Artifact . new ( name : "#{ probe_name } .webm" , content : file ) . attach_to ( probe_result , timestamped : true )
50- end
55+ Array ( video_artifacts ) . each do |artifact |
56+ next unless File . exist? ( artifact . fetch ( :path ) )
5157
52- if logger . respond_to? ( :struct ) && probe_result . artifacts . any?
53- logger . struct probe_artifact_url : Rails . application . routes . url_helpers . rails_blob_url ( probe_result . artifacts . first , expires_in : 24 . hours )
58+ File . open ( artifact . fetch ( :path ) , "rb" ) do | file |
59+ Upright :: Artifact . new ( name : recording_artifact_filename ( artifact [ :label ] , "webm" ) , content : file ) . attach_to ( probe_result , timestamped : true )
5460 end
5561
56- FileUtils . rm ( video_path )
57- self . video_path = nil
62+ FileUtils . rm ( artifact . fetch ( :path ) )
5863 end
64+
65+ if logger . respond_to? ( :struct )
66+ video_artifact = probe_result . artifacts . find { |attached | attached . content_type == "video/webm" }
67+ if video_artifact
68+ logger . struct probe_artifact_url : Rails . application . routes . url_helpers . rails_blob_url ( video_artifact , expires_in : 24 . hours )
69+ end
70+ end
71+
72+ self . video_artifacts = [ ]
73+ end
74+
75+ def recording_artifact_filename ( label , extension )
76+ [ recording_artifact_basename ( label ) , extension ] . join ( "." )
77+ end
78+
79+ def recording_artifact_basename ( label )
80+ [ artifact_recording_name , label ] . compact . join ( " " )
81+ end
82+
83+ def artifact_recording_name
84+ respond_to? ( :probe_name ) ? probe_name : service_name . to_s
85+ end
86+
87+ def current_recording_label
88+ nil
5989 end
6090end
0 commit comments