GoogleGit

blob: 801f14eda8519d85873168e97cf1c0deb66821e1 [file] [log] [blame]
  1. # Copyright (c) 2013 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. import logging
  5. import common
  6. from autotest_lib.client.bin import test
  7. from autotest_lib.client.common_lib import error
  8. from autotest_lib.client.common_lib.cros import chromedriver
  9. from autotest_lib.client.cros import httpd
  10. class desktopui_UrlFetchWithChromeDriver(test.test):
  11. """Test fetching url and cookie using Chrome Driver."""
  12. version = 1
  13. def initialize(self, live=True):
  14. """Initialize the test.
  15. @param live: Set to True to access external websites. Otherwise, test
  16. with localhost http server. Default value is set to True.
  17. """
  18. self._live = live
  19. super(desktopui_UrlFetchWithChromeDriver, self).initialize()
  20. if self._live:
  21. self._test_url = 'http://www.msn.com/'
  22. self._expected_title = 'MSN.com'
  23. self._domain = '.msn.com'
  24. else:
  25. self._test_url = 'http://localhost:8000/hello.html'
  26. self._expected_title = 'Hello World'
  27. self._domain = 'localhost'
  28. self._testServer = httpd.HTTPListener(8000, docroot=self.bindir)
  29. self._testServer.run()
  30. def cleanup(self):
  31. """Clean up the test environment, e.g., stop local http server."""
  32. if not self._live and hasattr(self, '_testServer'):
  33. self._testServer.stop()
  34. super(desktopui_UrlFetchWithChromeDriver, self).cleanup()
  35. def run_once(self):
  36. """Run the test code."""
  37. with chromedriver.chromedriver() as chromedriver_instance:
  38. driver = chromedriver_instance.driver
  39. driver.delete_all_cookies()
  40. driver.get(self._test_url)
  41. logging.info('Expected tab title: %s. Got: %s',
  42. self._expected_title, driver.title)
  43. if driver.title != self._expected_title:
  44. raise error.TestError('Getting title failed, got title: %s'
  45. % driver.title)
  46. cookie_found = any([cookie for cookie in
  47. driver.get_cookies()
  48. if cookie['domain'] == self._domain])
  49. if not cookie_found:
  50. raise error.TestError('Expected cookie for %s' % self._test_url)