blob: 801f14eda8519d85873168e97cf1c0deb66821e1 [file history] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging

import common
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
from autotest_lib.client.common_lib.cros import chromedriver
from autotest_lib.client.cros import httpd

class desktopui_UrlFetchWithChromeDriver(test.test):
    """Test fetching url and cookie using Chrome Driver."""
    version = 1

    def initialize(self, live=True):
        """Initialize the test.

        @param live: Set to True to access external websites. Otherwise, test
                     with localhost http server. Default value is set to True.
        self._live = live
        super(desktopui_UrlFetchWithChromeDriver, self).initialize()

        if self._live:
            self._test_url = ''
            self._expected_title = ''
            self._domain = ''
            self._test_url = 'http://localhost:8000/hello.html'
            self._expected_title = 'Hello World'
            self._domain = 'localhost'
            self._testServer = httpd.HTTPListener(8000, docroot=self.bindir)

    def cleanup(self):
        """Clean up the test environment, e.g., stop local http server."""
        if not self._live and hasattr(self, '_testServer'):
        super(desktopui_UrlFetchWithChromeDriver, self).cleanup()

    def run_once(self):
        """Run the test code."""
        with chromedriver.chromedriver() as chromedriver_instance:
            driver = chromedriver_instance.driver

  'Expected tab title: %s. Got: %s',
                         self._expected_title, driver.title)
            if driver.title != self._expected_title:
                raise error.TestError('Getting title failed, got title: %s'
                                      % driver.title)

            cookie_found = any([cookie for cookie in
                                if cookie['domain'] == self._domain])
            if not cookie_found:
                raise error.TestError('Expected cookie for %s' % self._test_url)