import logging import allure from assertpy import assert_that from ui_automation_framework.core import BasePage, TestStatus from ui_automation_framework.utils import AppConfig from ui_automation_framework.utils import logger as cl from app.api_client import ApiClient class YoutubeVideoPopup(BasePage): log = cl.Logger(logging.DEBUG) video_popup_link_xpath = "//*[contains(@id, '-videos-control')]" video_popup_close_xpath = "//*[contains(@class, 'icon-close')]" video_selector_all = "youtube-video-selector-select-all" video_selectors = "//*[contains(@id, 'youtube-video-selector-select')]" select_row = "//*[contains(@id, 'select-row')]" lifetime_views_xpath = "//*[contains(@id, 'youtube-table-cell-video-lifetime-views-')][string-length(.) > 0]" video_title_core_xpath = "//*[contains(@id, 'youtube-table-cell-video-title-')]" video_title_xpath = f"{video_title_core_xpath}/*/*/span[string-length(.) > 0]" video_title_label_xpath = f"{video_title_core_xpath}//../*[@tag='secondaryLabel'][1]" video_title_date_xpath = f"{video_title_core_xpath}//../*[@tag='secondaryLabel'][2]" video_title_by_xpath = f"{video_title_core_xpath}//../*[@tag='smallText']" apply_button_loc = "//*[text()='Apply']/ancestor::button" all_videos_text = "//span[@tag][text()='All Videos']" lifetime_views_column = "video-lifetime-views" video_title_column = "video-title" number_column = "number" icon_global = "//span[contains(@class, 'icon-global')]" views_text = f"{icon_global}/following-sibling::span" table = "youtube-video-selector-table" column_video_art = "youtube-table-cell-video-artwork" lifetime_tooltip_text_expected = "Lifetime views can delay against data in the performance section." views_text_expected = "Global views are shown" def __init__(self, driver): super().__init__(driver) self.driver = driver self.api_client = ApiClient() self.ts = TestStatus(self.driver) @allure.step("Open Youtube Video Popup") def open_video_popup(self): popup_link = self.wait_for_element_clickable(self.video_popup_link_xpath, "xpath") self.click_element(element=popup_link) titles_el = self.wait_for_element_visible(self.video_title_xpath, "xpath") assert_that(self.is_element_displayed(element=titles_el)).is_true() @allure.step("Youtube Video Popup - get table data") def get_all_from_table(self): self.wait_for_element_visible(self.lifetime_views_xpath, "xpath") lifetime_views = self.get_elements(self.lifetime_views_xpath, "xpath") lifetime_views = [int(self.get_text(element=l).replace(",", "")) for l in lifetime_views] self.wait_for_element_visible(self.video_title_xpath, "xpath") video_titles = self.get_elements(self.video_title_xpath, "xpath") video_labels = self.get_elements(self.video_title_label_xpath, "xpath") video_dates = self.get_elements(self.video_title_date_xpath, "xpath") video_bys = self.get_elements(self.video_title_by_xpath, "xpath") assert_that(len(video_titles)).is_equal_to(len(video_labels)) assert_that(len(video_titles)).is_equal_to(len(video_dates)) assert_that(len(video_titles)).is_equal_to(len(video_bys)) video_titles = [self.get_text(element=t) for t in video_titles] video_labels = [self.get_text(element=t) for t in video_labels] video_dates = [self.get_text(element=t) for t in video_dates] video_bys = [self.get_text(element=t) for t in video_bys] assert_that(len(video_titles)).is_greater_than_or_equal_to(1) data = {self.video_title_column: video_titles, self.lifetime_views_column: lifetime_views, "video_labels": video_labels, "video_dates": video_dates, "video_bys": video_bys} self.log.info(f"data: {data}") return data @allure.step("Youtube Video Popup - sort by column") def sort_by_column(self, column): column = self.wait_for_element_clickable(f"youtube-table-cell-{column}") self.click_element(element=column) self.click_element(self.all_videos_text, "xpath") @allure.step("Close Youtube Video Popup") def close_video_popup(self): close_link = self.wait_for_element_clickable(self.video_popup_close_xpath, "xpath") color_before = close_link.value_of_css_property("background-color") self.hover_on_element(element=close_link) close_link = self.wait_for_element_clickable(self.video_popup_close_xpath, "xpath") color_after = close_link.value_of_css_property("background-color") assert_that(color_before).is_not_equal_to(color_after) self.click_element(element=close_link) @allure.step("Close Youtube Video Popup") def verify_arrow_and_background_for_column(self, column, reverse=False): arrow = "up" if reverse: arrow = "down" arrow_xpath = f"//*[@id='youtube-table-cell-{column}']/..//*[contains(@class, 'icon-{arrow}')]" arrow_el = self.wait_for_element_visible(arrow_xpath, "xpath") column_el = self.wait_for_element_visible(f"youtube-table-cell-{column}-1") b_color = column_el.value_of_css_property("background-color") assert_that(self.is_element_displayed(element=arrow_el)).is_true() self.log.info(f"b color: {b_color}") assert_that(b_color).is_not_equal_to("rgba(0, 0, 0, 0)") @allure.step("Youtube Video Popup - verify elements") def verify_elements(self): icon_global = self.wait_for_element_visible(self.icon_global, "xpath") views_text = self.get_text(self.views_text, "xpath") table_displayed = self.wait_for_element_visible(self.table) assert_that(self.is_element_displayed(element=icon_global)).is_true() assert_that(self.is_element_displayed(element=table_displayed)).is_true() assert_that(views_text).is_equal_to(self.views_text_expected) assert_that(self.is_element_displayed(self.column_video_art)).is_true() assert_that(self.is_element_displayed(self.video_title_xpath, "xpath")).is_true() assert_that(self.is_element_displayed(self.lifetime_views_xpath, "xpath")).is_true() assert_that(self.is_element_displayed(element=self.wait_for_element_visible(self.select_row, "xpath"))).is_true() @allure.step("Youtube Video Popup - verify lifetime views column hover") def verify_lifetime_column_hover(self): self.hover_on_element(f"youtube-table-cell-{self.lifetime_views_column}") tooltip_text = self.get_text("youtube-table-cell-video-lifetime-views-tooltip").replace("\n", "") assert_that(tooltip_text).is_equal_to(self.lifetime_tooltip_text_expected) @allure.step("Youtube Video Popup - verify fire icons presence") def verify_fire_icons(self, isrc): positions_resp = self.api_client.get_delphi_video_positions(isrc=isrc) api_resp = self.api_client.get_delphi_videos( isrc=AppConfig.get("track40")["isrc"], params="group_by=date%2Cvideo_id&sort_by=views&limit=20&views=1000&expand_to=related_isrcs&content_type=partner_uploaded%2Cpremium_ugc&", ) video_ids_popup = [i["video_id"] for i in api_resp] video_ids_fire = list(set(p["video_id"] for p in positions_resp)) fire_icon_indexes = [video_ids_popup.index(id) + 1 for id in video_ids_fire] self.log.info(f"fire icon indexes: {fire_icon_indexes}") for f in fire_icon_indexes: assert_that(self.is_element_displayed(f"//*[contains(@id, 'trending')][{f}]//../*[contains(@class, 'icon-fire')]", "xpath")).is_true() @allure.step("Youtube Video Popup - verify apply button inactive") def verify_apply_button(self, inactive=False): rgb_disabled = "rgba(242, 242, 247, 1)" state = self.wait_for_element_visible(self.apply_button_loc, "xpath").value_of_css_property("background-color") if inactive: assert_that(state).is_equal_to(rgb_disabled) else: assert_that(state).is_not_equal_to(rgb_disabled) @allure.step("Youtube Video Popup - check videos") def check_uncheck_videos(self, indexes): for i in indexes: checkbox = self.get_element(f"//*[@id='youtube-table-cell-select-row-{i}']//../input", "xpath") self.click_on_element_js(element=checkbox) @allure.step("Youtube Video Popup - verify selected videos") def verify_selected_videos(self, indexes): for i in indexes: checkbox = self.get_element(f"//*[@id='youtube-table-cell-select-row-{i}']//../input", "xpath") attr_value = self.get_attribute_value(element=checkbox, attribute="data-checked") == "checked" assert_that(attr_value).is_true() @allure.step("Youtube Video Popup - click apply") def click_apply(self): button = self.wait_for_element_clickable(self.apply_button_loc, "xpath") self.click_element(element=button)