validar validacion pagina funcion formularios formulario enviar ejemplos ejecutar despues con cargar carga asincrona antes javascript java selenium selenium-webdriver integration-testing

validacion - validar formulario javascript antes de enviar



Averigüe qué recursos no se cargan con éxito con Selenio (3)

Tengo que probar una aplicación con Selenium. La aplicación contiene contenido externo como anuncios. En mi prueba, tengo varias veces que esperar hasta que se cargue el documento. Esto se ve así:

private static final String DOCUMENT_READY_STATE_COMPLETE = "complete"; protected void waitUntilDocumentLoaded() { wait.until(input -> getDocumentReadyState().equals(DOCUMENT_READY_STATE_COMPLETE)); } private String getDocumentReadyState() { return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString(); }

Ocurre a veces, que el navegador aún está cargando algún recurso. Por lo que tengo entendido, a veces podría aceptar si readyState es interactivo en lugar de completo . Por ejemplo, si algunos anuncios no se cargan a tiempo, mi prueba no es completamente interesante.

¿Es posible de alguna manera obtener una lista de recursos con URLs que aún no se han cargado? Luego podría construir una afirmación que verifique qué recursos son obligatorios para la prueba y cuáles no.

Uso Selenium Java WebDriver 2.53.1 con Firefox 46 en Linux.


  1. Espere a que elementos específicos estén presentes
  2. Una solución alternativa es usar el proxy browsermob o smthn similar, en el caso de browsermob hay un método que le permite esperar a que el tráfico de la red se detenga y esperar durante un cierto período de tiempo para asegurarse de que el navegador no realiza ninguna llamada adicional.

Así que esto es lo que hice yo mismo. Esto puede detectar las imágenes, que no están cargadas. Usé los servicios web geniales http://www.deelay.me y https://placekitten.com . Entonces, al menos para las imágenes, tengo algo.

<html> <body> <img src="http://deelay.me/1000/https://placekitten.com/g/500/500"/> <img src="http://deelay.me/10000/https://placekitten.com/g/500/501"/> <script> function isImageOk(img) { //Function taken from http://.com/a/1977898/337621 // During the onload event, IE correctly identifies any images that // weren’t downloaded as not complete. Others should too. Gecko-based // browsers act like NS4 in that they report this incorrectly. if (!img.complete) { return false; } // However, they do have two very useful properties: naturalWidth and // naturalHeight. These give the true size of the image. If it failed // to load, either of these should be zero. if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) { return false; } // No other way of checking: assume it’s ok. return true; } function checkState(){ var documentState = document.readyState; if ( documentState != "complete") { console.log("Document is still having readystate " + documentState + ". Pending images: " ); var images = document.getElementsByTagName("img"); for(i = 0;i < images.length; i++) { if ( !isImageOk( images[i] ) ) { console.log("Image URL: " + images[i].src ); } } setTimeout(checkState,500); } else { console.log("Document is loaded successfully."); } } checkState(); </script> </body> </html>

Salida de la consola:

Document is still having readystate loading. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500 Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is still having readystate interactive. Pending images: Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501 Document is loaded successfully.


Puedo compartir 3 enfoques que pueden verificar si el elemento está presente o no. Espero que al usar estos métodos, puedas obtener calma.

Enfoque n. ° 1:

/** * Checks if is element present or not. * * @param element * @return true, if is element present, otherwise return false. * @throws Exception */ public boolean isElementPresent(WebElement element) throws Exception { try { waitForElement(element, Integer.valueOf(PropertyLoader .loadProperty("implicit_timeout_second"))); return true; } catch (NoSuchElementException e) { return false; } }

Enfoque # 2:

/** * waits for element to be present. * * @param element * @param timeOut * The timeout in seconds when an expectation is called * @return */ public WebElement waitForElement(WebElement element, int timeOut) { WebDriverWait wait = new WebDriverWait(driverW, timeOut); wait.until(ExpectedConditions.elementToBeClickable(element)); return element; }

Enfoque n. ° 3:

/** * Waits for the element to be present BY. * * @param dinamic * element * @return true, if is element present, otherwise return false. * @throws Exception */ protected boolean waitForDinamicElementPresent(By by) throws IOException { try { WebDriverWait wait = new WebDriverWait(driverW, Integer.valueOf(PropertyLoader .loadProperty("implicit_call_timeout_second"))); wait.until(ExpectedConditions.elementToBeClickable(by)); return true; } catch (NoSuchElementException e) { return false; } }