Class: Capybara::Playwright::Node

Inherits:
Driver::Node
  • Object
show all
Defined in:
lib/capybara/playwright/node.rb

Overview

Selector and checking methods are derived from twapole/apparition Action methods (click, select_option, …) uses playwright.

ref:

selenium:   https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selenium/node.rb
apparition: https://github.com/twalpole/apparition/blob/master/lib/capybara/apparition/node.rb

Direct Known Subclasses

ShadowRootNode

Defined Under Namespace

Modules: UpdateValueJS Classes: Checkbox, ClickOptions, DateInput, DateTimeInput, DragTo, FileUpload, JSValueInput, NotActionableError, RadioButton, SendKeys, Settable, StaleReferenceError, TextInput, TimeInput

Constant Summary collapse

SCROLL_POSITIONS =
{
  top: '0',
  bottom: 'el.scrollHeight',
  center: '(el.scrollHeight - el.clientHeight)/2'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(driver, internal_logger, page, element) ⇒ Node

Returns a new instance of Node.



139
140
141
142
143
144
# File 'lib/capybara/playwright/node.rb', line 139

def initialize(driver, internal_logger, page, element)
  super(driver, element)
  @internal_logger = internal_logger
  @page = page
  @element = element
end

Instance Method Details

#==(other) ⇒ Object



1010
1011
1012
1013
1014
# File 'lib/capybara/playwright/node.rb', line 1010

def ==(other)
  return false unless other.is_a?(Node)

  @element.evaluate('(self, other) => self == other', arg: other.element)
end

#[](name) ⇒ Object



216
217
218
219
220
# File 'lib/capybara/playwright/node.rb', line 216

def [](name)
  assert_element_not_stale {
    property(name) || attribute(name)
  }
end

#all_textObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/capybara/playwright/node.rb', line 183

def all_text
  assert_element_not_stale {
    text = @element.text_content
    text.to_s.gsub(/[\u200b\u200e\u200f]/, '')
        .gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
        .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
        .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
        .tr("\u00a0", ' ')
  }
end

#checked?Boolean

Returns:

  • (Boolean)


910
911
912
913
914
# File 'lib/capybara/playwright/node.rb', line 910

def checked?
  assert_element_not_stale {
    @element.evaluate('el => !!el.checked')
  }
end

#click(keys = [], **options) ⇒ Object



437
438
439
440
# File 'lib/capybara/playwright/node.rb', line 437

def click(keys = [], **options)
  click_options = ClickOptions.new(@element, keys, options, capybara_default_wait_time)
  @element.click(**click_options.as_params)
end

#disabled?Boolean

Returns:

  • (Boolean)


922
923
924
925
926
927
928
929
930
931
932
# File 'lib/capybara/playwright/node.rb', line 922

def disabled?
  @element.evaluate(<<~JAVASCRIPT)
  function(el) {
    const xpath = 'parent::optgroup[@disabled] | \
                  ancestor::select[@disabled] | \
                  parent::fieldset[@disabled] | \
                  ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]';
    return el.disabled || document.evaluate(xpath, el, null, XPathResult.BOOLEAN_TYPE, null).booleanValue
  }
  JAVASCRIPT
end

#double_click(keys = [], **options) ⇒ Object



449
450
451
452
# File 'lib/capybara/playwright/node.rb', line 449

def double_click(keys = [], **options)
  click_options = ClickOptions.new(@element, keys, options, capybara_default_wait_time)
  @element.dblclick(**click_options.as_params)
end

#drag_to(element, **options) ⇒ Object



711
712
713
# File 'lib/capybara/playwright/node.rb', line 711

def drag_to(element, **options)
  DragTo.new(@page, @element, element.element, options).execute
end

#drop(*args) ⇒ Object

Raises:

  • (NotImplementedError)


785
786
787
# File 'lib/capybara/playwright/node.rb', line 785

def drop(*args)
  raise NotImplementedError
end

#find_css(query, **options) ⇒ Object



1024
1025
1026
1027
1028
1029
1030
# File 'lib/capybara/playwright/node.rb', line 1024

def find_css(query, **options)
  assert_element_not_stale {
    @element.query_selector_all(query).map do |el|
      Node.new(@driver, @internal_logger, @page, el)
    end
  }
end

#find_xpath(query, **options) ⇒ Object



1016
1017
1018
1019
1020
1021
1022
# File 'lib/capybara/playwright/node.rb', line 1016

def find_xpath(query, **options)
  assert_element_not_stale {
    @element.query_selector_all("xpath=#{query}").map do |el|
      Node.new(@driver, @internal_logger, @page, el)
    end
  }
end

#hoverObject



707
708
709
# File 'lib/capybara/playwright/node.rb', line 707

def hover
  @element.hover(timeout: capybara_default_wait_time)
end

#inspectObject



1006
1007
1008
# File 'lib/capybara/playwright/node.rb', line 1006

def inspect
  %(#<#{self.class} tag="#{tag_name}" path="#{path}">)
end

#multiple?Boolean

Returns:

  • (Boolean)


938
939
940
# File 'lib/capybara/playwright/node.rb', line 938

def multiple?
  @element.evaluate('el => el.multiple')
end

#obscured?Boolean

Returns:

  • (Boolean)


906
907
908
# File 'lib/capybara/playwright/node.rb', line 906

def obscured?
  @element.capybara_obscured?
end

#pathObject



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/capybara/playwright/node.rb', line 954

def path
  assert_element_not_stale {
    @element.evaluate(<<~JAVASCRIPT)
    (el) => {
      var xml = document;
      var xpath = '';
      var pos, tempitem2;
      if (el.getRootNode && el.getRootNode() instanceof ShadowRoot) {
        return "(: Shadow DOM element - no XPath :)";
      };
      while(el !== xml.documentElement) {
        pos = 0;
        tempitem2 = el;
        while(tempitem2) {
          if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) { // If it is ELEMENT_NODE of the same name
            pos += 1;
          }
          tempitem2 = tempitem2.previousSibling;
        }
        if (el.namespaceURI != xml.documentElement.namespaceURI) {
          xpath = "*[local-name()='"+el.nodeName+"' and namespace-uri()='"+(el.namespaceURI===null?'':el.namespaceURI)+"']["+pos+']'+'/'+xpath;
        } else {
          xpath = el.nodeName.toUpperCase()+"["+pos+"]/"+xpath;
        }
        el = el.parentNode;
        if (!el) {
          throw "(: Shadow DOM element - no XPath :)";
        }
      }
      xpath = '/'+xml.documentElement.nodeName.toUpperCase()+'/'+xpath;
      xpath = xpath.replace(/\\/$/, '');
      return xpath;
    }
    JAVASCRIPT
  }
end

#readonly?Boolean

Returns:

  • (Boolean)


934
935
936
# File 'lib/capybara/playwright/node.rb', line 934

def readonly?
  !@element.editable?
end

#rectObject



942
943
944
945
946
947
948
949
950
951
952
# File 'lib/capybara/playwright/node.rb', line 942

def rect
  assert_element_not_stale {
    @element.evaluate(<<~JAVASCRIPT)
    function(el){
      const rects = [...el.getClientRects()]
      const rect = rects.find(r => (r.height && r.width)) || el.getBoundingClientRect();
      return rect.toJSON();
    }
    JAVASCRIPT
  }
end

#right_click(keys = [], **options) ⇒ Object



442
443
444
445
446
447
# File 'lib/capybara/playwright/node.rb', line 442

def right_click(keys = [], **options)
  click_options = ClickOptions.new(@element, keys, options, capybara_default_wait_time)
  params = click_options.as_params
  params[:button] = 'right'
  @element.click(**params)
end

#scroll_by(x, y) ⇒ Object



789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/capybara/playwright/node.rb', line 789

def scroll_by(x, y)
  js = <<~JAVASCRIPT
  (el, [x, y]) => {
    if (el.scrollBy){
      el.scrollBy(x, y);
    } else {
      el.scrollTop = el.scrollTop + y;
      el.scrollLeft = el.scrollLeft + x;
    }
  }
  JAVASCRIPT

  @element.evaluate(js, arg: [x, y])
end

#scroll_to(element, location, position = nil) ⇒ Object



804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/capybara/playwright/node.rb', line 804

def scroll_to(element, location, position = nil)
  # location, element = element, nil if element.is_a? Symbol
  if element.is_a? Capybara::Playwright::Node
    scroll_element_to_location(element, location)
  elsif location.is_a? Symbol
    scroll_to_location(location)
  else
    scroll_to_coords(*position)
  end

  self
end

#select_optionObject



410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/capybara/playwright/node.rb', line 410

def select_option
  return false if disabled?

  select_element = parent_select_element
  if select_element.evaluate('el => el.multiple')
    selected_options = select_element.query_selector_all('option:checked')
    selected_options << @element
    select_element.select_option(element: selected_options, timeout: capybara_default_wait_time)
  else
    select_element.select_option(element: @element, timeout: capybara_default_wait_time)
  end
end

#selected?Boolean

Returns:

  • (Boolean)


916
917
918
919
920
# File 'lib/capybara/playwright/node.rb', line 916

def selected?
  assert_element_not_stale {
    @element.evaluate('el => !!el.selected')
  }
end

#send_keys(*args) ⇒ Object



533
534
535
# File 'lib/capybara/playwright/node.rb', line 533

def send_keys(*args)
  SendKeys.new(@element, args).execute
end

#set(value, **options) ⇒ Object

Parameters:

  • value (String, Array)

    Array is only allowed if node has ‘multiple’ attribute

  • options (Hash)

    Driver specific options for how to set a value on a node



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/capybara/playwright/node.rb', line 253

def set(value, **options)
  settable_class =
    case tag_name
    when 'input'
      case attribute('type')
      when 'radio'
        RadioButton
      when 'checkbox'
        Checkbox
      when 'file'
        FileUpload
      when 'date'
        DateInput
      when 'time'
        TimeInput
      when 'datetime-local'
        DateTimeInput
      when 'color'
        JSValueInput
      when 'range'
        JSValueInput
      else
        TextInput
      end
    when 'textarea'
      TextInput
    else
      if @element.editable?
        TextInput
      else
        raise NotSupportedByDriverError
      end
    end

  settable_class.new(@element, capybara_default_wait_time, @internal_logger).set(value, **options)
rescue ::Playwright::TimeoutError => err
  raise NotActionableError.new(err)
end

#shadow_rootObject



995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/capybara/playwright/node.rb', line 995

def shadow_root
  # Playwright does not distinguish shadow DOM.
  # https://playwright.dev/docs/selectors#selecting-elements-in-shadow-dom
  # Just do with Host element as shadow root Element.
  #
  #   Node.new(@driver, @page, @element.evaluate_handle('el => el.shadowRoot'))
  #
  # does not work well because of the Playwright Error 'Element is not attached to the DOM'
  ShadowRootNode.new(@driver, @internal_logger, @page, @element)
end

#style(styles) ⇒ Object

Raises:

  • (NotImplementedError)


245
246
247
248
249
# File 'lib/capybara/playwright/node.rb', line 245

def style(styles)
  # Capybara provides default implementation.
  # ref: https://github.com/teamcapybara/capybara/blob/f7ab0b5cd5da86185816c2d5c30d58145fe654ed/lib/capybara/node/element.rb#L92
  raise NotImplementedError
end

#tag_nameObject



868
869
870
# File 'lib/capybara/playwright/node.rb', line 868

def tag_name
  @tag_name ||= @element.evaluate('e => e.tagName.toLowerCase()')
end

#trigger(event) ⇒ Object



991
992
993
# File 'lib/capybara/playwright/node.rb', line 991

def trigger(event)
  @element.dispatch_event(event)
end

#unselect_optionObject



423
424
425
426
427
428
429
430
431
# File 'lib/capybara/playwright/node.rb', line 423

def unselect_option
  if parent_select_element.evaluate('el => el.multiple')
    return false if disabled?

    @element.evaluate('el => el.selected = false')
  else
    raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.'
  end
end

#valueObject



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/capybara/playwright/node.rb', line 231

def value
  assert_element_not_stale {
    # ref: https://github.com/teamcapybara/capybara/blob/f7ab0b5cd5da86185816c2d5c30d58145fe654ed/lib/capybara/selenium/node.rb#L31
    # ref: https://github.com/twalpole/apparition/blob/11aca464b38b77585191b7e302be2e062bdd369d/lib/capybara/apparition/node.rb#L728
    if tag_name == 'select' && @element.evaluate('el => el.multiple')
      @element.query_selector_all('option:checked').map do |option|
        option.evaluate('el => el.value')
      end
    else
      @element.evaluate('el => el.value')
    end
  }
end

#visible?Boolean

Returns:

  • (Boolean)


872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/capybara/playwright/node.rb', line 872

def visible?
  assert_element_not_stale {
    # if an area element, check visibility of relevant image
    @element.evaluate(<<~JAVASCRIPT)
    function(el) {
      if (el.tagName == 'AREA'){
        const map_name = document.evaluate('./ancestor::map/@name', el, null, XPathResult.STRING_TYPE, null).stringValue;
        el = document.querySelector(`img[usemap='#${map_name}']`);
        if (!el){
        return false;
        }
      }
      var forced_visible = false;
      while (el) {
        const style = window.getComputedStyle(el);
        if (style.visibility == 'visible')
          forced_visible = true;
        if ((style.display == 'none') ||
            ((style.visibility == 'hidden') && !forced_visible) ||
            (parseFloat(style.opacity) == 0)) {
          return false;
        }
        var parent = el.parentElement;
        if (parent && (parent.tagName == 'DETAILS') && !parent.open && (el.tagName != 'SUMMARY')) {
          return false;
        }
        el = parent;
      }
      return true;
    }
    JAVASCRIPT
  }
end

#visible_textObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/capybara/playwright/node.rb', line 194

def visible_text
  assert_element_not_stale {
    return '' unless visible?

    text = @element.evaluate(<<~JAVASCRIPT)
      function(el){
        if (el.nodeName == 'TEXTAREA'){
          return el.textContent;
        } else if (el instanceof SVGElement) {
          return el.textContent;
        } else {
          return el.innerText;
        }
      }
    JAVASCRIPT
    text.to_s.scrub.gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
        .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
        .gsub(/\n+/, "\n")
        .tr("\u00a0", ' ')
  }
end