Advertisement
Jamesxxx1997

Untitled

Dec 26th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. from selenium.webdriver.common.by import By
  2. from traceback import print_stack
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.common.exceptions import *
  6.  
  7. class SeleniumDriver():
  8.  
  9. def __init__(self, driver):
  10. self.driver = driver
  11.  
  12. def getByType(self, locatorType):
  13. locatorType = locatorType.lower()
  14. if locatorType == "id":
  15. return By.ID
  16. elif locatorType == "name":
  17. return By.NAME
  18. elif locatorType == "xpath":
  19. return By.XPATH
  20. elif locatorType == "css":
  21. return By.CSS_SELECTOR
  22. elif locatorType == "class":
  23. return By.CLASS_NAME
  24. elif locatorType == "link":
  25. return By.LINK_TEXT
  26. else:
  27. print("Locator type " + locatorType + " not correct/supported")
  28. return False
  29.  
  30. def getElement(self, locator, locatorType="id"):
  31. element = None
  32. try:
  33. locatorType = locatorType.lower()
  34. byType = self.getByType(locatorType)
  35. element = self.driver.find_element(byType, locator)
  36. print("Element Found with locator: " + locator + " and locatorType: " + locatorType)
  37. except:
  38. print("Element not found with locator: " + locator + " and locatorType: " + locatorType)
  39. return element
  40.  
  41. def elementClick(self, locator, locatorType="id"):
  42. try:
  43. element = self.getElement(locator, locatorType)
  44. element.click()
  45. print("Clicked on element with locator: " + locator + " locatorType: " + locatorType)
  46. except:
  47. print("Cannot click on the element with locator: " + locator + " locatorType: " + locatorType)
  48. print_stack()
  49.  
  50. def sendKeys(self, data, locator, locatorType="id"):
  51. try:
  52. element = self.getElement(locator, locatorType)
  53. element.send_keys(data)
  54. print("Sent data on element with locator: " + locator + " locatorType: " + locatorType)
  55. except:
  56. print("Cannot send data on the element with locator: " + locator +
  57. " locatorType: " + locatorType)
  58. print_stack()
  59.  
  60. def isElementPresent(self, locator, locatorType="id"):
  61. try:
  62. element = self.getElement(locator, locatorType)
  63. if element is not None:
  64. print("Element Found")
  65. return True
  66. else:
  67. print("Element not found")
  68. return False
  69. except:
  70. print("Element not found")
  71. return False
  72.  
  73. def elementPresenceCheck(self, locator, byType):
  74. try:
  75. elementList = self.driver.find_elements(byType, locator)
  76. if len(elementList) > 0:
  77. print("Element Found")
  78. return True
  79. else:
  80. print("Element not found")
  81. return False
  82. except:
  83. print("Element not found")
  84. return False
  85.  
  86. def waitForElement(self, locator, locatorType="id",
  87. timeout=10, pollFrequency=0.5):
  88. element = None
  89. try:
  90. byType = self.getByType(locatorType)
  91. print("Waiting for maximum :: " + str(timeout) +
  92. " :: seconds for element to be clickable")
  93. wait = WebDriverWait(self.driver, 10, poll_frequency=1,
  94. ignored_exceptions=[NoSuchElementException,
  95. ElementNotVisibleException,
  96. ElementNotSelectableException])
  97. element = wait.until(EC.element_to_be_clickable((byType,
  98. "stopFilter_stops-0")))
  99. print("Element appeared on the web page")
  100. except:
  101. print("Element not appeared on the web page")
  102. print_stack()
  103. return element
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement