~nskaggs/help-app/functional-test-template

« back to all changes in this revision

Viewing changes to internals/tests/functional/help_app/tests/__init__.py

  • Committer: nskaggs
  • Date: 2015-05-26 19:17:29 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20150526191729-6immvahyowz6uxm6
initial version of functional tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
 
 
3
""" Autopilot tests """
 
4
 
 
5
import os
 
6
import subprocess
 
7
 
 
8
from selenium import webdriver
 
9
from selenium.webdriver.chrome.options import Options
 
10
 
 
11
import ubuntuuitoolkit as uitk
 
12
from ubuntuuitoolkit import fixture_setup
 
13
 
 
14
from autopilot import platform
 
15
 
 
16
from testtools.matchers import NotEquals, EndsWith
 
17
from autopilot.matchers import Eventually
 
18
 
 
19
from autopilot.testcase import AutopilotTestCase
 
20
 
 
21
CURRENT_ARCHITECTURE = subprocess.check_output(
 
22
    ["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip()
 
23
CHROMEDRIVER_EXEC_PATH = \
 
24
    "/usr/lib/{}/oxide-qt/chromedriver".format(
 
25
        CURRENT_ARCHITECTURE.decode("utf-8"))
 
26
DEFAULT_WEBVIEW_INSPECTOR_IP = '127.0.0.1'
 
27
DEFAULT_WEBVIEW_INSPECTOR_PORT = 9221
 
28
 
 
29
 
 
30
class HTML5TestCaseBase(AutopilotTestCase):
 
31
    def setUp(self):
 
32
        self.driver = None
 
33
        self.app_proxy = None
 
34
        super().setUp()
 
35
        if platform.model() == 'Desktop':
 
36
            self.patch_environment(
 
37
                'UBUNTU_WEBVIEW_DEVTOOLS_HOST',
 
38
                DEFAULT_WEBVIEW_INSPECTOR_IP)
 
39
            self.patch_environment(
 
40
                'UBUNTU_WEBVIEW_DEVTOOLS_PORT',
 
41
                str(DEFAULT_WEBVIEW_INSPECTOR_PORT))
 
42
        else:
 
43
            self.useFixture(fixture_setup.InitctlEnvironmentVariable(
 
44
                global_=True,
 
45
                UBUNTU_WEBVIEW_DEVTOOLS_HOST=DEFAULT_WEBVIEW_INSPECTOR_IP,
 
46
                UBUNTU_WEBVIEW_DEVTOOLS_PORT=str(
 
47
                    DEFAULT_WEBVIEW_INSPECTOR_PORT)
 
48
            ))
 
49
 
 
50
    def tearDown(self):
 
51
        if self.driver:
 
52
            self.page.close()
 
53
            self.page.quit()
 
54
        # XXX: This should not be there but AP hangs
 
55
        # if we dont extra force the process to be killed
 
56
        # (although AP already tries to kill part of its teardown)
 
57
        if platform.model() == 'Desktop' \
 
58
           and self.app_proxy \
 
59
           and self.app_proxy.process:
 
60
            self.app_proxy.process.kill()
 
61
        super(HTML5TestCaseBase, self).tearDown()
 
62
 
 
63
    def get_webview(self):
 
64
        return self.app_proxy.select_single('UbuntuWebView02')
 
65
 
 
66
    def launch_webdriver(self):
 
67
        options = Options()
 
68
        options.binary_location = ''
 
69
        options.debugger_address = '{}:{}'.format(
 
70
            DEFAULT_WEBVIEW_INSPECTOR_IP,
 
71
            DEFAULT_WEBVIEW_INSPECTOR_PORT)
 
72
 
 
73
        self.driver = webdriver.Chrome(
 
74
            executable_path=CHROMEDRIVER_EXEC_PATH,
 
75
            chrome_options=options)
 
76
 
 
77
        self.assertThat(self.driver, NotEquals(None))
 
78
 
 
79
    @property
 
80
    def page(self):
 
81
        return self.driver
 
82
 
 
83
    def launch_html5_app_inline(self, args):
 
84
        return self.launch_test_application(
 
85
            'ubuntu-html5-app-launcher',
 
86
            *args,
 
87
            emulator_base=uitk.UbuntuUIToolkitCustomProxyObjectBase)
 
88
 
 
89
    def launch_html5_app(self):
 
90
        self.app_proxy = self.launch_html5_app_inline(
 
91
            ['--www={}/{}'.format(
 
92
                os.path.dirname(
 
93
                    os.path.realpath(__file__)),
 
94
                '../../../../www')])
 
95
        self.wait_for_app()
 
96
 
 
97
        self.launch_webdriver()
 
98
 
 
99
    def wait_for_app(self):
 
100
        self.assertThat(self.app_proxy, NotEquals(None))
 
101
        webview = self.get_webview()
 
102
        self.assertThat(
 
103
            lambda: webview.url,
 
104
            Eventually(EndsWith("/index.html")))
 
105