hooks_recording.py (6478B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 import json 5 import os 6 import platform 7 from pathlib import Path 8 9 from mozperftest.test.browsertime import add_option, add_options 10 11 # Uncomment the manual_login line if you need to do a manual login. 12 # The extra browsertime arguments get overwritten below so they 13 # need to be set here. The value is the time you need to do a login. 14 options = [ 15 ("pageCompleteWaitTime", "10000"), 16 # ("browsertime.manual_login", 100000), 17 ] 18 19 next_site = None 20 21 RECORDING_LIST = Path(Path(__file__).parent, "pageload_sites.json") 22 23 # Uncomment this to record tp7 desktop sites in CI or locally. 24 # This is still a WIP (Bug 1831310) and meant to be used by the 25 # perftest team. 26 # RECORDING_LIST = Path(Path(__file__).parent, "tp7_desktop_sites.json") 27 28 SCM_1_LOGIN_SITES = ("facebook", "netflix") 29 30 31 def before_iterations(kw): 32 global next_site 33 print("Setting up next site to record.") 34 35 with RECORDING_LIST.open() as f: 36 site_list = json.load(f) 37 # currently can't record websites that require user interactions(Logins) 38 if kw.get("android"): 39 site_list = site_list["mobile"] 40 else: 41 site_list = site_list["desktop"] 42 43 def __should_record(test): 44 # If a test page selection was provided, only select those 45 # tests and exclude all the others 46 specified_tests = kw["proxy_perftest_page"] 47 if specified_tests is not None: 48 if test.get("name") == specified_tests: 49 if test.get("login"): 50 print(f"WARNING: You selected a login test: {test.get('name')}") 51 return True 52 else: 53 return False 54 55 # Only perform login recordings in automation or when 56 # RAPTOR_LOGINS is defined 57 record = False 58 if not test.get("login") or test.get("login-test"): 59 record = True 60 if not ( 61 "MOZ_AUTOMATION" in os.environ or "RAPTOR_LOGINS" in os.environ 62 ) and test.get("login-test"): 63 record = False 64 print( 65 f"Skipping login test `{test.get('name')}` " 66 f"because login info cannot be obtained." 67 ) 68 69 # When pushing to Try, only attempt login recording using the 70 # taskcluster secrets that are associated with SCM level 1 as defined 71 # in `SCM_LVL_1_SITES`. 72 if test.get("login"): 73 if "MOZ_AUTOMATION" in os.environ.keys(): 74 if ( 75 os.environ.get("MOZ_SCM_LEVEL") == 1 76 and test.get("name") not in SCM_1_LOGIN_SITES 77 ): 78 print( 79 f"Skipping login test `{test.get('name')}` " 80 f"Because SCM = `{os.environ.get('MOZ_SCM_LEVEL')}`" 81 f"and there is no secret available at this level" 82 ) 83 return False 84 return True 85 elif "RAPTOR_LOGINS" in os.environ: 86 # Leave it to the user to have properly set up a local json file with 87 # the login websites of interest 88 return True 89 90 return record 91 92 sites = [test_site for test_site in site_list if __should_record(test_site)] 93 94 if not sites: 95 raise Exception("No tests were selected for recording!") 96 97 def next_site(): 98 yield from sites 99 100 next_site = next_site() 101 102 # Set the number of test-iterations to the number of builds 103 kw["test_iterations"] = len(sites) 104 return kw 105 106 107 def before_runs(env): 108 print("Running before_runs") 109 add_options(env, options) 110 111 if next_site: 112 test_site = next(next_site) 113 print("Next site: %s" % test_site) 114 115 if env.get_arg("android"): 116 platform_name = "android" 117 app_name = env.get_arg("android-app-name").split(".")[-1] 118 else: 119 platform_name = platform.system().lower() 120 app_name = "firefox" 121 122 # bug 1883701 linux uses a different version for now 123 name = [ 124 "mitm8" if platform_name == "linux" else "mitm11", 125 platform_name, 126 "gve" if app_name == "geckoview_example" else app_name, 127 test_site["name"], 128 ] 129 130 recording_file = "%s.zip" % "-".join(name) 131 132 env.set_arg("proxy-mode", "record") 133 env.set_arg( 134 "proxy-file", 135 recording_file, 136 ) 137 138 add_options(env, options, overwrite=True) 139 add_option(env, "browsertime.url", test_site.get("test_url")) 140 add_option(env, "browsertime.screenshot", "true") 141 add_option(env, "browsertime.testName", test_site.get("name")) 142 add_option(env, "browsertime.testType", test_site.get("type", "pageload")) 143 add_option( 144 env, "browsertime.login", "true" if test_site.get("login") else "false" 145 ) 146 147 prefs = test_site.get("preferences", {}) 148 for pref, val in prefs.items(): 149 add_option(env, "firefox.preference", f"{pref}:{val}") 150 151 # Add prefs that will attempt to remove cookie banners 152 add_option( 153 env, "firefox.preference", "cookiebanners.bannerClicking.enabled:true" 154 ) 155 add_option(env, "firefox.preference", "cookiebanners.service.mode:2") 156 157 second_url = test_site.get("secondary_url", None) 158 if second_url: 159 add_option(env, "browsertime.secondary_url", second_url) 160 161 inject_deterministic = test_site.get("inject_deterministic", True) 162 env.set_arg("proxy-deterministic", inject_deterministic) 163 164 dismiss_cookie_prompt = test_site.get("dismiss_cookie_prompt", []) 165 if dismiss_cookie_prompt: 166 parsed_cmds = [ 167 ":::".join([str(i) for i in item]) 168 for item in dismiss_cookie_prompt 169 if item 170 ] 171 add_option( 172 env, "browsertime.dismiss_cookie_prompt", ";;;".join(parsed_cmds) 173 ) 174 175 cmds = test_site.get("test_cmds", []) 176 if cmds: 177 parsed_cmds = [":::".join([str(i) for i in item]) for item in cmds if item] 178 add_option(env, "browsertime.commands", ";;;".join(parsed_cmds)) 179 180 print("Recording %s to file: %s" % (test_site.get("test_url"), recording_file))