【问题标题】:The Intern: Polling Until Element is Visible实习生:轮询直到元素可见
【发布时间】:2015-09-16 16:48:17
【问题描述】:

实习生有没有办法可以轮询直到元素可见?我网站中的很多元素都在 dom 中但被隐藏了,所以每次我在应该出现之后执行“查找”元素 X 时,它都会失败,因为该元素明显破坏了 selenium 检查的可见属性之一。

我已经尝试过辅助函数“pollUntil”,但我似乎无法让它工作。 Dojo 似乎不喜欢 document.getElement*()

传递给 pollUntil 的辅助函数

//this is a helper function for pollUntil
//first we want to find an element by class name and text
var elementVisibleAndText = function(elems, innerText){
        elems = document.getElementsByClassName(elems);
        //if we can't even find it, we return null
        //but if we do find it, we want to return a
        //not null element
        if (!elems || elems.length == 0){
            return null; 
        }

        //now let's look at all of the elements found by
        //in elems, and see if the innerHTML matches. If it 
        //does then we want to return that it was found
        var each;
        for(each in elems){
            if(elems[each].innerHTML == innerText)
                return (elems[each].offsetWidth > 0 && elems[each].offsetHeight > 0) ? elems[each] : null;
        }
        //else return null if nothing is found in the elements

        return null;
};  

【问题讨论】:

    标签: intern


    【解决方案1】:

    查看https://theintern.github.io/leadfoot/pollUntil.html。 Intern 使用 Leadfoot - 因此您应该可以访问此功能。

    var Command = require('leadfoot/Command');
    var pollUntil = require('leadfoot/helpers/pollUntil');
    
    new Command(session)
        .get('http://example.com')
        .then(pollUntil('return document.getElementById("a");', 1000))
        .then(function (elementA) {
            // element was found
        }, function (error) {
            // element was not found
        });
    

    要在您的一项测试中使用该函数 - 您可以使用以下路径导入它:

    'intern/dojo/node!leadfoot/helpers/pollUntil'
    

    【讨论】:

      【解决方案2】:

      我一直遇到这个问题,我们使用实习生的 pollUntil 功能编写了一些帮助实用程序。在我们的测试中,我们使用类似.then(pollUntil(util.element_visible_by_class(), ['toast_notification'], 22000))

      在一个单独的util.js 文件中我们有

      /**
       * Our shared utility for unit testing
       */
      define([
          'intern!object',
          'intern/chai!assert',
          'require',
          'intern/dojo/node!leadfoot/helpers/pollUntil'
      
      ], function (registerSuite, assert, require, util, pollUntil) {
          return {
              element_visible_by_class: function(elem) {
                  return function(elem) {
                      elem = document.getElementsByClassName(elem);
                      if (!elem || elem.length == 0) { return null; }
                      elem = elem[0];
                      return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
                  }  
              },
      
              element_visible_by_id: function(elem) {
                  return function(elem) {
                      elem = document.getElementById(elem);
                      if (!elem) { return null; }
                      return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
                  }  
              },
              element_hidden_by_id: function(elem) {
                  return function(elem) {
                      elem = document.getElementById(elem);
                      if (!elem) { return null; }
                      return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
                  }  
              },
              element_hidden_by_class: function(elem) {
                  return function(elem) {
                      elem = document.getElementsByClassName(elem);
                      if (!elem || elem.length == 0) { return null; }
                      elem = elem[0];
                      return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
                  }  
              },
          }
      })
      

      【讨论】:

      • 这正是我在我当前的项目中所做的,并且对于 OP 来说工作得很好。 +1
      猜你喜欢
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 2021-09-05
      • 2014-05-03
      相关资源
      最近更新 更多