class RoyalQueenSeedsMain: """RoyalQueenSeedsMain class lands in the website and it has all the navigation functions.""" website = 'https://www.royalqueenseeds.com/' AGE_CONTROL = (By.CSS_SELECTOR, '.button.enter-btn') HEADER_LOGIN = (By.XPATH, '//a[@title="Your Account"]') LOGIN_BUTTON = (By.XPATH, '//a[@title="Log in"]') HOMEPAGE_CONTROL = (By.ID, 'slide-holder') HEADER_CATS_CONTAINER = (By.XPATH, '//a[contains(@class, "cat_")]') CART_PAGE = (By.XPATH, '//a[contains(@class, "top-cart-checkout")]' ) # '//*[@title="Check out"]' def __init__(self, driver): self.driver = driver self.methods = BaseClass(self.driver) def navigate_to_home_page(self): """ Navigates to the homepage and checks it """ self.driver.get(self.website) self.methods.wait_for_element(self.AGE_CONTROL).click() home_page_loaded = self.methods.element_exists(self.HOMEPAGE_CONTROL) assert home_page_loaded, True def navigate_to_login_page(self): """ Navigates to the login page """ try: self.methods.wait_for_element(self.HEADER_LOGIN) self.methods.hover(self.HEADER_LOGIN) self.methods.wait_for_element(self.LOGIN_BUTTON).click() except TimeoutException: self.methods.hover(self.HEADER_LOGIN) self.methods.wait_for_element(self.LOGIN_BUTTON).click() sleep(2) def navigate_to_random_category_page(self): """ Navigates to a random category page """ sleep(2) categories = self.driver.find_elements(*self.HEADER_CATS_CONTAINER) random_value = self.methods.random_number(0, len(categories) - 1) random_category = categories[random_value] random_category.click() def navigate_to_cart_page(self): """ Navigates to the cart page """ self.methods.wait_for_element(self.CART_PAGE).click()
class RoyalQueenSeedsCart: """Navigating to checkout page and delete items from cart""" CHECKOUT_BUTTON = (By.CSS_SELECTOR, '#shoppingCartCheckout') DELETE_ITEM = (By.XPATH, '//img[@class= "icon"]') EMPTY_CART = (By.ID, 'emptyCartWarning') BACK_TO_CART = (By.ID, 'cart-buttons') BELOW_MINIMUM_ORDER_AMOUNT = (By.XPATH, '//a[@class="back"][@title= "Previous"]') SCROLL_TO_TOP = (By.TAG_NAME, 'body') def __init__(self, driver): self.driver = driver self.methods = BaseClass(self.driver) def navigate_to_checkout_page(self): """ Navigates the checkout page then come back to the cart page """ self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight);") self.methods.wait_for_element(self.CHECKOUT_BUTTON).click() if self.methods.element_exists(self.BELOW_MINIMUM_ORDER_AMOUNT): try: self.methods.wait_for_element(self.BACK_TO_CART).click() except TimeoutException: self.driver.execute_script("window.history.go(-1)") else: self.driver.execute_script("window.history.go(-1)") def delete_items_from_cart(self): """ Deletes product from the cart page and check if it is any left. If it is exist, deletes that one too """ self.methods.wait_for_element(self.SCROLL_TO_TOP).send_keys(Keys.CONTROL + Keys.HOME) if self.methods.element_exists(self.DELETE_ITEM): try: self.methods.wait_for_element(self.DELETE_ITEM).click() assert self.methods.wait_for_element(self.EMPTY_CART).is_displayed, True except TimeoutException or AssertionError: self.delete_items_from_cart() else: assert self.methods.wait_for_element(self.EMPTY_CART).is_displayed
class RoyalQueenSeedsProduct: """Product page to select size and add to cart.""" NUMBER_SEEDS_CONTAINER = (By.CSS_SELECTOR, '.attributes-block') ADD_TO_CART = (By.CSS_SELECTOR, '#add_to_cart') ADDED_TO_CART = (By.ID, 'cart_block_total') PRODUCT_STOCK = (By.ID, 'availability_value') POPUP_CLOSE_BUTTON = (By.XPATH, '') OUT_OF_STOCK = (By.CSS_SELECTOR, '#availability_value') def __init__(self, driver): self.driver = driver self.methods = BaseClass(self.driver) # def select_size(self): # """ # Selects product size # # """ # if self.methods.element_exists(self.NONE_SIZE): # self.methods.wait_for_element(self.AVAILABLE_SIZE).click() def check_stock_info(self): """ Search for stock info if the product is not in stock returns False """ availability = self.methods.presence_of_element_located(self.OUT_OF_STOCK).text if availability.lower() != 'no stock': return True else: return False def add_product_to_cart(self): """ Adds product to the cart page and check is it added successfully """ sleep(1) cart_total = self.methods.presence_of_element_located(self.ADDED_TO_CART).text self.methods.wait_for_element(self.ADD_TO_CART).click() if self.methods.element_exists(self.ADDED_TO_CART): try: sleep(1) successfully_added = self.methods.presence_of_element_located(self.ADDED_TO_CART).text assert cart_total != successfully_added # change assertion if any bug appears except AssertionError: self.add_product_to_cart() else: self.add_product_to_cart()