Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. from selenium.webdriver.common.keys import Keys
  8.  
  9. import os, sys, inspect
  10. # fetch path to the directory in which current file is, from root directory or C:\ (or whatever driver number it is)
  11. currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  12. # extract the path to parent directory
  13. parentdir = os.path.dirname(currentdir)
  14. # insert path to the folder from parent directory from which the python module/ file is to be imported
  15. sys.path.insert(0, parentdir)
  16.  
  17. from Locators import Locators
  18. from TestData import TestData
  19.  
  20. class BasePage():
  21. """This class is the parent class for all the pages in our application."""
  22. """It contains all common elements and functionalities available to all pages."""
  23.  
  24. # this function is called every time a new object of the base class is created.
  25. def __init__(self, driver):
  26. self.driver=driver
  27.  
  28. # this function performs click on web element whose locator is passed to it.
  29. def click(self, by_locator):
  30. WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator)).click()
  31.  
  32. # this function asserts comparison of a web element's text with passed in text.
  33. def assert_element_text(self, by_locator, element_text):
  34. web_element=WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
  35. assert web_element.text == element_text
  36.  
  37. # this function performs text entry of the passed in text, in a web element whose locator is passed to it.
  38. def enter_text(self, by_locator, text):
  39. return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator)).send_keys(text)
  40.  
  41. # this function checks if the web element whose locator has been passed to it, is enabled or not and returns
  42. # web element if it is enabled.
  43. def is_enabled(self, by_locator):
  44. return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
  45.  
  46. # this function checks if the web element whose locator has been passed to it, is visible or not and returns
  47. # true or false depending upon its visibility.
  48. def is_visible(self,by_locator):
  49. element=WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
  50. return bool(element)
  51.  
  52. # this function moves the mouse pointer over a web element whose locator has been passed to it.
  53. def hover_to(self, by_locator):
  54. element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
  55. ActionChains(self.driver).move_to_element(element).perform()
  56.  
  57. class HomePage(BasePage):
  58. """Home Page of Amazon India"""
  59. def __init__(self, driver):
  60. super().__init__(driver)
  61. self.driver.get(TestData.BASE_URL)
  62.  
  63. def search(self):
  64. self.driver.find_element(*Locators.SEARCH_TEXTBOX).clear()
  65. self.enter_text(Locators.SEARCH_TEXTBOX, TestData.SEARCH_TERM)
  66. self.click(Locators.SEARCH_SUBMIT_BUTTON)
  67.  
  68. class SearchResultsPage(BasePage):
  69. """Search Results Page of Amazon India"""
  70. def __init__(self, driver):
  71. super().__init__(driver)
  72.  
  73. def click_search_result(self):
  74. self.click(Locators.SEARCH_RESULT_LINK)
  75.  
  76. class ProductDetailsPage(BasePage):
  77. """Product Details Page for the clicked product on Amazon India"""
  78. def __init__(self,driver):
  79. super().__init__(driver)
  80.  
  81. def click_add_to_cart_button(self):
  82. self.click(Locators.ADD_TO_CART_BUTTON)
  83.  
  84. class SubCartPage(BasePage):
  85. """Sub Cart Page on Amazon India"""
  86. def __init__(self,driver):
  87. super().__init__(driver)
  88.  
  89. def click_cart_link(self):
  90. self.click(Locators.CART_LINK)
  91.  
  92. class CartPage(BasePage):
  93. """Cart Page on Amazon India"""
  94. def __init__(self,driver):
  95. super().__init__(driver)
  96.  
  97. def delete_item(self):
  98. cartCount=int(self.driver.find_element(*Locators.CART_COUNT).text)
  99. # print ("Cart Count is"+ str(cartCount))
  100. if(cartCount<1):
  101. print("Cart is empty")
  102. exit()
  103. if(self.driver.title.startswith("Amazon.in Shopping Cart")):
  104. #to delete an item from the Cart
  105. self.click(Locators.DELETE_ITEM_LINK)
  106. time.sleep(2)
  107.  
  108. def click_proceed_to_checkout_button(self):
  109. self.click(Locators.PROCEED_TO_CHECKOUT_BUTTON)
  110.  
  111. class SignInPage(BasePage):
  112. """SignIn Page on Amazon India"""
  113. def __init__(self,driver):
  114. super().__init__(driver)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement