Chromium Code Reviews| Index: pyautolib/pyauto_utils.py |
| =================================================================== |
| --- pyautolib/pyauto_utils.py (revision 153326) |
| +++ pyautolib/pyauto_utils.py (working copy) |
| @@ -4,11 +4,15 @@ |
| """Utilities for PyAuto.""" |
| +import httplib |
| import logging |
| import os |
| import shutil |
| +import socket |
| import sys |
| import tempfile |
| +import unittest |
| +import urlparse |
| import zipfile |
| @@ -191,4 +195,83 @@ |
| NoSuchElementException if it is not found. |
| """ |
| pyauto.WaitUntil(lambda: len(driver.find_elements_by_xpath(xpath)) > 0) |
| - return driver.find_element_by_xpath(xpath) |
| + return driver.find_element_by_xpath(xpath) |
| + |
| + |
| +def DoesUrlExist(url): |
|
dennis_jeffrey
2012/10/04 21:54:46
Something similar already exists in other pyauto c
nkang
2012/10/04 22:25:58
Originally FetchPrebuiltPyauto was part of this CL
dennis_jeffrey
2012/10/04 22:37:14
Yeah, I think it would be good to remove the metho
dennis_jeffrey
2012/10/05 17:57:23
Please address this comment and then it'll be good
nkang
2012/10/05 18:58:02
Removed this code from fetch_prebuilt_pyauto.py an
|
| + """Determines whether a resource exists at the given URL. |
| + |
| + Args: |
| + url: URL to be verified. |
| + |
| + Returns: |
| + True if url exists, otherwise False. |
| + """ |
| + parsed = urlparse.urlparse(url) |
| + try: |
| + conn = httplib.HTTPConnection(parsed.netloc) |
| + conn.request('HEAD', parsed.path) |
| + response = conn.getresponse() |
| + except (socket.gaierror, socket.error): |
| + return False |
| + finally: |
| + conn.close() |
| + # Follow both permanent (301) and temporary (302) redirects. |
| + if response.status == 302 or response.status == 301: |
| + return DoesUrlExist(response.getheader('location')) |
| + return response.status == 200 |
| + |
| + |
| +class _GTestTextTestResult(unittest._TextTestResult): |
|
dennis_jeffrey
2012/10/04 21:54:46
This code looks like it already exists for webdriv
nkang
2012/10/04 22:25:58
This code also exists in PyAuto.py. PyAuto was ori
dennis_jeffrey
2012/10/04 22:37:14
I would like to see only 1 copy of this code in th
kkania
2012/10/05 15:17:41
It would be nice to just have one copy. I'll take
dennis_jeffrey
2012/10/05 17:57:23
Sounds good - thanks!
|
| + """A test result class that can print formatted text results to a stream. |
| + |
| + Results printed in conformance with gtest output format, like: |
| + [ RUN ] autofill.AutofillTest.testAutofillInvalid: "test desc." |
| + [ OK ] autofill.AutofillTest.testAutofillInvalid |
| + [ RUN ] autofill.AutofillTest.testFillProfile: "test desc." |
| + [ OK ] autofill.AutofillTest.testFillProfile |
| + [ RUN ] autofill.AutofillTest.testFillProfileCrazyCharacters: "Test." |
| + [ OK ] autofill.AutofillTest.testFillProfileCrazyCharacters |
| + """ |
| + |
| + def __init__(self, stream, descriptions, verbosity): |
| + unittest._TextTestResult.__init__(self, stream, descriptions, verbosity) |
| + |
| + def _GetTestURI(self, test): |
| + if sys.version_info[:2] <= (2, 4): |
| + return '%s.%s' % (unittest._strclass(test.__class__), |
| + test._TestCase__testMethodName) |
| + return '%s.%s' % (unittest._strclass(test.__class__), test._testMethodName) |
| + |
| + def getDescription(self, test): |
| + return '%s: "%s"' % (self._GetTestURI(test), test.shortDescription()) |
| + |
| + def startTest(self, test): |
| + unittest.TestResult.startTest(self, test) |
| + self.stream.writeln('[ RUN ] %s' % self.getDescription(test)) |
| + |
| + def addSuccess(self, test): |
| + unittest.TestResult.addSuccess(self, test) |
| + self.stream.writeln('[ OK ] %s' % self._GetTestURI(test)) |
| + |
| + def addError(self, test, err): |
| + unittest.TestResult.addError(self, test, err) |
| + self.stream.writeln('[ ERROR ] %s' % self._GetTestURI(test)) |
| + |
| + def addFailure(self, test, err): |
| + unittest.TestResult.addFailure(self, test, err) |
| + self.stream.writeln('[ FAILED ] %s' % self._GetTestURI(test)) |
| + |
| + |
| +class GTestTextTestRunner(unittest.TextTestRunner): |
| + """Test Runner for displaying test results in textual format. |
| + |
| + Results are displayed in conformance with gtest output. |
| + """ |
| + |
| + def __init__(self, verbosity=1): |
| + unittest.TextTestRunner.__init__(self, stream=sys.stderr, |
| + verbosity=verbosity) |
| + |
| + def _makeResult(self): |
| + return _GTestTextTestResult(self.stream, self.descriptions, self.verbosity) |