| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for PyAuto.""" | 5 """Utilities for PyAuto.""" |
| 6 | 6 |
| 7 import httplib |
| 7 import logging | 8 import logging |
| 8 import os | 9 import os |
| 9 import shutil | 10 import shutil |
| 11 import socket |
| 10 import sys | 12 import sys |
| 11 import tempfile | 13 import tempfile |
| 14 import unittest |
| 15 import urlparse |
| 12 import zipfile | 16 import zipfile |
| 13 | 17 |
| 14 | 18 |
| 15 class ExistingPathReplacer(object): | 19 class ExistingPathReplacer(object): |
| 16 """Facilitates backing up a given path (file or dir).. | 20 """Facilitates backing up a given path (file or dir).. |
| 17 | 21 |
| 18 Often you want to manipulate a directory or file for testing but don't want to | 22 Often you want to manipulate a directory or file for testing but don't want to |
| 19 meddle with the existing contents. This class lets you make a backup, and | 23 meddle with the existing contents. This class lets you make a backup, and |
| 20 reinstate the backup when done. A backup is made in an adjacent directory, | 24 reinstate the backup when done. A backup is made in an adjacent directory, |
| 21 so you need to make sure you have write permissions to the parent directory. | 25 so you need to make sure you have write permissions to the parent directory. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 Args: | 188 Args: |
| 185 pyauto: an instance of pyauto.PyUITest. | 189 pyauto: an instance of pyauto.PyUITest. |
| 186 driver: an instance of chrome driver or a web element. | 190 driver: an instance of chrome driver or a web element. |
| 187 xpath: the xpath of the element to wait for. | 191 xpath: the xpath of the element to wait for. |
| 188 | 192 |
| 189 Returns: | 193 Returns: |
| 190 The element if it is found. | 194 The element if it is found. |
| 191 NoSuchElementException if it is not found. | 195 NoSuchElementException if it is not found. |
| 192 """ | 196 """ |
| 193 pyauto.WaitUntil(lambda: len(driver.find_elements_by_xpath(xpath)) > 0) | 197 pyauto.WaitUntil(lambda: len(driver.find_elements_by_xpath(xpath)) > 0) |
| 194 return driver.find_element_by_xpath(xpath) | 198 return driver.find_element_by_xpath(xpath) |
| 199 |
| 200 |
| 201 def DoesUrlExist(url): |
| 202 """Determines whether a resource exists at the given URL. |
| 203 |
| 204 Args: |
| 205 url: URL to be verified. |
| 206 |
| 207 Returns: |
| 208 True if url exists, otherwise False. |
| 209 """ |
| 210 parsed = urlparse.urlparse(url) |
| 211 try: |
| 212 conn = httplib.HTTPConnection(parsed.netloc) |
| 213 conn.request('HEAD', parsed.path) |
| 214 response = conn.getresponse() |
| 215 except (socket.gaierror, socket.error): |
| 216 return False |
| 217 finally: |
| 218 conn.close() |
| 219 # Follow both permanent (301) and temporary (302) redirects. |
| 220 if response.status == 302 or response.status == 301: |
| 221 return DoesUrlExist(response.getheader('location')) |
| 222 return response.status == 200 |
| 223 |
| 224 |
| 225 class _GTestTextTestResult(unittest._TextTestResult): |
| 226 """A test result class that can print formatted text results to a stream. |
| 227 |
| 228 Results printed in conformance with gtest output format, like: |
| 229 [ RUN ] autofill.AutofillTest.testAutofillInvalid: "test desc." |
| 230 [ OK ] autofill.AutofillTest.testAutofillInvalid |
| 231 [ RUN ] autofill.AutofillTest.testFillProfile: "test desc." |
| 232 [ OK ] autofill.AutofillTest.testFillProfile |
| 233 [ RUN ] autofill.AutofillTest.testFillProfileCrazyCharacters: "Test." |
| 234 [ OK ] autofill.AutofillTest.testFillProfileCrazyCharacters |
| 235 """ |
| 236 |
| 237 def __init__(self, stream, descriptions, verbosity): |
| 238 unittest._TextTestResult.__init__(self, stream, descriptions, verbosity) |
| 239 |
| 240 def _GetTestURI(self, test): |
| 241 if sys.version_info[:2] <= (2, 4): |
| 242 return '%s.%s' % (unittest._strclass(test.__class__), |
| 243 test._TestCase__testMethodName) |
| 244 return '%s.%s' % (unittest._strclass(test.__class__), test._testMethodName) |
| 245 |
| 246 def getDescription(self, test): |
| 247 return '%s: "%s"' % (self._GetTestURI(test), test.shortDescription()) |
| 248 |
| 249 def startTest(self, test): |
| 250 unittest.TestResult.startTest(self, test) |
| 251 self.stream.writeln('[ RUN ] %s' % self.getDescription(test)) |
| 252 |
| 253 def addSuccess(self, test): |
| 254 unittest.TestResult.addSuccess(self, test) |
| 255 self.stream.writeln('[ OK ] %s' % self._GetTestURI(test)) |
| 256 |
| 257 def addError(self, test, err): |
| 258 unittest.TestResult.addError(self, test, err) |
| 259 self.stream.writeln('[ ERROR ] %s' % self._GetTestURI(test)) |
| 260 |
| 261 def addFailure(self, test, err): |
| 262 unittest.TestResult.addFailure(self, test, err) |
| 263 self.stream.writeln('[ FAILED ] %s' % self._GetTestURI(test)) |
| 264 |
| 265 |
| 266 class GTestTextTestRunner(unittest.TextTestRunner): |
| 267 """Test Runner for displaying test results in textual format. |
| 268 |
| 269 Results are displayed in conformance with gtest output. |
| 270 """ |
| 271 |
| 272 def __init__(self, verbosity=1): |
| 273 unittest.TextTestRunner.__init__(self, stream=sys.stderr, |
| 274 verbosity=verbosity) |
| 275 |
| 276 def _makeResult(self): |
| 277 return _GTestTextTestResult(self.stream, self.descriptions, self.verbosity) |
| OLD | NEW |