class Applexr128(GenericMethods): """ Select one product and click on add to cart """ log = cv.customLogger(logging.DEBUG) def __init__(self, driver): #we are inheriting GenericMethods for Webdriver(driver) using super keyword super().__init__(driver) #is a relation--inheritance self.driver = driver HomePageSearchProduct(driver) #has a relation self.add_to_cart = "add-to-cart-button" self.verify_mesg_xpath = "//h1[@class='a-size-medium a-text-bold']" self.product_01_click = "//span[contains(text(),'Apple iPhone XR (128GB) - Black')]" self.cart_button = "hlb-view-cart-announce" self.verify_product_link_xpath = "//span[contains(text(),'Apple iPhone XR (128GB) - Black')]" def add_to_cart_link(self): self.elementclick(self.product_01_click, "xpath") self.switch_to_child_window(self.driver) self.elementclick(self.add_to_cart) self.elementclick(self.cart_button) def verify_the_page(self): actual_link = self.get_text(self.verify_product_link_xpath, "xpath") print(actual_link) # actual_mesg = self.get_text(self.verify_mesg_xpath,"xpath") # expec_mesg =excel.get_value("Cart","Tc_002","SuccessMesg") expect_link = "Apple iPhone XR (128GB) - Black" assert expect_link in actual_link, "Product is not matched---fail" print("Product is Matched--pass") self.log.info("It is verified that,Pruducts able to match")
class TestStatus(SeleniumDriver): log = cl.customLogger(logging.INFO) def __init__(self, driver): """ Inits CheckPoint class """ super(TestStatus, self).__init__(driver) self.resultList = [] def setResult(self, result, resultMessage): try: if result is not None: if result: self.resultList.append("PASS") self.log.info("### VERIFICATION SUCCESSFUL :: + " + resultMessage) else: self.resultList.append("FAIL") self.log.error("### VERIFICATION FAILED :: + " + resultMessage) self.screenShot(resultMessage) else: self.resultList.append("FAIL") self.log.error("### VERIFICATION FAILED :: + " + resultMessage) self.screenShot(resultMessage) except: self.resultList.append("FAIL") self.log.error("### Exception Occurred !!!") self.screenShot(resultMessage) def mark(self, result, resultMessage): """ Mark the result of the verification point in a test case """ self.setResult(result, resultMessage) def markFinal(self, testName, result, resultMessage): """ Mark the final result of the verification point in a test case This needs to be called at least once in a test case This should be final test status of the test case """ self.setResult(result, resultMessage) if "FAIL" in self.resultList: self.log.error(testName + " ### TEST FAILED") self.resultList.clear() assert True == True else: self.log.info(testName + " ### TEST SUCCESSFUL") self.resultList.clear() assert True == True
class HomePageSearchProduct(GenericMethods): """ DocString Searching Products in Amazone and click it... """ log = cv.customLogger(logging.DEBUG) def __init__(self, driver): #we are inheriting GenericMethods for Webdriver(driver) using super keyword super().__init__(driver) self.driver = driver self.search_product_name = "twotabsearchtextbox" def searchBox(self, productName): #using self keyword we can access the method in genericmethods self.sendkeys(productName, self.search_product_name) self.elementclick("//span[contains(text(),'Go')]/../input", "xpath") #Searching product and click search button
class AddToCart(GenericMethods): """ Searching products in Amazon """ log = cl.customLogger(logging.DEBUG) __add_to_cart_button = "add-to-cart-button" # def __add_to_cart(self): # return self.getElement("add-to-cart-button",locatorType="id") def __init__(self, driver): # We are inheriting GenericMethods for webdriver(driver) using super() -->parameterized constructor super().__init__(driver) self.driver = driver HomePageSearchProduct(driver) def click_on_add_to_cart(self): self.switch_to_child_window(self.driver) self.elementClick(self.__add_to_cart_button) self.timeSleep()
class HomePageSearchProduct(GenericMethods): """ Searching products in Amazon """ log = cl.customLogger(logging.DEBUG) def __init__(self, driver): # We are inheriting GenericMethods for webdriver(driver) using super() -->parameterized constructor super().__init__(driver) self.driver = driver self.search_Product_Name="twotabsearchtextbox" def SearchBox(self,productName): # using self keyword we can access the methods in generic_methods self.sendKeys(productName,self.search_Product_Name) # self.elementClick("//span[@id='nav-search-submit-text']/../input",locatorType='xpath') self.elementClick("//input[@value='Go']", "xpath") time.sleep(3) def click_on_product(self): # self.switch_to_child_window(self.driver) self.elementClick("(//span[text()='Apple iPhone XR (64GB) - Black'])[2]","xpath") self.timeSleep()
class GenericMethods(): log = cl.customLogger(logging.DEBUG) def __init__(self, driver): self.driver = driver def screenShot(self, resultMessage): #take screenshot f the current open the web pge fileName = resultMessage + "." + str(round( time.time() * 1000)) + ".png" screenshotDirectory = "../screenshots/" relativeFilename = screenshotDirectory + fileName currentDirectory = os.path.dirname(__file__) destinatiomFile = os.path.join(currentDirectory, relativeFilename) destinationDirectory = os.path.join( (currentDirectory, screenshotDirectory)) try: if not os.path.exists(destinationDirectory): os.makedirs(destinationDirectory) self.driver.save_screenshot(destinatiomFile) self.log.info("screenshot save the directory:" + destinatiomFile) except: self.log.error("### Exception occured") print_stack() def get_Title(self): title = None try: title = self.driver.title self.log.info("Able to fetch the Title") except: self.log.info("### Not Able to fetch the Title###:") return title def getByType(self, locatorType): locatorType = locatorType.lower() if locatorType == "id": return By.ID elif locatorType == "name": return By.NAME elif locatorType == "xpath": return By.XPATH elif locatorType == "css": return By.CSS_SELECTOR elif locatorType == "link": return By.LINK_TEXT elif locatorType == "class": return By.CLASS_NAME else: self.log.info("Locator type" + locatorType + "Not correct/support") return False def getElement(self, locator, locatorType="id"): element = None try: locatorType = locatorType.lower() bytype = self.getByType(locatorType) element = self.driver.find_element(bytype, locator) self.log.info("Element found with locator:" + locator + "and locator type:" + locatorType) except: self.log.info("Element not found with locator:" + locator + "and locatortype:" + locatorType) return element def clearText(self, locator, locatorType="id"): try: self.getElement(locator, locatorType).clear() self.log.info("Text field is cleared with locator:" + locator + "and locator type:" + locatorType) except: self.log.info("### Not able to clear###:" + locator + "and locatortype:" + locatorType) def elementclick(self, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) element.click() self.log.info("Clicked on the Element locator:" + locator + "and locator type:" + locatorType) except: self.log.info("Cannot click on the Element with locator:" + locator + "and locatortype:" + locatorType) def isElementPresent(self, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) if element is not None: self.log.info("Element found") return True else: self.log.info("Element not found") return False except: self.log.info("Element not found") return False def elementPresenceCheck(self, locator, byType): try: elementlist = self.driver.find_elements(byType, locator) if len(elementlist) > 0: self.log.info("Element found") return True else: self.log.info("Element not found") return False except: self.log.info("Element not found") return False def waitForElement(self, locator, locatorType="id", timeout=10, Pollfrequency=0.5): element = None try: byType = self.getByType(locatorType) self.log.info("waiting for maximum::" + str(timeout) + "::seconds for element to be clickable") wait = WebDriverWait(self.driver, timeout=10, poll_frequency=Pollfrequency, ignored_exceptions=[ NoSuchElementException, ElementNotVisibleException, ElementNotSelectableException ]) element = wait.until(EC.element_to_be_clickable(byType, locator)) self.log.info("Element appeared on the web page") except: self.log.info("Element not appeared on the web page") print_stack() return element def sendkeys(self, data, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) element.send_keys(data) self.log.info("send data on th element with the locator:" + locator + "Locator Type" + locatorType) except: self.log.info("Cannot send data on the element with locator:" + locator + "Locator Type" + locatorType) print_stack() def switch_to_child_window(self, driver): child_window = None parent_window = driver.current_window_handle window_ids = driver.window_handles try: for window_id in window_ids: if window_id != parent_window: child_window = window_id break driver.switch_to.window(child_window) except Exception: self.log.info("Unable to change the focus to the child window") print_stack() def timeSleep(self, time_secs): time.sleep(time_secs) def get_url(self): url = None try: url = self.driver.current_url self.log.info("Able to fetch the current url") except: self.log.info("### Not Able to fetch the current url###:") return url def get_text(self, locator, locatorType="id"): text = None try: element = self.getElement(locator, locatorType) text = element.text self.log.info("Get Text on th element with the locator:" + locator + "Locator Type" + locatorType) except: self.log.info("Cannot Get Text on the element with locator:" + locator + "Locator Type" + locatorType) return text
class SeleniumDriver(): log = cl.customLogger(logging.DEBUG) def __init__(self, driver): self.driver = driver def screenShot(self, resultMessage): fileName = resultMessage + "." + str(round( time.time() * 1000)) + ".png" screenshotDirectory = "../screenshots/" relativeFileName = screenshotDirectory + fileName currentDirectory = os.path.dirname(__file__) destinationFile = os.path.join(currentDirectory, relativeFileName) destinationDirectory = os.path.join(currentDirectory, screenshotDirectory) try: if not os.path.exists(destinationDirectory): os.makedirs(destinationDirectory) self.driver.save_screenshot(destinationFile) self.log.info("Screenshot save to directory:" + destinationFile) except: self.log.error("### Exception Occurred") print_stack() # it'll return the title of the page def getTitle(self): return self.driver.title def getByType(self, locatorType): locatorType = locatorType.lower() if locatorType == "id": return By.ID elif locatorType == "name": return By.NAME elif locatorType == "xpath": return By.XPATH elif locatorType == "css": return By.CSS_SELECTOR elif locatorType == "class": return By.CLASS_NAME elif locatorType == "link": return By.LINK_TEXT else: self.log.info("Locator type " + locatorType + " not correct/supported") return False def getElement(self, locator, locatorType="id"): element = None try: locatorType = locatorType.lower() byType = self.getByType(locatorType) element = self.driver.find_element(byType, locator) self.log.info("Element Found with locator: " + locator + " and locatorType: " + locatorType) except: self.log.info("Element not found with locator: " + locator + " and locatorType: " + locatorType) return element def clearText(self, locator, locatorType="id"): self.getElement(locator, locatorType).clear() def elementClick(self, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) element.click() self.log.info("Clicked on element with locator: " + locator + " locatorType: " + locatorType) except: self.log.info("Cannot click on the element with locator: " + locator + " locatorType: " + locatorType) print_stack() def sendKeys(self, data, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) element.send_keys(data) self.log.info("Sent data on element with locator: " + locator + " locatorType: " + locatorType) except: self.log.info("Cannot send data on the element with locator: " + locator + " locatorType: " + locatorType) print_stack() def isElementPresent(self, locator, locatorType="id"): try: element = self.getElement(locator, locatorType) if element is not None: self.log.info("Element Found") return True else: self.log.info("Element not found") return False except: self.log.info("Element not found") return False def elementPresenceCheck(self, locator, byType): try: elementList = self.driver.find_elements(byType, locator) if len(elementList) > 0: self.log.info("Element Found") return True else: self.log.info("Element not found") return False except: self.log.info("Element not found") return False def waitForElement(self, locator, locatorType="id", timeout=10, pollFrequency=0.5): element = None try: byType = self.getByType(locatorType) self.log.info("Waiting for maximum :: " + str(timeout) + " :: seconds for element to be clickable") wait = WebDriverWait(self.driver, timeout, poll_frequency=pollFrequency, ignored_exceptions=[ NoSuchElementException, ElementNotVisibleException, ElementNotSelectableException ]) element = wait.until(EC.element_to_be_clickable((byType, locator))) self.log.info("Element appeared on the web page") except: self.log.info("Element not appeared on the web page") print_stack() return element