2016-03-18 18 views
6

Come posso verificare fino a che un elemento non è selezionabile con l'orologio notturno js? Voglio fare clic su un elemento ma quando eseguo nightwatch, il selenio non fa clic sull'elemento perché non è ancora cliccabile.Come posso verificare fino a che un elemento non è selezionabile con nightwatchjs?

+0

Cosa intendi cliccabile? L'elemento è disabilitato? È nascosto? –

+0

Mi piacerebbe vedere un esempio che sarebbe l'equivalente di 'waitUntilElementIsEnabled()'. C'è un helper in selenio che combina un controllo per isEnabled con un controllo per isVisible prima di restituire 'true'. – Monkpit

+0

Vedere [questa ricerca sul Github di Selenium] (https://github.com/SeleniumHQ/selenium/search?l=python&q=clickable&type=Code&utf8=%E2%9C%93) per gli esempi – Monkpit

risposta

1

Puoi mostrare un elemento di esempio, di solito dovrebbe esserci un nome di attributo "disabilitato" se il pulsante non è cliccabile, questo dovrebbe funzionare.

browser.assert.attributeEquals(yourCSS, 'disabled', true) 
+0

Mi piacerebbe vedere un esempio questo sarebbe l'equivalente di 'waitUntilElementIsEnabled()'. C'è un helper in selenio che combina un controllo per isEnabled con un controllo per isVisible prima di restituire 'true'. – Monkpit

10

Qualcosa di simile dovrebbe funzionare. Fatemi sapere se avete domande

var util = require('util'); 
var events = require('events'); 

/* 
* This custom command allows us to locate an HTML element on the page and then wait until the element is both visible 
* and does not have a "disabled" state. It rechecks the element state every 500ms until either it evaluates to true or 
* it reaches maxTimeInMilliseconds (which fails the test). Nightwatch uses the Node.js EventEmitter pattern to handle 
* asynchronous code so this command is also an EventEmitter. 
*/ 

function WaitUntilElementIsClickable() { 
    events.EventEmitter.call(this); 
    this.startTimeInMilliseconds = null; 
} 

util.inherits(WaitUntilElementIsClickable, events.EventEmitter); 

WaitUntilElementIsClickable.prototype.command = function (element, timeoutInMilliseconds) { 
    this.startTimeInMilliseconds = new Date().getTime(); 
    var self = this; 
    var message; 

    if (typeof timeoutInMilliseconds !== 'number') { 
    timeoutInMilliseconds = this.api.globals.waitForConditionTimeout; 
    } 

    this.check(element, function (result, loadedTimeInMilliseconds) { 
    if (result) { 
     message = '@' + element + ' was clickable after ' + (loadedTimeInMilliseconds - self.startTimeInMilliseconds) + ' ms.'; 
    } else { 
     message = '@' + element + ' was still not clickable after ' + timeoutInMilliseconds + ' ms.'; 
    } 
    self.client.assertion(result, 'not visible or disabled', 'visible and not disabled', message, true); 
    self.emit('complete'); 
    }, timeoutInMilliseconds); 

    return this; 
}; 

WaitUntilElementIsClickable.prototype.check = function (element, callback, maxTimeInMilliseconds) { 
    var self = this; 

    var promises =[]; 
    promises.push(new Promise(function(resolve) { 
    self.api.isVisible(element, function(result) { 
     resolve(result.status === 0 && result.value === true); 
    }); 
    })); 

    promises.push(new Promise(function(resolve) { 
    self.api.getAttribute(element, 'disabled', function (result) { 
     resolve(result.status === 0 && result.value === null); 
    }); 
    })); 

    Promise.all(promises) 
    .then(function(results) { 
     var now = new Date().getTime(); 
     const visibleAndNotDisabled = !!results[0] && !!results[1]; 
     if (visibleAndNotDisabled) { 
     callback(true, now); 
     } else if (now - self.startTimeInMilliseconds < maxTimeInMilliseconds) { 
     setTimeout(function() { 
      self.check(element, callback, maxTimeInMilliseconds); 
     }, 500); 
     } else { 
     callback(false); 
     } 
    }) 
    .catch(function(error) { 
     setTimeout(function() { 
     self.check(element, callback, maxTimeInMilliseconds); 
     }, 500); 
    }); 
}; 

module.exports = WaitUntilElementIsClickable; 

Aggiungi questo codice come file alla tua cartella dei comandi. Dovrebbe essere chiamato waitUntilElementIsClickable.js o come vuoi che sia il tuo comando.

L'uso è:

browser.waitUntilElementIsClickable('.some.css'); 

È inoltre possibile utilizzare gli elementi della pagina:

var page = browser.page.somePage(); 
page.waitUntilElementIsClickable('@someElement'); 
+0

Ehi, stavo cercando il tuo codice, ma è dare errore di sintassi '' 'waitForElementClickable.js: 57 Then ((risultati) => { ^^ SyntaxError: token imprevisto => ' '' esserle –

+0

Ahh, il codice è parzialmente scritto in formato ES6, l'ho aggiornato in modo che potesse funzionare con ES5 puro –

+0

Grazie per la tua risposta rapida, ora funziona. –

Problemi correlati