# ------------------------------------------------------------------ # TEST 1 of 3: Check Clay API metaTitle with expected_title # ------------------------------------------------------------------ print(" TEST 1 of 3: Compare metaTitle to expected title") # Call the Clay API for the dynamic meta title data meta_title_text = functions.call_url(meta_title_url) meta_title_data = json.loads(meta_title_text) # If there is no data, the function returns an empty string. Make the title # empty too. Otherwise, check the metaTitle content values. if meta_title_data == "": meta_title = "" else: meta_title = meta_title_data["_computed"]["metaTitle"] meta_title = functions.isNoneOrEmpty(meta_title, "Meta Title") # Compare and print expected vs actual metaTitle match = compareAndPrint(expected_title, meta_title) # Add to the counters if there wasn't a match if match == False: clay_meta_title_failed += 1 tests_failed += 1 # --------------------------------------------------------- # TEST 2 of 3: Match HTML og:title to expected_title # --------------------------------------------------------- print(" TEST 2 of 3 : Compare og.title to expected title") # Get the og:title content value from the Listen Live web page
total_calls += 1 api_url = url["api_url"] html_url = url["html_url"] # Get the API data podcast_data_text = functions.call_url(api_url) podcast_data = json.loads(podcast_data_text) # Get the web page data page_data = functions.call_url(html_url)[:PAGE_BUFFER] # API description is the expected description for the test expected_description = podcast_data["data"]["attributes"]["description"] # If the API doesn't have a description - log it expected_description = functions.isNoneOrEmpty(expected_description, "Podcast API Description") # Get the tag content values from the HTML page description = functions.get_meta_tag_content(page_data, "", "description") og_description = functions.get_meta_tag_content(page_data, "og:description") twitter_description = functions.get_meta_tag_content(page_data, "", "twitter:description") # Set value to empty string if value is ${PARAMVALUE} og_description = functions.check_for_empty(og_description) twitter_description = functions.check_for_empty(twitter_description) description = functions.check_for_empty(description) # Print something easy to see in the console print(" Looking for : '%s'" % expected_description) print(" OG Found : '%s'" % og_description) print(" Twitter Found : '%s'" % twitter_description)
else: message = " TEST: %s\n" % FAILED failed_title += 1 tests_failed += 1 functions.show_message(message) # ------------------------------------------------------------------------ # TEST 2 of 8: Compare the API description with the HTML Description, # OG Description, and Twitter Description # ------------------------------------------------------------------------ print(" TEST 2 of 8: Description Check") # If ${PARAMVALUE}, return empty string station_description = functions.check_for_empty(station_description) # If None/Empty, return empty string. Else return in uppercase. station_description = functions.isNoneOrEmpty(station_description, "Station API Description") # Get the actual values from the web page html_description = functions.get_meta_tag_content(page_data, "", "description") og_description = functions.get_meta_tag_content(page_data, "og:description") twitter_description = functions.get_meta_tag_content( page_data, "", "twitter:description") # Comparison time - all descriptions should match print(" Looking for : '%s'" % station_description) print(" Desc Tag Found : '%s'" % html_description) print(" OG Desc Found : '%s'" % og_description) print(" Twitter Desc Found : '%s'" % twitter_description)
api_title = podcast_dictionary["attributes"]["title"] api_description = podcast_dictionary["attributes"]["description"] api_image = podcast_dictionary["attributes"]["image"] # Build the url to visit the podcast web page podcast_url = "sanitized" % site_slug # Call for the podcast web page data page_data = functions.call_url(podcast_url)[:PAGE_BUFFER] # ------------------------------------------------------------------------ # TEST 1 of 3: Compare API title with HTML <title>, og:title and # twitter:title. All 4 should match. # ------------------------------------------------------------------------ print(" TEST 1 of 3: Title Check") # If None/Empty, return empty string. Else return in uppercase. api_title = functions.isNoneOrEmpty(api_title, "Podcast API Title") # Function returns string in upper case html_title = functions.get_title(page_data) og_title = functions.get_meta_tag_content(page_data, "og:title") twitter_title = functions.get_meta_tag_content(page_data, "", "twitter:title") # Comparison time - all titles should match print(" Looking for : '%s'" % api_title) print(" OG Found : '%s'" % og_title) print(" Twitter Found : '%s'" % twitter_title) print(" HTML Title Found : '%s'" % html_title) # If they all match, test passes. Else, test fails. if api_title == og_title and api_title == twitter_title and \
page_data = functions.call_url(html_url)[:PAGE_BUFFER] # If getting a 404 "not found" in the url results, # break the loop and report failed to the Google Sheet if "not found" in page_data: pages_failed = 1 break # ------------------------------------------------------------------------ # Test 1 of 3: Compare API Episode Title with HTML Episode Title, og:title, # and twitter:title. All 4 should match # ------------------------------------------------------------------------ print(" TEST 1 of 3: Episode Title Check") # If None/Empty, return empty string. Else return in uppercase. episode_title = functions.isNoneOrEmpty(episode_title, "Episode Title") # Function returns string in upper case html_title = functions.get_title(page_data) og_title = functions.get_meta_tag_content(page_data, "og:title") twitter_title = functions.get_meta_tag_content(page_data, "", "twitter:title") # Comparison time - all titles should match print(" Looking for : '%s'" % episode_title) print(" Title Tag Found : '%s'" % html_title) print(" OG Found : '%s'" % og_title) print(" Twitter Found : '%s'" % twitter_title) match1 = compare(episode_title, html_title) match2 = compare(episode_title, og_title)
def baseTest(page_urls, html, html2=""): """ All the Branch IO Test scripts have the same base test set for Station URLs. This function tests the Station Market, Logo, Name, ID, Category, and Genre Name for a given list of page urls. This function accepts 3 arguments: 1) The list of page_urls to test 2) The HTML page under test (example: the article_url or the contest_url) 3) A second HTML page under test which is optional (example: the script for section fronts tests both the primary section front url and the secondary section front url at the same time). """ # Counters for tallying test results total_calls = 0 pages_passed = 0 pages_failed = 0 marketFail = 0 logoFail = 0 nameFail = 0 idFail = 0 categoryFail = 0 genreFail = 0 # Start a loop to go through the given page_urls list for url in page_urls: total_calls += 1 ##################################### # API Variable SECTION ##################################### api_url = url[ "api_url"] # <-- URL is given to us in the page_urls list # Get all of the station data from the core API as a list of dictionaries station_data_text = functions.call_url(api_url) station_data = json.loads( station_data_text) # <-- List of dictionaries station_dictionary = station_data[ "data"] # <-- We ALWAYS want variables from the data section # Set variables for API comparison testing later station_id = station_dictionary["id"] station_market = station_dictionary["attributes"]["market"][ "display_name"] station_name = station_dictionary["attributes"]["name"] station_logo = station_dictionary["attributes"]["square_logo_small"] # Category is a LIST (of ONLY ONE dictionary) that is nested inside of "attributes" station_category = station_dictionary["attributes"]["category"] # Genre is a LIST (of AT LEAST ONE dictionary) that is nested inside of "attributes" station_genre = station_dictionary["attributes"]["genre"] # Check if there is more than one dictionary in this list # Separate out the genre names from the other dictionary values if len(station_genre) > 1: message = "Station has more than one genre dictionary in the list!" functions.show_message(message) list_of_genre_names = [] for item_dict in station_genre: name = item_dict["name"] list_of_genre_names.append(name) station_genre_name = list_of_genre_names print("There is more than one genre name!! API returns: %s" % station_genre_name) else: station_genre_name = station_genre[0]["name"] # If any variables are empty, log them station_market = functions.isNoneOrEmpty(station_market, "Station Market") station_logo = functions.isNoneOrEmpty(station_logo, "Station Logo") station_name = functions.isNoneOrEmpty(station_name, "Station Name") station_category = functions.isCategoryOrGenreEmpty( station_category, "Station Category") station_genre = functions.isCategoryOrGenreEmpty( station_genre, "Station Genre") station_id = functions.isIDEmpty(station_id) # Check if Genre Name is a list (the list we made earlier or just a single value) # Make uppercase for comparison testing if type(station_genre_name) is list: message = "The API Genre is a list." station_genre_name = [item.upper() for item in station_genre_name] else: message = "The API Genre is a single value." station_genre_name = station_genre_name.upper() functions.show_message(message) # Check if category (the value not the section) is a list # Make uppercase for comparison testing if type(station_category) is list: message = "The API Category is a list." station_category = [item.upper() for item in station_category] else: message = "The API Category is a single value." station_category = station_category.upper() functions.show_message(message) ##################################### # HTML Variable SECTION ##################################### html_url1 = url[html] # <-- URL is given to us in the page_urls list url1_web_page_data = functions.call_url(html_url1)[:PAGE_BUFFER] # Some scripts have 2 HTML URLs to verify against. Some have only one. if html2 != "": html_url2 = url[html2] url2_web_page_data = functions.call_url(html_url2)[:PAGE_BUFFER] else: url2_web_page_data = "" ##################################### # TEST SECTION ##################################### # Tests with 1 HTML URL if url2_web_page_data == "": market_test = test.runTagCheck(station_market, url1_web_page_data, "branch:deeplink:market") logo_test = test.runTagCheck(station_logo, url1_web_page_data, "branch:deeplink:station_logo") name_test = test.runTagCheck(station_name, url1_web_page_data, "branch:deeplink:station_name") id_test = test.runTagCheck(str(station_id), url1_web_page_data, "branch:deeplink:station_id") # If there is a list of categories to compare against if type(station_category) is list: category_test = test.runTagListCheck( station_category, url1_web_page_data, "branch:deeplink:category") elif type(station_category) is str: category_test = test.runTagCheck(station_category, url1_web_page_data, "branch:deeplink:category") # If there is a list of genre names to compare against if type(station_genre_name) is list: genre_test = test.runTagListCheck(station_genre_name, url1_web_page_data, "branch:deeplink:genre") elif type(station_genre_name) is str: genre_test = test.runTagCheck(station_genre_name, url1_web_page_data, "branch:deeplink:genre") # Tests with 2 HTML URLs else: market_test = test.runTagCheckDouble(station_market, url1_web_page_data, url2_web_page_data, "branch:deeplink:market") logo_test = test.runTagCheckDouble(station_logo, url1_web_page_data, url2_web_page_data, "branch:deeplink:station_logo") name_test = test.runTagCheckDouble(station_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:station_name") id_test = test.runTagCheckDouble(str(station_id), url1_web_page_data, url2_web_page_data, "branch:deeplink:station_id") # If there is a list of genre names to compare against if type(station_category) is list: category_test = test.runTagListCheckDouble( station_category, url1_web_page_data, url2_web_page_data, "branch:deeplink:category") elif type(station_category) is str: category_test = test.runTagCheckDouble( station_category, url1_web_page_data, url2_web_page_data, "branch:deeplink:category") # If there is a list of genre names to compare against if type(station_genre_name) is list: genre_test = test.runTagListCheckDouble( station_genre_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:genre") elif type(station_genre_name) is str: genre_test = test.runTagCheckDouble(station_genre_name, url1_web_page_data, url2_web_page_data, "branch:deeplink:genre") ##################################### # TEST RESULTS SECTION ##################################### # Counter for how many of the 6 tests that this set of urls failed urlFailCount = 0 # Check if all are truthy if market_test and logo_test and name_test and id_test and category_test and genre_test: message = "\nAll tests passed for this URL set.\n\n" pages_passed += 1 # If at least one is not True, gotta count what failed and how many of them else: pages_failed += 1 # <-- Increment the overall/total URL Failure count if market_test == False: urlFailCount += 1 marketFail += 1 if logo_test == False: urlFailCount += 1 logoFail += 1 if name_test == False: urlFailCount += 1 nameFail += 1 if id_test == False: urlFailCount += 1 idFail += 1 if category_test == False: urlFailCount += 1 categoryFail += 1 if genre_test == False: urlFailCount += 1 genreFail += 1 # Print and tally results before going to the next url for testing message = "\n%d of 6 Tests Failed!!\n\n" % urlFailCount functions.show_message(message) # Finally outside the for loop # Printing test results print("\n-------STATION BASE TEST RESULTS----------") print("%d of %d Total Calls PASSED" % (pages_passed, total_calls)) print("----------------------------") print("%d of %d Market Tests FAILED" % (marketFail, total_calls)) print("%d of %d Logo Tests FAILED" % (logoFail, total_calls)) print("%d of %d Name Tests FAILED" % (nameFail, total_calls)) print("%d of %d ID Tests FAILED" % (idFail, total_calls)) print("%d of %d Category Tests FAILED" % (categoryFail, total_calls)) print("%d of %d Genre Name Tests FAILED" % (genreFail, total_calls)) print("-------STATION BASE TEST RESULTS----------\n\n") # Return how many total URL failures there were return pages_failed
podcast_dictionary = podcast_data["data"] podcast_logo = podcast_dictionary["attributes"]["image"] podcast_category_list = podcast_dictionary["attributes"]["category"] podcast_category_name = podcast_category_list[0][ "name"] # TODO fix this after category issue is addressed # Call Podcast Episode Page page_data = functions.call_url(html_url)[:PAGE_BUFFER] # ------------------------------------------------------------- # Check if the variables are null/None/empty # Set them to empty strings for comparison if they are # ------------------------------------------------------------- podcast_id = functions.isIDEmpty(podcast_id) episode_id = functions.isIDEmpty(episode_id) episode_type = functions.isNoneOrEmpty(episode_type, "Episode Type") podcast_title = functions.isNoneOrEmpty(podcast_title, "Podcast Title") episode_title = functions.isNoneOrEmpty(episode_title, "Episode Title") podcast_logo = functions.isNoneOrEmpty(podcast_logo, "Podcast Logo") podcast_category_name = functions.isNoneOrEmpty(podcast_category_name, "Podcast Category Name") # ------------- # Test Section # ------------- # ------------------------------------------------------------- # Test 1 of 7: Compare API Type with HTML Type # ------------------------------------------------------------- print(" TEST 1 of 7: Type Check") episode_type_test = runTagCheck(episode_type, page_data,
podcast_partner = podcast_dictionary["attributes"]["partner"] expected_partner_id = podcast_partner["id"] expected_partner_name = podcast_partner["name"] # Category is a LIST (of ONE dictionary) that is nested inside of "attributes" # Right now HTML only contains the first one from the list podcast_category = podcast_dictionary["attributes"]["category"] expected_category_name = podcast_category[0]["name"] # ------------------------------------------------------------- # Check if the variables are null/None/empty # Set them to empty strings for comparison if they are # ------------------------------------------------------------- expected_id = functions.isIDEmpty(expected_id) expected_partner_id = functions.isIDEmpty(expected_partner_id) expected_type = functions.isNoneOrEmpty(expected_type, "Podcast Type") expected_title = functions.isNoneOrEmpty(expected_title, "Podcast Title") expected_partner_name = functions.isNoneOrEmpty(expected_partner_name, "Partner Name") expected_category_name = functions.isNoneOrEmpty(expected_category_name, "Category Name") expected_image = functions.isNoneOrEmpty(expected_image, "Image") # ---------------------------------------------------------------- # Only testing Station podcast pages; not National podcasts # ---------------------------------------------------------------- if podcast_dictionary["attributes"]["station"] == []: print("\n***\nThis Podcast %s is National and has no Station data." % expected_id) print("\nSkipping to the next one.\n***\n") skipped += 1 # ---------------------------------------------------------------- # For each station podcast page, get station info and build the URL