Sha256: 4cb94cf989f2104275dbb70a07e20d21ff7b723d1cb39e7711ecf1fc087bb3c6

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

<script>

  function getPathTo(element) {
    if (element.tagName === 'HTML') {
      return '/HTML[1]';
    }
    if (element===document.body) {
      return '/HTML[1]/BODY[1]';
    }
    var ix = 0;
    var siblings = element.parentNode.childNodes;
    for (var i= 0; i<siblings.length; i++) {
      var sibling= siblings[i];
      if (sibling===element) {
        return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
      }
      if (sibling.nodeType===1 && sibling.tagName===element.tagName) {
        ix++;
      }
    }
  }

  function visibleFilter(target){
    var computedStyle = window.getComputedStyle(target);
    return !(computedStyle.display === 'none' || computedStyle.visibility === 'hidden' || target.hidden)
  }


  function finderForElement(element) {
    // Try to find just using the element tagName
    var tagName = element.tagName.toLowerCase();
    if (document.querySelectorAll(tagName).length === 1) {
      return `find('${tagName}')`;
    }
    // Try adding in the classes of the element
    var classList = [].slice.apply(element.classList)
    var classString = classList.length ? "." + classList.join('.') : "";
    if (classList.length && document.querySelectorAll(tagName + classList).length === 1) {
      return `find('${tagName}${classString}')`;
    }
    // Try adding in the text of the element
    var text = element.textContent.trim();
    var targets = Array.from(document.querySelectorAll(`${tagName}${classString}`))
                       .filter(visibleFilter)
                       .filter((el) => el.textContent.includes(text))
    if (text && targets.length === 1) {
      return `find('${tagName}${classString}', text: '${text}')`;
    }
    // use the xpath to the element
    return `find(:xpath, '${getPathTo(element)}')`;
  }


</script>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
magic_test-0.0.9 app/views/magic_test/_finders.html
magic_test-0.0.7 app/views/magic_test/_finders.html
magic_test-0.0.6 app/views/magic_test/_finders.html
magic_test-0.0.5 app/views/magic_test/_finders.html