import logging import time from datetime import datetime import allure from assertpy import assert_that from selenium.webdriver import ActionChains from ui_automation_framework.core import BasePage, TestStatus from ui_automation_framework.utils import CoreConfig from ui_automation_framework.utils import logger as cl from app.src.pages.login_page import LoginPage from app.src.pages.track_page import TrackPage class NewMusicFridayPopup(BasePage): log = cl.Logger(logging.DEBUG) grouping_by_regions = "By regions" grouping_by_markets = "By markets" regions_popup = { "NORTH AMERICA": ["US", "CA"], "CONTINENTAL EUROPE": ["DE", "IT", "FR", "NL", "PL", "CH", "BE"], "LATIN": ["MX", "BR", "ES", "PE", "CO", "PT", "LAT", "S.AM", "SUR"], "UK": ["GB"], "ASIA": ["IN", "PH", "ID", "SG", "MY", "TW", "TH", "HK", "VN", "KR", "JP"], "AUNZ": ["AU"], "NORDICS": ["SE", "DK", "NO", "FI", "IS"], "AFRICA": ["ZA", "NG", "KE", "GH", "MGH"], "MIDDLE EAST": ["TR", "IL", "EG", "GULF", "LEV"], "OTHER": ["RU", "UA", "CRT", "OTH"], } regions_popup_tooltips = { "NORTH AMERICA": ["United States", "Canada"], "CONTINENTAL EUROPE": ["Germany", "Italy", "France", "Netherlands", "Poland", "Switzerland", "Belgium"], "LATIN": ["Mexico", "Brazil", "Spain", "Peru", "Colombia", "Portugal", "Latin", "South America Latin", "Chile, Argentina"], "UK": ["United Kingdom"], "ASIA": ["India", "Philippines", "Indonesia", "Singapore", "Malaysia", "Taiwan", "Thailand", "Hong Kong", "Vietnam", "South Korea", "Japan"], "AUNZ": ["Australia, New Zealand"], "NORDICS": ["Sweden", "Denmark", "Norway", "Finland", "Iceland"], "AFRICA": ["South Africa", "Nigeria", "Kenya", "Ghana", "North Africa, Maghreb"], "MIDDLE EAST": ["Turkey", "Israel", "Egypt", "Gulf", "Levant"], "OTHER": ["Russia", "Ukraine", "Cratediggers", "Other"], } sort_by_top_markets = "Top Markets" sort_by_alphabetical = "Alphabetical" sort_by_track_position = "Track Position" featured_in = "featured" not_reatured_in = "not-featured" BORDER_COLOR_GREEN = "rgba(29, 185, 84, 1)" BORDER_COLOR_GREY = "rgba(173, 177, 204, 1)" BACKGROUND_COLOR_DISABLED = "rgba(242, 242, 247, 1)" BLUE_COLOR = "rgba(1, 156, 254, 1)" BLUE_FONT_COLOR = "rgba(14, 16, 26, 1)" spotify_playlist_link = f"{CoreConfig.ENV_BASE_URL}/spotify/playlist/" def __init__(self, driver): super().__init__(driver) self.driver = driver self.nmf_popup_locators = self.get_page_locators("NewMusicFridayPopup", "new_music_friday_popup_elements.json") self.ts = TestStatus(self.driver) self.loginPage = LoginPage(self.driver) self.track_page = TrackPage(self.driver) @allure.step("Popup - is opened") def is_opened(self): self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "popup")) header = self.is_element_displayed(*self.locator(self.nmf_popup_locators, "popup")) assert_that(header).is_true() @allure.step("Popup - open") def open(self, index=0): self.wait_for_element_clickable(*self.locator(self.nmf_popup_locators, "icons_open_popup")) link = self.get_elements(*self.locator(self.nmf_popup_locators, "icons_open_popup"))[index] cursor = link.value_of_css_property("cursor") assert_that(cursor).is_equal_to("pointer") self.click_element(element=link) self.is_opened() @allure.step("Popup - set grouping") def set_grouping(self, param): grouping = self.wait_for_element_clickable(f"//*[text()='{param}']/parent::*", "xpath") self.click_element(element=grouping) @allure.step("Popup - verify regions") def verify_regions(self): regions_sel = "//*[@tag='secondaryLabel' and not(text()='Sony')]" self.wait_for_element_visible(regions_sel, "xpath") regions = self.get_elements(regions_sel, "xpath") regions = [self.get_text(element=r) for r in regions] assert_that(list(self.regions_popup.keys())).is_equal_to(regions) @allure.step("Popup - verify markets in each region") def get_and_verify_region_markets(self, verify=True, sorted=False): countries_tooltips = {} markets_all = {} for r in list(self.regions_popup.keys()): markets_sel = f"//*[text()='{r}']/parent::*/following-sibling::*/..//*[@id][string-length(text()) > 1][last()]" self.wait_for_element_visible(markets_sel, "xpath") markets = self.get_elements(markets_sel, "xpath") countries_tooltips[r] = [] for m in markets: ActionChains(self.driver).move_to_element(to_element=m).perform() country_full = self.get_text("//*[contains(@id, 'tooltip')]", "xpath") countries_tooltips[r].append(country_full) markets = [self.get_text(element=m) for m in markets] markets_all[r] = markets if verify: if not sorted: assert_that(set(markets)).is_equal_to(set(self.regions_popup[r])) else: assert_that(markets).is_sorted() assert_that(set(markets)).is_equal_to(set(self.regions_popup[r])) if verify and not sorted: assert_that(set(self.regions_popup_tooltips)).is_equal_to(set(countries_tooltips)) self.log.info(f"markets nmf popup: {markets_all}") return markets_all @allure.step("Popup - get home market data") def get_home_market_and_verify(self, verify_link=False): flag_sel = "new-music-friday-popup-home-market-icon" position_sel = "new-music-friday-popup-home-market-position" market_sel = "(//*[@id='new-music-friday-popup-home-market'])[last()]" market_box_sel = "(//*[@id='new-music-friday-popup-home-market'])[1]" home_market_link_sel = "new-music-friday-popup-home-market-link" flag = self.wait_for_element_visible(flag_sel) position = self.wait_for_element_visible(position_sel) market = self.wait_for_element_visible(market_sel, "xpath") market_box = self.wait_for_element_visible(market_box_sel, "xpath") market_border_color = market_box.value_of_css_property("border-top-color") market_background = market_box.value_of_css_property("background-color") flag = flag.get_attribute("src").split("/")[-1].split(".")[0] position = int(self.get_text(element=position).replace("–", "-1").replace("N/A", "-1")) if position < 0: assert_that(market_background).is_equal_to(self.BACKGROUND_COLOR_DISABLED) elif position <= 10: assert_that(market_border_color).is_equal_to(self.BORDER_COLOR_GREEN) else: assert_that(market_border_color).is_equal_to(self.BORDER_COLOR_GREY) market = self.get_text(element=market).lower() data = dict(position=position, market=market) self.log.info(f"home market data: {data}") assert_that(flag).is_equal_to(market) if verify_link: link = self.get_element(home_market_link_sel).get_attribute("href") assert_that(link).starts_with(self.spotify_playlist_link) assert_that(link).is_length(64) return data @allure.step("Popup - verify home market tooltip") def verify_home_market_tooltip(self, param): tooltip_sel = "new-music-friday-popup-home-market-tooltip" home_market_sle = "new-music-friday-popup-home-market" self.hover_on_element(home_market_sle) tooltip_ui = self.get_text(tooltip_sel).replace("\n", "") assert_that(param).is_equal_to(tooltip_ui) @allure.step("Popup - verify home market contains spotify link") def verify_home_market_spotify_link(self, present=True): link_sel = "new-music-friday-popup-home-market-link" if present: link_ui = self.get_element(link_sel).get_attribute("href") assert_that(link_ui).starts_with(self.spotify_playlist_link) else: assert_that(self.is_element_displayed(link_sel)).is_false() @allure.step("Popup - close") def close(self): self.wait_for_element_clickable(*self.locator(self.nmf_popup_locators, "icons_close_popup")) link = self.get_element(*self.locator(self.nmf_popup_locators, "icons_close_popup")) self.click_element(element=link) @allure.step("Select Sort By parameter") def select_sort_by(self, value): self.wait_for_element_clickable(*self.locator(self.nmf_popup_locators, "sort_by_dd")) sort_by_dd = self.get_element(*self.locator(self.nmf_popup_locators, "sort_by_dd")) self.click_element(element=sort_by_dd) dd_value_locator = "//div[@id='modal']//div[contains(text(), '{}')]".format(value) dd_value = self.get_element(locator=dd_value_locator, locator_type="xpath") self.click_on_element_js(element=dd_value) selected_value = self.get_text(element=sort_by_dd) assert_that(selected_value).is_equal_to(value) @allure.step("Filtering by market features") def filter_by_market_features(self, values, apply=True): self.wait_for_element_clickable(*self.locator(self.nmf_popup_locators, "market_features_icon")) features_icon = self.get_element(*self.locator(self.nmf_popup_locators, "market_features_icon")) self.click_element(element=features_icon) apply_btn = self.get_element("//*[contains(@id, 'apply-btn')]", "xpath") color = apply_btn.value_of_css_property("background-color") assert_that(color).is_not_equal_to("rgba(255, 198, 25, 1)") featured_in_locator = "new-music-friday-popup-filter-dd-checkbox-featured" not_featured_in_locator = "new-music-friday-popup-filter-dd-checkbox-not-featured" for locator in [featured_in_locator, not_featured_in_locator]: if self.track_page.is_checked(locator=locator, locator_type="id"): element = self.get_element(locator=locator) self.click_on_element_js(element=element) for value in values: checkbox = self.get_element(f"new-music-friday-popup-filter-dd-checkbox-{value}") self.click_on_element_js(element=checkbox) if apply: apply_btn = self.get_element("//*[contains(@id, 'apply-btn')]", "xpath") color = apply_btn.value_of_css_property("background-color") assert_that(color).is_equal_to("rgba(255, 198, 25, 1)") self.click_on_element_js(element=apply_btn) time.sleep(3) @allure.step("NMF Popup - verify i icon hint") def verify_i_icon_hint(self, displayed=True): icon_sel_base = "//*[contains(@class, 'icon-info')]" icon_sel = f"({icon_sel_base})[1]" if displayed: self.hover_on_element(icon_sel, "xpath") hint_ui = self.get_text("//*[contains(@id, 'tooltip')]", "xpath").replace("\n", " ") hint_exp = "Top Markets list compiled by aggregating all Sony Spotify streams for the past 28 days." assert_that(hint_exp).is_equal_to(hint_ui) else: assert_that(self.get_elements(icon_sel_base, "xpath")).is_length(1) @allure.step("NMF Popup - get region_positions") def get_region_positions(self): positions_all = {} self.wait_for_element_visible("//div[.='By regions']", "xpath") if self.track_page.is_tab_selected_no_assert("//div[.='By regions']", "xpath"): self.wait_for_element_visible("//span[@tag='secondaryLabel' and not(text()='Sony')]", "xpath") regions_displayed = self.get_elements("//span[@tag='secondaryLabel' and not(text()='Sony')]", "xpath") regions_displayed = [self.get_text(element=r) for r in regions_displayed] for r in regions_displayed: base_sel = f"//*[text()='{r}']/parent::*/following-sibling::*/" markets_sel = f"{base_sel}..//*[@id][string-length(text()) > 1][last()]" position_sel = f"{base_sel}..//*[@id][string-length(text()) > 0][1]" market_border_color_sel = f"{base_sel}*/a/*/*[@id]" self.wait_for_element_visible(markets_sel, "xpath") markets = self.get_elements(markets_sel, "xpath") self.wait_for_element_visible(position_sel, "xpath") positions = self.get_elements(position_sel, "xpath") markets = [self.get_text(element=m) for m in markets] market_border_color = [m.value_of_css_property("border-top-color") for m in self.get_elements(market_border_color_sel, "xpath")] positions = [int(self.get_text(element=p).replace("–", "-1")) for p in positions] positions_all[r] = {m: p for m in markets for p in positions} for p, c in zip(positions, market_border_color): if 0 <= p <= 10: assert_that(c).described_as(p).is_equal_to(self.BORDER_COLOR_GREEN) elif p > 10: assert_that(c).described_as(p).is_equal_to(self.BORDER_COLOR_GREY) else: by_markets_positions_sel = "//*[contains(@id, 'position') and string-length(@id) = 31]" by_markets_market_sel = "//*[string-length(@id) = 22 and string-length(text()) > 1]" self.wait_for_element_visible(by_markets_market_sel, "xpath") markets = self.get_elements(by_markets_market_sel, "xpath") self.wait_for_element_visible(by_markets_positions_sel, "xpath") positions = self.get_elements(by_markets_positions_sel, "xpath") markets = [self.get_text(element=m) for m in markets] positions = [int(str(self.get_text(element=p)).replace("–", "-1")) for p in positions] positions_all["all"] = {m: p for m, p in zip(markets, positions)} self.log.info(f"positions_all: {positions_all}") return positions_all @allure.step("NMF Popup - verify cards are clickable in popup") def verify_links_regions_markets(self): links_selector = "//*[contains(@id, 'position')]/ancestor::a[contains(@id, 'link')]" self.wait_for_element_visible(links_selector, "xpath") links = self.get_elements(links_selector, "xpath") links = [l.get_attribute("href") for l in links] assert_that(len(links)).is_greater_than_or_equal_to(200) for link in links: assert_that(link).starts_with(self.spotify_playlist_link) @allure.step("NMF Popup - verify popup market flags") def verify_region_markets_flags(self): flags_sel = "//img[contains(@id, 'icon')]" markets_sel = f"{flags_sel}/parent::*/following-sibling::*[@id][last()]" flags_el = self.get_elements(flags_sel, "xpath") markets_el = self.get_elements(markets_sel, "xpath") flags = [f.get_attribute("src").split("/")[-1].split(".")[0] for f in flags_el] flags = [f for f in flags if f != "latin"] markets = [self.get_text(element=m).lower() for m in markets_el] flags_unkn = "//*[contains(@class, 'icon-placeholder')]" market_unkn = f"{flags_unkn}/parent::*/following-sibling::*[@id][last()]" markets_u = [self.get_text(element=m) for m in self.get_elements(market_unkn, "xpath")] assert_that(set(flags)).is_subset_of(markets) assert_that(markets_u).is_equal_to(["S.AM", "SUR", "MGH", "GULF", "LEV", "CRT", "OTH"]) @allure.step("NMF Popup - click on header") def click_on_header(self): header_el = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "title")) self.click_element(element=header_el) @allure.step("NMF Popup - verify market features filter") def verify_market_features_hover(self): dd = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "market_features_icon")) cursor = dd.value_of_css_property("cursor") assert_that(cursor).is_equal_to("pointer") @allure.step("NMF Popup - get spotify id from UI") def get_spotify_id(self): track_el = self.wait_for_element_clickable("new-music-friday-popup-link") id = track_el.get_attribute("href").split("/")[-1] return id @allure.step("NMF Popup - verify title") def verify_title(self): date_url = self.track_page.parameter_from_url("nmf_date") date_url = datetime.strptime(date_url, "%Y-%m-%d").strftime(self.track_page.get_date_format_from_locale()) header_el = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "title")) header = self.get_text(element=header_el) assert_that(f"NMF Markets, {date_url}").is_equal_to(header) @allure.step("NMF Popup - verify track art") def verify_track_art(self): track_art = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "track_art")) url_before = self.get_url() self.click_element(element=track_art) url_after = self.get_url() assert_that(url_before).is_equal_to(url_after) assert_that(self.is_element_displayed(element=track_art)).is_true() @allure.step("NMF Popup - verify track name") def verify_track_name_and_hover(self): track_name = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "track_name")) color = track_name.value_of_css_property("color") track_name_text = self.get_text(element=track_name) self.hover_on_element(element=track_name) tooltip = self.get_text(self.track_page.hint_locator, "xpath") href = track_name.get_attribute("href") assert_that(color).is_equal_to(self.BLUE_COLOR) assert_that(self.is_element_displayed(element=track_name)).is_true() assert_that(track_name_text).is_equal_to(tooltip) assert_that(href).starts_with(f"{CoreConfig.ENV_BASE_URL}/spotify/track/") @allure.step("NMF Popup - verify artist name") def verify_artist_name_and_hover(self): artist_name = self.wait_for_element_visible(*self.locator(self.nmf_popup_locators, "track_artist")) # artist_name_text = self.get_text(element=artist_name) self.hover_on_element(element=artist_name) # tooltip = self.get_text(self.track_page.hint_locator, "xpath") href = self.get_element("(//*[contains(@href, '/spotify/artist')])[1]", "xpath").get_attribute("href") assert_that(self.is_element_displayed(element=artist_name)).is_true() # assert_that(artist_name_text).is_equal_to(tooltip) assert_that(href).starts_with(f"{CoreConfig.ENV_BASE_URL}/spotify/artist/") @allure.step("NMF Popup - get track indicators parameter for index") def get_track_indicators(self): parameters_sel = "//*[@id='new-music-friday-popup-param-featuring-value']/parent::div/parent::div/*/*" parameters_el = self.get_elements(parameters_sel, "xpath") parameters = [self.get_text(element=p) for p in parameters_el] assert_that(["Featuring", "Top 10 Rank", "Avg. Position"]).is_equal_to([parameters[1], parameters[3], parameters[5]]) parameters = [int(p) for p in [parameters[0], parameters[2], parameters[4]]] self.log.info(f"parameters nmf popup: {parameters}") return parameters @allure.step("NMF Popup - verify markets received note") def verify_markets_received_note(self): note_sel = "//*[text()='All markets received.']" note = self.is_element_displayed(note_sel, "xpath") assert_that(note).is_true() @allure.step("NMF Popup - verify i icon hover") def verify_i_icon_hover(self, visible=True): icon_sel = "(//*[contains(@class, 'icon-info')])[1]" if visible: self.hover_on_element(icon_sel, "xpath") tooltip_text = self.get_text(self.track_page.hint_locator, "xpath").replace("\n", " ") tooltip_exp = "Top Markets list compiled by aggregating all Sony Spotify streams for the past 28 days." assert_that(tooltip_exp).is_equal_to(tooltip_text) else: assert_that(len(self.get_elements(icon_sel, "xpath"))).is_less_than_or_equal_to(1) @allure.step("NMF Popup - verify Sort By dd") def verify_sort_by_dd(self, index=0): self.wait_for_element_clickable(*self.locator(self.nmf_popup_locators, "sort_by_dd")) sort_by_dd = self.get_element(*self.locator(self.nmf_popup_locators, "sort_by_dd")) self.click_element(element=sort_by_dd) dd_values_locator = "//*[contains(@class, 'icon-up')]/preceding-sibling::*/*/*" dd_values_el = self.get_elements(locator=dd_values_locator, locator_type="xpath") dd_values = [self.get_text(element=d) for d in dd_values_el] self.hover_on_element(element=dd_values_el[index]) assert_that(dd_values).is_equal_to([self.sort_by_top_markets, self.sort_by_alphabetical, self.sort_by_track_position]) assert_that(dd_values_el[index].value_of_css_property("font-weight")).is_equal_to("400") assert_that(dd_values_el[index].value_of_css_property("color")).is_equal_to(self.BLUE_FONT_COLOR) self.click_element(element=sort_by_dd)