import logging import random import unittest import allure import pytest from assertpy import assert_that from ui_automation_framework.utils import AppConfig from ui_automation_framework.utils import logger as cl from app.src.helpers.api_data_provider import ApiDataProvider from app.src.helpers.test_data_helper import get_random_hot_hits_spotify_playlist from app.src.pages.hot_hits_placements_page import HotHitsPlacementsPage from app.src.pages.login_page import LoginPage from app.src.pages.playlist_page import PlaylistPage from app.src.pages.playlist_placements_page import PlaylistPlacementsPage from app.src.pages.search_page import SearchPage from app.src.pages.track_page import TrackPage log = cl.Logger(logging.DEBUG) @pytest.mark.usefixtures("set_up", "one_time_set_up", "authorized_api") class HotHitsTest(unittest.TestCase): @pytest.fixture(autouse=True) def classSetup(self, authorized_api): self.api = authorized_api self.loginPage = LoginPage(self.driver, authorized_api) self.trackPage = TrackPage(self.driver, authorized_api) self.searchPage = SearchPage(self.driver, authorized_api) self.playlistPage = PlaylistPage(self.driver, authorized_api) self.playlistsPage = PlaylistPlacementsPage(self.driver, authorized_api) self.hotHitsPage = HotHitsPlacementsPage(self.driver, authorized_api) self.data_provider = ApiDataProvider(authorized_api) @allure.story("https://data-analytics.atlassian.net/browse/AG-9295") @allure.severity(allure.severity_level.NORMAL) @allure.title("Hot Hits data comparison with API") @allure.testcase("https://data-analytics.atlassian.net/browse/AG-9209," "https://data-analytics.atlassian.net/browse/AG-9222,") @pytest.mark.smoke def test_comparison_of_widget_and_header_elements_data_with_api(self): hh_playlist_id, hh_playlist_name = get_random_hot_hits_spotify_playlist() tracklist = self.api.apollo.get_spotify_playlist_tracklist(hh_playlist_id, order_by="current_position")["body"]["items"] track_name = tracklist[0]["name"] track_isrc = tracklist[0]["isrc"] hh_data = self.api.apollo.get_hot_hits_playlists(isrc=track_isrc) count = hh_data["count"] positions_avg = hh_data["positions_avg"] positions_top_total = hh_data["positions_top_total"] total = hh_data["total"] self.loginPage.login_as_regular_user() self.searchPage.click_playlists_tab().search_for(hh_playlist_name) self.searchPage.open_playlist_page_from_results(1) self.playlistPage.click_search_field().click_track_card(1) # Parse Hot Hits widget and compare these data with API results hh_widget_text = self.trackPage.bn_hh_widget.text placements_from_widget = int(hh_widget_text.partition("PLACEMENTS")[-1].split()[0]) total_from_widget = int(hh_widget_text.partition("TOTAL")[-1].split()[0]) avg_position_from_widget = int(hh_widget_text.partition("AVG. POSITION #")[-1].split()[0]) top_10_ranks_from_widget = int(self.trackPage.bn_hh_widget.text.split()[-1]) assert_that(count).is_equal_to(placements_from_widget) assert_that(total).is_equal_to(total_from_widget) assert_that(positions_avg).is_equal_to(avg_position_from_widget) assert_that(positions_top_total).is_equal_to(top_10_ranks_from_widget) self.trackPage.click_hot_hits_widget() # Compare data in the header of the Hot Hits page with API results assert_that(self.hotHitsPage.lb_track_name(track_name=track_name).is_visible()).is_true() assert_that(self.hotHitsPage.lb_avg_position(position=positions_avg).is_visible()).is_true() assert_that(self.hotHitsPage.lb_top_position(position=positions_top_total).is_visible()).is_true() @allure.story("https://data-analytics.atlassian.net/browse/AG-9293") @allure.severity(allure.severity_level.NORMAL) @allure.title("Hot Hits data comparison with API") @allure.testcase( "https://data-analytics.atlassian.net/browse/AG-9223," "https://data-analytics.atlassian.net/browse/AG-9225," "https://data-analytics.atlassian.net/browse/AG-9232," "https://data-analytics.atlassian.net/browse/AG-9233," "https://data-analytics.atlassian.net/browse/AG-9234," "https://data-analytics.atlassian.net/browse/AG-9235," ) @pytest.mark.smoke def test_markets(self): regions = AppConfig.get("regions") random_region = random.choice(regions[3:]) # we use such slice from 4th element in order to check swiping by region hh_playlist_id, hh_playlist_name = get_random_hot_hits_spotify_playlist() log.debug(f"Playlist ID: {hh_playlist_id}, Playlist name: {hh_playlist_name}") tracklist = self.api.apollo.get_spotify_playlist_tracklist(hh_playlist_id, order_by="current_position")["body"]["items"] track_isrc = tracklist[0]["isrc"] all_markets_data = self.api.apollo.get_hot_hits_playlists(isrc=track_isrc, offset=0, order_by=["top_market", "code"], region="all", include="inactive_markets")["items"] all_active_markets = [i for i in all_markets_data if i["isrc"]] all_inactive_markets = [i for i in all_markets_data if i["isrc"] is None] if random_region == "AUNZ": random_region_for_filtering = "AUNZ" elif random_region == "UK": random_region_for_filtering = "UK" else: random_region_for_filtering = random_region.title() random_region_active_markets = [i for i in all_markets_data if i["isrc"] and i["region"] == random_region_for_filtering] random_region_inactive_markets = [i for i in all_markets_data if i["isrc"] is None and i["region"] == random_region_for_filtering] self.loginPage.login_as_regular_user() self.searchPage.click_playlists_tab().search_for(hh_playlist_name) self.searchPage.open_playlist_page_from_results(1) assert_that(self.playlistPage.is_playlist_page_opened_for_name(hh_playlist_name), "Appropriate playlist is opened").is_true() self.playlistPage.click_search_field().click_track_card(1) self.trackPage.click_hot_hits_widget() assert_that(self.hotHitsPage.check_all_regions_are_present()).is_true() assert_that(self.hotHitsPage.lt_active_market_cards.elements).is_length(len(all_active_markets)) assert_that(self.hotHitsPage.lt_inactive_market_cards.elements).is_length(len(all_inactive_markets)) is_first_item = True for market_api, market_ui in zip(all_active_markets, self.hotHitsPage.lt_active_market_cards.texts): added_days = int(market_ui.split()[0]) position = int(market_ui.split()[3]) position_change = int(market_ui.split()[5]) market_name = market_ui.split()[6] assert_that(market_api["added_days"]).is_equal_to(added_days) assert_that(market_api["position"]).is_equal_to(position) if market_api["position_change"] > 0: icon = "\ue905" elif market_api["position_change"] < 0: icon = "\ue902" position_change *= -1 else: icon = "\ue904" assert_that(market_ui, "Trend icon").contains(icon) assert_that(market_api["position_change"]).is_equal_to(position_change) assert_that(market_api["name"].upper()).contains(market_name) # contains() is used to handle situations when we have 2 words market, e.g. "United States" if is_first_item: self.hotHitsPage.lt_active_market_cards.elements[0].click() assert_that( self.playlistsPage.is_playlists_placements_page_opened_for_name(hh_playlist_name), "Playlist placement wasn't opened", ) self.playlistsPage.click_back_button() is_first_item = False self.hotHitsPage.swipe_up() before_sorting = self.hotHitsPage.lt_market_cards.texts self.hotHitsPage.sort_markets(sort_by="Track Position") after_sorting = self.hotHitsPage.lt_market_cards.texts assert_that(before_sorting).is_not_equal_to(after_sorting) after_sorting = [market.split() for market in after_sorting] after_sorting_parsed = [market[3] if len(market) > 3 else None for market in after_sorting] after_sorting_only_positions = [int(market) for market in after_sorting_parsed if market is not None] assert_that(after_sorting_only_positions).is_equal_to(sorted(after_sorting_only_positions)) self.hotHitsPage.click_region_button(random_region) self.hotHitsPage.sort_markets(sort_by="Top Markets") assert_that(self.hotHitsPage.lt_active_market_cards.elements).is_length(len(random_region_active_markets)) assert_that(self.hotHitsPage.lt_inactive_market_cards.elements).is_length(len(random_region_inactive_markets)) for market_api, market_ui in zip(random_region_active_markets, self.hotHitsPage.lt_active_market_cards.texts): added_days = int(market_ui.split()[0]) position = int(market_ui.split()[3]) position_change = int(market_ui.split()[5]) market_name = market_ui.split()[6] assert_that(market_api["added_days"]).is_equal_to(added_days) assert_that(market_api["position"]).is_equal_to(position) assert_that(abs(market_api["position_change"])).is_equal_to(position_change) assert_that(market_api["name"].upper()).contains(market_name)