Class: TestCentricity::Elements::Media

Inherits:
UIElement show all
Defined in:
lib/testcentricity_web/web_elements/media.rb

Direct Known Subclasses

Audio, Video

Constant Summary

Constants inherited from UIElement

UIElement::CSS_SELECTORS, UIElement::XPATH_SELECTORS

Instance Attribute Summary

Attributes inherited from UIElement

#alt_locator, #base_object, #context, #locator, #locator_type, #mru_driver, #mru_locator, #mru_object, #mru_parent, #name, #original_style, #parent, #type

Instance Method Summary collapse

Methods inherited from UIElement

#aria_autocomplete, #aria_busy?, #aria_checked?, #aria_colcount, #aria_controls, #aria_describedby, #aria_disabled?, #aria_expanded?, #aria_haspopup?, #aria_hidden?, #aria_invalid?, #aria_keyshortcuts, #aria_label, #aria_labelledby, #aria_live, #aria_modal?, #aria_multiline?, #aria_multiselectable?, #aria_orientation, #aria_pressed?, #aria_readonly?, #aria_required?, #aria_roledescription, #aria_rowcount, #aria_selected?, #aria_sort, #aria_valuemax, #aria_valuemin, #aria_valuenow, #aria_valuetext, #clear_alt_locator, #click, #click_at, #content_editable?, #count, #crossorigin, #disabled?, #displayed?, #double_click, #drag_and_drop, #drag_by, #enabled?, #exists?, #find_element, #focused?, #get_attribute, #get_locator, #get_locator_type, #get_name, #get_native_attribute, #get_object_type, #get_value, #height, #hidden?, #highlight, #hover, #hover_at, #initialize, #obscured?, #required?, #reset_mru_cache, #right_click, #role, #scroll_to, #send_keys, #set, #set_alt_locator, #set_locator_type, #style, #tabindex, #title, #unhighlight, #verify_value, #visible?, #wait_until_enabled, #wait_until_exists, #wait_until_gone, #wait_until_hidden, #wait_until_value_changes, #wait_until_value_is, #wait_until_visible, #wait_while_busy, #width, #x, #y

Constructor Details

This class inherits a constructor from TestCentricity::Elements::UIElement

Instance Method Details

#active_trackInteger

Return index of active text track of associated media

Examples:

track_num = media_player.active_track

Returns:

  • (Integer)

    number of active text track



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/testcentricity_web/web_elements/media.rb', line 238

def active_track
  num_tracks = track_count
  return 0 if num_tracks.zero?

  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  (0..num_tracks).each do |track|
    track_info = page.execute_script("return arguments[0].textTracks[#{track}].mode", obj)
    return track + 1 if track_info == 'showing'
  end
  0
end

#active_track_dataHash

Return properties of active text track of associated media

Examples:

track_info = media_player.active_track_data

Returns:

  • (Hash)

    properties of active text track (:kind, :label, :language, :mode)



257
258
259
260
261
262
# File 'lib/testcentricity_web/web_elements/media.rb', line 257

def active_track_data
  active = active_track
  return nil if active.zero?

  track_data(active)
end

#active_track_sourceString

Return src property of active text track of associated media

Examples:

track_src = media_player.active_track_source

Returns:

  • (String)

    src property of active text track



307
308
309
310
311
312
# File 'lib/testcentricity_web/web_elements/media.rb', line 307

def active_track_source
  active = active_track
  return nil if active.zero?

  track_source(active)
end

#all_tracks_dataArray of Hash

Return properties of all text tracks of associated media

Examples:

all_track_info = media_player.all_tracks_data

Returns:

  • (Array of Hash)

    properties of active text track (:kind, :label, :language, :mode)



270
271
272
273
274
275
276
277
278
279
# File 'lib/testcentricity_web/web_elements/media.rb', line 270

def all_tracks_data
  num_tracks = track_count
  return 0 if num_tracks.zero?

  all_data = []
  (1..num_tracks).each do |track|
    all_data.push( { track => track_data(track) })
  end
  all_data
end

#autoplay?Boolean

Return media autoplay property

Examples:

media_player.autoplay?

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/testcentricity_web/web_elements/media.rb', line 10

def autoplay?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  state = obj.native.attribute('autoplay')
  state.boolean? ? state : state == 'true'
end

#controls?Boolean

Return media controls property

Examples:

media_player.controls?

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/testcentricity_web/web_elements/media.rb', line 36

def controls?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  state = obj.native.attribute('controls')
  state.boolean? ? state : state == 'true'
end

#current_timeFloat

Return media currentTime property

Examples:

current_player_time = media_player.current_time

Returns:

  • (Float)

    current playback position in seconds



126
127
128
129
130
# File 'lib/testcentricity_web/web_elements/media.rb', line 126

def current_time
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('currentTime').to_f.round(2)
end

#current_time=(value) ⇒ Object

Set the media currentTime property

Examples:

media_player.current_time = 1.5

Parameters:

  • value (Float)

    time in seconds



334
335
336
337
338
# File 'lib/testcentricity_web/web_elements/media.rb', line 334

def current_time=(value)
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].currentTime = arguments[1]', obj, value)
end

#default_muted?Boolean

Return media defaultMuted property

Examples:

media_player.default_muted?

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/testcentricity_web/web_elements/media.rb', line 75

def default_muted?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  mute_state = obj.native.attribute('defaultMuted')
  mute_state.boolean? ? mute_state : mute_state == 'true'
end

#default_playback_rateInteger or Float

Return media defaultPlaybackRate property

Examples:

default_speed = media_player.default_playback_rate

Returns:

  • (Integer or Float)

    default playback speed



138
139
140
141
142
# File 'lib/testcentricity_web/web_elements/media.rb', line 138

def default_playback_rate
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('defaultPlaybackRate').to_f
end

#durationFloat

Return media duration property

Examples:

how_long = media_player.duration

Returns:

  • (Float)

    duration of media



150
151
152
153
154
# File 'lib/testcentricity_web/web_elements/media.rb', line 150

def duration
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('duration').to_f.round(2)
end

#ended?Boolean

Return media ended property

Examples:

media_player.ended?

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/testcentricity_web/web_elements/media.rb', line 23

def ended?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  state = obj.native.attribute('ended')
  state.boolean? ? state : state == 'true'
end

#loop?Boolean

Return media loop property

Examples:

media_player.loop?

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/testcentricity_web/web_elements/media.rb', line 49

def loop?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  loop_state = obj.native.attribute('loop')
  loop_state.boolean? ? loop_state : loop_state == 'true'
end

#muteObject

Mute the media's audio

Examples:

media_player.mute


367
368
369
370
371
# File 'lib/testcentricity_web/web_elements/media.rb', line 367

def mute
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].muted = true;', obj)
end

#muted?Boolean

Return media muted property

Examples:

media_player.muted?

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/testcentricity_web/web_elements/media.rb', line 62

def muted?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  mute_state = obj.native.attribute('muted')
  mute_state.boolean? ? mute_state : mute_state == 'true'
end

#pauseObject

Pause the media

Examples:

media_player.pause


356
357
358
359
360
# File 'lib/testcentricity_web/web_elements/media.rb', line 356

def pause
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].pause()', obj)
end

#paused?Boolean

Return media paused property

Examples:

media_player.paused?

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/testcentricity_web/web_elements/media.rb', line 88

def paused?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  paused_state = obj.native.attribute('paused')
  paused_state.boolean? ? paused_state : paused_state == 'true'
end

#playObject

Play the media

Examples:

media_player.play


345
346
347
348
349
# File 'lib/testcentricity_web/web_elements/media.rb', line 345

def play
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].play()', obj)
end

#playback_rateInteger or Float

Return media playbackRate property

Examples:

playback_speed = media_player.playback_rate

Returns:

  • (Integer or Float)

    current playback speed



162
163
164
165
166
# File 'lib/testcentricity_web/web_elements/media.rb', line 162

def playback_rate
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('playbackRate').to_f
end

#playback_rate=(value) ⇒ Object

Set the media playbackRate property

Examples:

media_player.playback_rate = 1.5

Parameters:

  • value (Float)


390
391
392
393
394
# File 'lib/testcentricity_web/web_elements/media.rb', line 390

def playback_rate=(value)
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].playbackRate = arguments[1]', obj, value)
end

#preloadObject

Return media preload property

Examples:

preload = media_player.preload

Returns:

  • preload value



414
415
416
417
418
# File 'lib/testcentricity_web/web_elements/media.rb', line 414

def preload
  obj, = find_element
  object_not_found_exception(obj, @type)
  obj.native.attribute('preload')
end

#ready_stateInteger

Return media readyState property

Examples:

media_status = media_player.ready_state

Returns:

  • (Integer)

    media ready state 0 = HAVE_NOTHING - no information whether or not the audio/video is ready 1 = HAVE_METADATA - metadata for the audio/video is ready 2 = HAVE_CURRENT_DATA - data for the current playback position is available, but not enough data to play next frame/millisecond 3 = HAVE_FUTURE_DATA - data for the current and at least the next frame is available 4 = HAVE_ENOUGH_DATA - enough data available to start playing



179
180
181
182
183
# File 'lib/testcentricity_web/web_elements/media.rb', line 179

def ready_state
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('return arguments[0].readyState', obj)
end

#seeking?Boolean

Return media seeking property

Examples:

media_player.seeking?

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/testcentricity_web/web_elements/media.rb', line 101

def seeking?
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  state = obj.native.attribute('seeking')
  state.boolean? ? state : state == 'true'
end

#srcString

Return media src property

Examples:

src_value = media_player.src

Returns:

  • (String)

    value of src property



114
115
116
117
118
# File 'lib/testcentricity_web/web_elements/media.rb', line 114

def src
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('src')
end

#track_countInteger

Return number of text tracks of associated media

Examples:

num_tracks = media_player.track_count

Returns:

  • (Integer)

    number of text tracks



226
227
228
229
230
# File 'lib/testcentricity_web/web_elements/media.rb', line 226

def track_count
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('return arguments[0].textTracks.length', obj)
end

#track_data(track) ⇒ Hash

Return properties of specified text track of associated media

Examples:

track_info = media_player.track_data(1)

Parameters:

  • track (Integer)

    index of requested track

Returns:

  • (Hash)

    properties of requested text track (:kind, :label, :language, :mode)



288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/testcentricity_web/web_elements/media.rb', line 288

def track_data(track)
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  track_mode = page.execute_script("return arguments[0].textTracks[#{track - 1}].mode", obj)
  track_obj = obj.find(:css, "track:nth-of-type(#{track})", visible: :all, wait: 1)
  {
    kind: track_obj[:kind],
    label: track_obj[:label],
    language: track_obj[:srclang],
    mode: track_mode
  }
end

#track_source(track) ⇒ String

Return srv property of specified text track of associated media

Examples:

track_src = media_player.track_source(2)

Parameters:

  • track (Integer)

    index of requested track

Returns:

  • (String)

    src property of requested text track



321
322
323
324
325
326
# File 'lib/testcentricity_web/web_elements/media.rb', line 321

def track_source(track)
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  track_obj = obj.find(:css, "track:nth-of-type(#{track})", visible: :all, wait: 1)
  track_obj[:src]
end

#unmuteObject

Unmute the media's audio

Examples:

media_player.unmute


378
379
380
381
382
# File 'lib/testcentricity_web/web_elements/media.rb', line 378

def unmute
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].muted = false;', obj)
end

#volumeFloat

Return media volume property

Examples:

volume_level = media_player.volume

Returns:

  • (Float)

    media volume setting



214
215
216
217
218
# File 'lib/testcentricity_web/web_elements/media.rb', line 214

def volume
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  obj.native.attribute('volume').to_f
end

#volume=(value) ⇒ Object

Set the media volume property

Examples:

media_player.volume = 0.5

Parameters:

  • value (Float)

    between 0 and 1



402
403
404
405
406
# File 'lib/testcentricity_web/web_elements/media.rb', line 402

def volume=(value)
  obj, = find_element(visible = :all)
  object_not_found_exception(obj, @type)
  page.execute_script('arguments[0].volume = arguments[1]', obj, value)
end

#wait_until_ready_state_is(value, seconds = nil, post_exception = true) ⇒ Object

Wait until the media object's readyState value equals the specified value, or until the specified wait time has expired. If the wait time is nil, then the wait time will be Capybara.default_max_wait_time.

Examples:

media_player.wait_until_ready_state_is(4, 5)

Parameters:

  • value (Integer)

    value expected

  • seconds (Integer or Float) (defaults to: nil)

    wait time in seconds



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/testcentricity_web/web_elements/media.rb', line 193

def wait_until_ready_state_is(value, seconds = nil, post_exception = true)
  timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until do
    reset_mru_cache
    ready_state == value
  end
rescue StandardError
  if post_exception
    raise "Ready state of media #{object_ref_message} failed to equal '#{value}' after #{timeout} seconds" unless get_value == value
  else
    ready_state == value
  end
end