import time import allure from appium.webdriver.common.touch_action import TouchAction from ui_automation_framework.core import BasePage from app.src.ui_elements.button_ui_element import ButtonUIElement class Navigation(BasePage): def __init__(self, driver): super().__init__(driver) self.app_locators = self.get_page_locators("BasePage", "app_elements.json") self.starred_locators = self.get_page_locators("StarredPage", "starred_elements.json") @property def bn_home_tab(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "home_tab"), "Home tab") @property def bn_search_tab(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "search_tab"), "Search tab") @property def bn_notifications_tab(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "notifications_tab"), "Notifications tab¬") @property def bn_starred_tab(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "starred_tab"), "Starred tab") @property def bn_profile_tab(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "profile_tab"), "Profile tab") @property def back_button(self): return ButtonUIElement(self.driver, self.locator(self.app_locators, "back_button"), "Back button") @allure.step("Open chart digest tab") def select_chart_digest_tab(self): self.bn_home_tab.click() return self @allure.step("Open search tab") def select_search_tab(self): self.bn_search_tab.click() return self @allure.step("Open notifications tab") def select_notifications_tab(self): self.bn_notifications_tab.click() return self @allure.step("Open starred tracks tab") def select_starred_tracks_tab(self): self.bn_starred_tab.click() return self @allure.step("Open profile tab") def select_profile_tab(self): self.bn_profile_tab.click() return self @allure.step("Closing popup by swipe") def close_popup_by_swipe(self): action = TouchAction(self.driver) size = self.driver.get_window_size() action.long_press(x=size["width"] / 2, y=size["height"] * 0.55).move_to(x=size["width"] / 2, y=size["height"] * 0.8).release().perform() time.sleep(3) return self @allure.step("Closing keyboard by swipe") def close_keyboard_by_swipe(self): self.close_popup_by_swipe() @allure.step("Closing popup by tapping outside") def close_popup_tap_outside(self): size = self.driver.get_window_size() action = TouchAction(self.driver) action.tap(x=size["width"] / 2, y=size["height"] / 2.5).perform() time.sleep(2) return self @allure.step("Scrolling to the top of the page") def scroll_up(self): self.driver.execute_script("mobile: scroll", {"direction": "up"}) return self @allure.step("Scrolling to the end of the page") def scroll_down(self): self.driver.execute_script("mobile: scroll", {"direction": "down"}) return self @allure.step("Scrolling down several times") def scroll_down_several_times(self, times): for i in range(times): self.driver.execute_script("mobile: scroll", {"direction": "down"}) return self @allure.step("Expanding popup") def expand_popup(self, height=0.15): action = TouchAction(self.driver) size = self.driver.get_window_size() action.long_press(x=size["width"] / 2, y=size["height"] * 0.6).move_to(x=size["width"] / 2, y=size["height"] * height).release().perform() time.sleep(1) return self @allure.step("Swiping to navigate back") def swipe_back(self): time.sleep(1) size = self.driver.get_window_size() TouchAction(self.driver).long_press(None, size["width"] * 0.01, size["height"] / 2.5).wait(30).move_to(None, size["width"] * 0.95, size["height"] / 2.5).release().perform() time.sleep(0.5) return self def swipe_down(self): # TODO: make swipe configurable time.sleep(1) size = self.driver.get_window_size() TouchAction(self.driver).long_press(None, size["width"] * 0.5, size["height"] / 2.5).wait(30).move_to(None, size["width"] * 0.5, size["height"] / 10).release().perform() time.sleep(0.5) return self def swipe_up(self): # TODO: make swipe configurable time.sleep(1) size = self.driver.get_window_size() TouchAction(self.driver).long_press(None, size["width"] * 0.5, size["height"] / 8).wait(30).move_to(None, size["width"] * 0.5, size["height"] / 2.5).release().perform() time.sleep(0.5) return self # move into package @allure.step("Getting text from the clipboard") def get_clipboard(self): return self.driver.get_clipboard_text() @allure.step("Putting text into the clipboard") def set_clipboard(self, text): self.driver.set_clipboard_text(text) return self def mobile_horizontal_swipe_left(self, element_locator="", locator_type="xpath"): size = self.driver.get_window_size() element = self.get_element(element_locator, f"{locator_type}") location = element.location TouchAction(self.driver).long_press(x=size["width"] * 0.9, y=location["y"] + 30).move_to(x=size["width"] * 0.07, y=location["y"] + 30).release().perform() return self def mobile_horizontal_swipe_right(self, element_locator="", locator_type="xpath"): size = self.driver.get_window_size() element = self.get_element(element_locator, f"{locator_type}") location = element.location TouchAction(self.driver).long_press(x=size["width"] * 0.07, y=location["y"] + 30).move_to(x=size["width"] * 0.9, y=location["y"] + 30).release().perform() return self def click_back_button(self): self.back_button.click() time.sleep(1) return self def smooth_scroll_down(self): # Get the window size size = self.driver.get_window_size() # Define start and end coordinates for the scroll start_x = size["width"] / 2 start_y = size["height"] * 0.65 end_x = start_x end_y = size["height"] * 0.2 # Perform the scroll action action = TouchAction(self.driver) action.long_press(x=start_x, y=start_y).move_to(x=end_x, y=end_y).wait(5000).release().perform() # The wait is needed for the press to be released and the scrollbar to disappear time.sleep(1)