common.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. cont = 0 #Controla la cantidad de intentos para descargar el archivo
  13. result=False
  14. ready = False
  15. while (not ready):
  16. try:
  17. browser.find_element_by_id(id)
  18. self.logger.debug("El elmento con id {0} esta listo".format(id))
  19. ready = True
  20. result = True
  21. except:
  22. self.logger.debug("El elmento con id {0} no esta listo".format(id))
  23. ready = False
  24. if cont == 120:
  25. self.logger.debug("Se agoto el tiempo de espera para validar la descargar.")
  26. ready = True
  27. result=False
  28. cont = cont+1
  29. time.sleep(0.25)
  30. return result
  31. def validate_element_by_tag_name(self, browser, tag):
  32. self.logger.debug( "Buscando elemento: " + tag)
  33. ready = False
  34. while (not ready):
  35. try:
  36. browser.find_element_by_tag_name(tag)
  37. self.logger.debug("El elmento con tag name {0} esta listo".format(tag))
  38. ready = True
  39. except:
  40. self.logger.debug("El elmento con tag name {0} no esta listo".format(tag))
  41. ready = False
  42. time.sleep(0.25)
  43. return ready
  44. def validate_element_not_present(self, browser, id):
  45. self.logger.debug("Buscando elemento: " + id)
  46. ready = False
  47. while not ready:
  48. try:
  49. browser.find_element_by_id(id)
  50. self.logger.debug("El elmento con id {0} esta presente".format(id))
  51. ready = False
  52. except NoSuchElementException:
  53. self.logger.debug("El elmento con id {0} no esta presente".format(id))
  54. ready = True
  55. time.sleep(0.25)
  56. return ready
  57. def check_element_by_id(self, driver, id):
  58. try:
  59. driver.find_element_by_id(id)
  60. except NoSuchElementException:
  61. return False
  62. return True
  63. def validate_download(self, filename):
  64. self.logger.debug( "Se descargara el archivo: " + filename.split("\\")[-1])
  65. my_file = Path(filename)
  66. cont = 0 #Controla la cantidad de intentos para descargar el archivo
  67. result=False
  68. listo = False
  69. while (not listo):
  70. try:
  71. if my_file.exists():
  72. self.logger.debug("Archivo descargado.!")
  73. listo = True
  74. result=True
  75. else:
  76. self.logger.debug("Descargando ...")
  77. listo = False
  78. except:
  79. self.logger.debug("Descargando ...")
  80. listo = False
  81. traceback.print_exc(file=sys.stdout)
  82. if cont == 120:
  83. self.logger.debug("Se agoto el tiempo de espera para validar la descargar.")
  84. listo = True
  85. result=False
  86. cont = cont+1
  87. time.sleep(0.5)
  88. return result