Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2013 The Chromium 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 import json | |
| 6 import logging | |
| 7 import os | |
| 8 import time | |
| 9 from PIL import Image | |
| 10 | |
| 11 from ..common import cloud_bucket | |
| 12 from ..common import ispy_utils | |
| 13 | |
| 14 | |
| 15 class ChromeUtils(object): | |
| 16 """A utility for using ISpy with Chrome.""" | |
| 17 | |
| 18 def __init__(self, cloud_bucket, version_file, screenshot_func): | |
| 19 """Initializes the utility class. | |
| 20 | |
| 21 Args: | |
| 22 cloud_bucket: a BaseCloudBucket in which to the version file, | |
| 23 expectations and results are to be stored. | |
| 24 version_file: path to the version file in the cloud bucket. The version | |
| 25 file contains a json list of ordered Chrome versions for which | |
| 26 expectations exist. | |
| 27 screenshot_func: a function that returns a PIL.Image. | |
| 28 """ | |
| 29 self._cloud_bucket = cloud_bucket | |
| 30 self._version_file = version_file | |
| 31 self._screenshot_func = screenshot_func | |
| 32 self._ispy = ispy_utils.ISpyUtils(self._cloud_bucket) | |
| 33 with open( | |
| 34 os.path.join(os.path.dirname(__file__), 'wait_on_ajax.js'), 'r') as f: | |
| 35 self._wait_for_unchanging_dom_script = f.read() | |
| 36 | |
| 37 def UpdateExpectationVersion(self, chrome_version): | |
| 38 """Updates the most recent expectation version to the Chrome version. | |
|
frankf
2013/11/06 23:06:40
As discussed offline: just make device/url first c
craigdh
2013/11/06 23:41:07
Done.
| |
| 39 | |
| 40 Should be called after generating a new set of expectations. | |
| 41 | |
| 42 Args: | |
| 43 chrome_version: the chrome version as a string of the form "31.0.123.4". | |
| 44 """ | |
| 45 insert_pos = 0 | |
| 46 expectation_versions = [] | |
| 47 try: | |
| 48 expectation_versions = self._GetExpectationVersionList() | |
| 49 if expectation_versions: | |
| 50 try: | |
| 51 version = self._GetExpectationVersion( | |
| 52 chrome_version, expectation_versions) | |
| 53 if version == chrome_version: | |
| 54 return | |
| 55 insert_pos = expectation_versions.index(version) | |
| 56 print expectation_versions, chrome_version, version, insert_pos | |
| 57 except: | |
| 58 insert_pos = len(expectation_versions) | |
| 59 except cloud_bucket.FileNotFoundError: | |
| 60 pass | |
| 61 expectation_versions.insert(insert_pos, chrome_version) | |
| 62 logging.info('Updating expectation version...') | |
| 63 self._cloud_bucket.UploadFile( | |
| 64 self._version_file, json.dumps(expectation_versions), | |
| 65 'application/json') | |
| 66 | |
| 67 def _GetExpectationVersion(self, chrome_version, expectation_versions): | |
| 68 """Returns the expectation version for the given Chrome version. | |
| 69 | |
| 70 Args: | |
| 71 chrome_version: the chrome version as a string of the form "31.0.123.4". | |
| 72 expectation_versions: Ordered list of Chrome versions for which | |
| 73 expectations exist, as stored in the version file. | |
| 74 | |
| 75 Returns: | |
| 76 Expectation version string. | |
| 77 """ | |
| 78 chrome_version_split = chrome_version.split('.') | |
| 79 # Find the closest version that is not greater than the chrome version. | |
| 80 for version in (v.split('.') for v in expectation_versions): | |
| 81 if version == chrome_version_split: | |
| 82 return chrome_version | |
| 83 for v, c in zip(version, chrome_version_split): | |
| 84 if int(v) < int(c): | |
| 85 return '.'.join(version) | |
| 86 elif int(v) > int(c): | |
| 87 break | |
| 88 raise Exception('No expectation exists for Chrome %s' % chrome_version) | |
| 89 | |
| 90 def _GetExpectationVersionList(self): | |
| 91 """Gets the list of expectation versions from google storage.""" | |
| 92 return json.loads(self._cloud_bucket.DownloadFile(self._version_file)) | |
| 93 | |
| 94 def _GetExpectationNameWithVersion(self, expectation, chrome_version): | |
| 95 """Get the expectation to be used with the current Chrome version. | |
| 96 | |
| 97 Args: | |
| 98 expectation: name for the expectation to generate. | |
| 99 chrome_version: the chrome version as a string of the form "31.0.123.4". | |
| 100 | |
| 101 Returns: | |
| 102 Version as an integer. | |
| 103 """ | |
| 104 version = self._GetExpectationVersion( | |
| 105 chrome_version, self._GetExpectationVersionList()) | |
| 106 return self._CreateExpectationName(expectation, version) | |
| 107 | |
| 108 def _CreateExpectationName(self, expectation, version): | |
| 109 """Create the full expectation name from the expectation and version. | |
| 110 | |
| 111 Args: | |
| 112 expectation: name for the expectation to generate. | |
| 113 version: expectation version. | |
| 114 | |
| 115 Returns: | |
| 116 Full expectation name as a string. | |
| 117 """ | |
| 118 return '%s(%s)' % (expectation, version) | |
| 119 | |
| 120 def GenerateExpectation(self, expectation, chrome_version): | |
| 121 """Take screenshots and store as an Expectation in I-Spy. | |
| 122 | |
| 123 Args: | |
| 124 expectation: name for the expectation to generate. | |
| 125 chrome_version: the chrome version as a string of the form "31.0.123.4". | |
| 126 """ | |
| 127 # https://code.google.com/p/chromedriver/issues/detail?id=463 | |
| 128 time.sleep(1) | |
| 129 expectation_with_version = self._CreateExpectationName( | |
| 130 expectation, chrome_version) | |
| 131 if self._ispy.ExpectationExists(expectation_with_version): | |
| 132 logging.warning( | |
| 133 'I-Spy expectation \'%s\' already exists, overwriting.', | |
| 134 expectation_with_version) | |
| 135 screenshots = [self._screenshot_func() for _ in range(8)] | |
| 136 logging.info('Generating I-Spy expectation...') | |
| 137 self._ispy.GenerateExpectation(expectation_with_version, screenshots) | |
| 138 | |
| 139 def PerformComparison(self, test_run, expectation, chrome_version): | |
| 140 """Take a screenshot and compare it with the given expecation in I-Spy. | |
| 141 | |
| 142 Args: | |
| 143 test_run: name for the test run. | |
| 144 expectation: name for the expectation to compare against. | |
| 145 chrome_version: the chrome version as a string of the form "31.0.123.4". | |
| 146 """ | |
| 147 # https://code.google.com/p/chromedriver/issues/detail?id=463 | |
| 148 time.sleep(1) | |
| 149 screenshot = self._screenshot_func() | |
| 150 logging.info('Performing I-Spy comparison...') | |
| 151 self._ispy.PerformComparison( | |
| 152 test_run, | |
| 153 self._GetExpectationNameWithVersion(expectation, chrome_version), | |
| 154 screenshot) | |
| 155 | |
| 156 def WaitForUnchangingDOM(self, driver): | |
| 157 """Waits for the DOM to stop changing. | |
| 158 | |
| 159 Args: | |
| 160 driver: a chromedriver driver instance. | |
| 161 """ | |
| 162 try: | |
| 163 driver.execute_async_script(self._wait_for_unchanging_dom_script) | |
| 164 except exceptions.TimeoutException: | |
| 165 logging.warning('Timed out waiting for DOM to stop changing') | |
| 166 | |
| OLD | NEW |