Best Python code snippet using ATX
autoupdate_UserData.py
Source:autoupdate_UserData.py
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 be3# found in the LICENSE file.4import logging5import os6from autotest_lib.client.bin import utils7from autotest_lib.client.common_lib import error8from autotest_lib.client.common_lib.cros import chrome9from autotest_lib.client.cros.update_engine import nebraska_wrapper10from autotest_lib.client.cros.update_engine import update_engine_test11class autoupdate_UserData(update_engine_test.UpdateEngineTest):12 """13 Modifies some user settings and checks they were not reset by an update.14 This test is used as part of the server test autoupdate_DataPreserved.15 This test will make use of several private Chrome APIs:16 inputMethodPrivate: chrome/common/extensions/api/input_method_private.json17 languageSettingsPrivate: chrome/common/extensions/api/18 language_settings_private.idl19 settingsPrivate: chrome/common/extensions/api/settings_private.idl20 """21 version = 122 _TEST_FILE = '/home/chronos/user/Downloads/test.txt'23 _US_IME = '_comp_ime_jkghodnilhceideoidjikpgommlajknkxkb:us::eng'24 _US_INTL_IME = '_comp_ime_jkghodnilhceideoidjikpgommlajknkxkb:us:intl:eng'25 _TIME_ZONE_PREF = 'generated.resolve_timezone_by_geolocation_on_off'26 _GET_IME_JS = '''27 new Promise(function(resolve, reject) {28 chrome.inputMethodPrivate.getCurrentInputMethod(29 function(id) {30 resolve(id);31 });32 })33 '''34 _GET_PREF_JS = '''35 new Promise(function(resolve, reject) {36 chrome.settingsPrivate.getPref("%s", function(pref) {37 resolve(pref['value']);38 });39 })40 '''41 _SET_IME_JS = 'chrome.inputMethodPrivate.setCurrentInputMethod("%s")'42 _SET_PREF_JS = ('chrome.settingsPrivate.setPref("%s", %s, '43 'x => console.log(x))')44 def _modify_input_methods(self):45 """ Change default Input Method to US International."""46 current_ime = self._cr.autotest_ext.EvaluateJavaScript(47 self._GET_IME_JS, promise=True)48 logging.info('Current IME is %s', current_ime)49 add_ime_js = ('chrome.languageSettingsPrivate.addInputMethod("%s")' %50 self._US_INTL_IME)51 self._cr.autotest_ext.EvaluateJavaScript(add_ime_js)52 self._cr.autotest_ext.EvaluateJavaScript(self._SET_IME_JS %53 self._US_INTL_IME)54 new_ime = self._cr.autotest_ext.EvaluateJavaScript(self._GET_IME_JS)55 if current_ime == new_ime:56 raise error.TestFail('IME could not be changed before update.')57 def _modify_time_zone(self):58 """Change time zone to be user selected instead of automatic by IP."""59 current_time_zone = self._cr.autotest_ext.EvaluateJavaScript(60 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True)61 logging.info('Calculating timezone by IP: %s', current_time_zone)62 self._cr.autotest_ext.EvaluateJavaScript(63 self._SET_PREF_JS % (self._TIME_ZONE_PREF, 'false'))64 new_timezone = self._cr.autotest_ext.EvaluateJavaScript(65 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True)66 if current_time_zone == new_timezone:67 raise error.TestFail('Timezone detection could not be changed.')68 def _perform_after_update_checks(self):69 """Check the user preferences and files are the same."""70 with chrome.Chrome(dont_override_profile=True,71 autotest_ext=True) as cr:72 # Check test file is still present.73 if not os.path.exists(self._TEST_FILE):74 raise error.TestFail('Test file was not present after update.')75 # Check IME has not changed.76 current_ime = cr.autotest_ext.EvaluateJavaScript(77 self._GET_IME_JS, promise=True)78 if current_ime != self._US_INTL_IME:79 raise error.TestFail('Input method was not preserved after'80 'update. Expected %s, Actual: %s' %81 (self._US_INTL_IME, current_ime))82 # Check that timezone is user selected.83 current_time_zone = cr.autotest_ext.EvaluateJavaScript(84 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True)85 if current_time_zone:86 raise error.TestFail('Time zone detection was changed back to '87 'automatic.')88 def run_once(self, payload_url=None):89 """90 Tests that user settings are not reset by update.91 @param payload_url: The payload url to use.92 """93 if payload_url:94 with nebraska_wrapper.NebraskaWrapper(95 log_dir=self.resultsdir, payload_url=payload_url) as nebraska:96 with chrome.Chrome(autotest_ext=True) as cr:97 self._cr = cr98 utils.run(['echo', 'hello', '>', self._TEST_FILE])99 self._modify_input_methods()100 self._modify_time_zone()101 self._check_for_update(102 nebraska.get_update_url(critical_update=True))103 # Sign out of Chrome and wait for the update to complete.104 # If we waited for the update to complete and then logged out105 # the DUT will auto-reboot and the client test cannot return.106 self._wait_for_update_to_complete()107 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!