1# Copyright 2018 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5 6from autotest_lib.client.common_lib import error 7from autotest_lib.client.common_lib import utils 8from autotest_lib.client.cros.enterprise import enterprise_policy_base 9 10from telemetry.core import exceptions 11 12 13class policy_AlternateErrorPages( 14 enterprise_policy_base.EnterprisePolicyTest): 15 """ 16 Test effect of policy_AlternateErrorPages policy on Chrome OS. 17 18 """ 19 version = 1 20 21 POLICY_NAME = 'AlternateErrorPagesEnabled' 22 SUGGESTED = 'Did you mean http://localhost-8080.com/?' 23 RESULTS_DICT = { 24 True: SUGGESTED, 25 False: 'Checking the connection', 26 None: SUGGESTED} 27 28 def _alt_page_check(self, policy_value): 29 """ 30 Navigates to an invalid webpage, then checks the first item of the 31 suggestion list. 32 33 @param policy_value: bool or None, the setting of the policy. 34 35 """ 36 search_box = '#suggestions-list li' 37 tab = self.navigate_to_url('http://localhost:8080/') 38 39 get_text = "document.querySelector('{}').innerText".format(search_box) 40 41 def is_page_loaded(): 42 """ 43 Checks to see if page has fully loaded. 44 45 @returns True if loaded False if not. 46 47 """ 48 try: 49 tab.EvaluateJavaScript(get_text) 50 return True 51 except exceptions.EvaluateException: 52 return False 53 54 # Wait for the page to load before checking it. 55 utils.poll_for_condition( 56 lambda: is_page_loaded(), 57 exception=error.TestFail('Page Never loaded!'), 58 timeout=5, 59 sleep_interval=1, 60 desc='Polling for page to load.') 61 62 list_content = tab.EvaluateJavaScript(get_text) 63 64 if self.RESULTS_DICT[policy_value] != list_content: 65 raise error.TestFail( 66 'AlternateErrorPage was not set! Expected the first item in' 67 ' the suggestions-list to be "{}" but received "{}"' 68 .format(self.RESULTS_DICT[policy_value], list_content)) 69 70 def run_once(self, case): 71 """ 72 @param case: Name of the test case to run. 73 74 """ 75 self.setup_case(user_policies={self.POLICY_NAME: case}) 76 self._alt_page_check(case) 77