common.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from selenium.common.exceptions import NoSuchElementException
  2. import time
  3. import logging
  4. import sys, traceback
  5. import os
  6. from pathlib import Path
  7. class Validation:
  8. def __init__(self):
  9. self.logger = logging.getLogger(__name__)
  10. def validate_element_by_id(self, browser, id):
  11. self.logger.debug( "Buscando elemento: " + id)
  12. ready = False
  13. while (not ready):
  14. try:
  15. browser.find_element_by_id(id)
  16. self.logger.debug("El elmento con id {0} esta listo".format(id))
  17. ready = True
  18. except:
  19. self.logger.debug("El elmento con id {0} no esta listo".format(id))
  20. ready = False
  21. time.sleep(0.25)
  22. return ready
  23. def validate_element_by_tag_name(self, browser, tag):
  24. self.logger.debug( "Buscando elemento: " + tag)
  25. ready = False
  26. while (not ready):
  27. try:
  28. browser.find_element_by_tag_name(tag)
  29. self.logger.debug("El elmento con tag name {0} esta listo".format(tag))
  30. ready = True
  31. except:
  32. self.logger.debug("El elmento con tag name {0} no esta listo".format(tag))
  33. ready = False
  34. time.sleep(0.25)
  35. return ready
  36. def validate_element_not_present(self, browser, id):
  37. self.logger.debug("Buscando elemento: " + id)
  38. ready = False
  39. while not ready:
  40. try:
  41. browser.find_element_by_id(id)
  42. self.logger.debug("El elmento con id {0} esta presente".format(id))
  43. ready = False
  44. except NoSuchElementException:
  45. self.logger.debug("El elmento con id {0} no esta presente".format(id))
  46. ready = True
  47. time.sleep(0.25)
  48. return ready
  49. def check_element_by_id(self, driver, id):
  50. try:
  51. driver.find_element_by_id(id)
  52. except NoSuchElementException:
  53. return False
  54. return True
  55. def validate_download(self, filename):
  56. self.logger.debug( "Se descargara el archivo: " + filename.split("\\")[-1])
  57. my_file = Path(filename)
  58. cont = 0 #Controla la cantidad de intentos para descargar el archivo
  59. result=False
  60. listo = False
  61. while (not listo):
  62. try:
  63. if my_file.exists():
  64. self.logger.debug("Archivo descargado.!")
  65. listo = True
  66. result=True
  67. else:
  68. self.logger.debug("Descargando ...")
  69. listo = False
  70. except:
  71. self.logger.debug("Descargando ...")
  72. listo = False
  73. traceback.print_exc(file=sys.stdout)
  74. if cont == 120:
  75. self.logger.debug("Se agoto el tiempo de espera para validar la descargar.")
  76. listo = True
  77. result=False
  78. cont = cont+1
  79. time.sleep(0.5)
  80. return result