Стажер: опрос до тех пор, пока элемент не станет видимым

Есть ли способ в Intern, который я могу опрашивать, пока элемент не станет видимым? Многие элементы на моем веб-сайте находятся в dom, но скрыты, поэтому каждый раз, когда я выполняю «поиск» элемента X после того, как он должен появиться, он терпит неудачу, потому что элемент явно нарушает один из видимых атрибутов, которые проверяет селен.

Я пробовал вспомогательную функцию "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;
};  

person Jon    schedule 16.09.2015    source источник


Ответы (2)


Посетите 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'
person MinimalMaximizer    schedule 17.09.2015

Я постоянно сталкиваюсь с этой проблемой, и мы использовали функцию 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;
            }  
        },
    }
})
person kgstew    schedule 14.10.2015
comment
Это именно то, что я делаю в своем текущем проекте, и это отлично работает для целей ОП. +1 - person Guilherme Fujiyoshi; 16.10.2015