class DecathlonsgCategoryPage: """DecathlonsgCategory is selecting random product from category page .""" PRODUCT_CONTAINER = (By.ID, 'products') PRODUCT_LIST = (By.CSS_SELECTOR, '.thumbnail.product-thumbnail') def __init__(self, driver): self.driver = driver self.methods = BaseClass(self.driver) def selects_random_product(self): """ Select random products from product list. """ sleep(2) self.methods.wait_for_element(self.PRODUCT_CONTAINER) self.methods.exist_element(self.PRODUCT_LIST, multiple=True) products = self.driver.find_elements(*self.PRODUCT_LIST) if len(products) > 7: random_int = self.methods.random_number(1, 6) random_product_url = products[random_int].get_attribute('href') random_product_url = random_product_url.split('html') random_product_url = random_product_url[0] + 'html' self.driver.get(random_product_url) else: random_int = self.methods.random_number(1, len(products) - 1) random_product_url = products[random_int].get_attribute('href') random_product_url = random_product_url.split('html') random_product_url = random_product_url[0] + 'html' self.driver.get(random_product_url)
class DecathlonsgMain: """DecathlonsgMain class lands in the website and it has all the navigation functions.""" website = 'https://www.decathlon.sg/' HOMEPAGE_CONTROL = (By.CSS_SELECTOR, '.elementor-container.elementor-column-gap-default') LOGIN_HEADER = (By.CSS_SELECTOR, '#header-user-btn') LOGIN_BUTTON = (By.CSS_SELECTOR, '.elementor-button-text') SECOND_LOGIN_BUTTON = (By.CSS_SELECTOR, 'elementor-button-content-wrapper') MAIN_CATS_CONTAINER_HEADER = (By.XPATH, '//*[contains(@class, "cbp-has-submeu")]') SUB_CATS_CONTAINER_HEADER = (By.XPATH, '//*[contains(@class, "cbp-category-title")]') CART_HEADER = (By.ID, 'button_cart_cart') 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) home_page_loaded = self.methods.exist_element(self.HOMEPAGE_CONTROL) assert home_page_loaded, True def navigate_to_login_page(self): """ Navigates to the login page """ sleep(3) try: self.methods.hover_element(self.LOGIN_HEADER) self.methods.wait_for_element(self.LOGIN_BUTTON).click() except (TimeoutException, ElementClickInterceptedException): self.navigate_to_login_page() def navigate_to_random_category_page(self): """ Navigates to a random category page """ self.methods.exist_element(self.SUB_CATS_CONTAINER_HEADER, multiple=True) main_categories = self.driver.find_elements(*self.MAIN_CATS_CONTAINER_HEADER) sub_categories = self.driver.find_elements(*self.SUB_CATS_CONTAINER_HEADER) self.methods.select_random_category(sub_categories, main_categories) def navigate_to_cart_page(self): """ Navigates to the cart page. if self.methods.exist_element(DecathlonsgProductPage.IFRAME_IS_OPENED): iframe_name = self.methods.presence_of_element_located(self.IFRAME).get_attribute('name') self.driver.switch_to.frame(iframe_name) self.methods.wait_for_element(DecathlonsgProductPage.CLOSE_POPUP_BTN).click() self.driver.switch_to.default_content() self.methods.wait_for_element(self.CART_HEADER).click() else: self.methods.wait_for_element(self.CART_HEADER).click() """ self.driver.get("https://www.decathlon.sg/cart")
class DecathlonsgProductPage: """DecathlonsgProduct is select exits size, check stock info and add product to cart.""" ADD_TO_CART_EXISTS = (By.CLASS_NAME, "add") NONE_SIZE = (By.CSS_SELECTOR, '//*[@style="display:none"]') AVAILABLE_SIZE = (By.CLASS_NAME, 'radio-label') ADD_TO_CART = (By.CSS_SELECTOR, '.btn.btn-primary.btn-lg') ADD_TO_CART_ICON = (By.CSS_SELECTOR, '.bag-icon') CLOSE_POPUP_BTN = (By.ID, 'logo') MINIFY_POPUP_BTN = (By.CSS_SELECTOR, '[aria-label="close"]') ADDED_TO_CART = (By.CSS_SELECTOR, '.sc-cart-summary-line') OUT_OF_STOCK = (By.XPATH, './/*[text()=" There are not enough products in stock "]') IFRAME_IS_OPENED = (By.CSS_SELECTOR, '.anchor_right.fb_customer_chat_bounce_in_v2') IFRAME = (By.CSS_SELECTOR, '[data-testid="dialog_iframe"]') def __init__(self, driver): self.driver = driver self.methods = BaseClass(self.driver) def select_size(self): """ Selects product size """ if self.methods.exist_element(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 """ if self.methods.exist_element(self.OUT_OF_STOCK): return False else: return True def add_product_to_cart(self): """ Adds product to the cart page and check is it added successfully. """ if self.methods.exist_element(self.ADD_TO_CART_EXISTS): try: if self.methods.exist_element(self.IFRAME_IS_OPENED): iframe_name = self.methods.presence_of_element_located( self.IFRAME).get_attribute('name') self.driver.switch_to.frame(iframe_name) self.methods.wait_for_element( DecathlonsgProductPage.MINIFY_POPUP_BTN).click() self.driver.switch_to.default_content() self.methods.wait_for_element( self.ADD_TO_CART_ICON).click() self.methods.wait_for_element(self.ADD_TO_CART_ICON).click() assert self.methods.exist_element( self.ADDED_TO_CART), "ADD_TO_CART is not clickable" except: self.driver.execute_script("window.scrollTo(0, 400)") self.methods.wait_for_element(self.ADD_TO_CART_ICON).click() else: assert self.methods.wait_for_element( self.ADD_TO_CART_EXISTS).is_displayed( ), "No add to cart button"