~canonical-platform-qa/snappy-ecosystem-tests/fixing_ci

« back to all changes in this revision

Viewing changes to tests/helpers/__init__.py

  • Committer: iahmad
  • Date: 2017-02-03 07:02:58 UTC
  • Revision ID: iftikhar.ahmad@canonical.com-20170203070258-g46zkumbqhzp527t
snapcraft wrapper, login and logout tests using snapcraft and webinterface, baseclass for selenium based web tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
from selenium import webdriver
 
3
from selenium.webdriver.common.keys import Keys
 
4
from selenium.webdriver.support.ui import WebDriverWait
 
5
from selenium.webdriver.support import expected_conditions as EC
 
6
from selenium.common.exceptions import TimeoutException
 
7
from selenium.webdriver.common.by import By
 
8
import storeconfig
 
9
 
 
10
LOGIN_EMAIL, LOGIN_PASSWORD = storeconfig.get_store_credentials()
 
11
STORE_URL = storeconfig.get_store_web_url()
 
12
 
 
13
# default PAGE and ELEMENTS load timeout
 
14
PAGE_LOAD_TIMEOUT = 30
 
15
 
 
16
 
 
17
class UbuntuStoreWebTestsBase(unittest.TestCase):
 
18
 
 
19
    @classmethod
 
20
    def setUpClass(cls):
 
21
        pass
 
22
 
 
23
    def setUp(self):
 
24
        #
 
25
        # TODO - add support for multiple type browsers including Chrome, FF and IE
 
26
        options = webdriver.ChromeOptions()
 
27
        options.add_argument("--start-maximized")
 
28
        self.driver = webdriver.Chrome(chrome_options=options)
 
29
 
 
30
    def wait_for_element(self, locator, timeout=PAGE_LOAD_TIMEOUT):
 
31
        """Wait of element to be loaded  for the given timeout,
 
32
        default timeout is as per PAGE_LOAD_TIMEOUT variable"""
 
33
        wait = WebDriverWait(self.driver, timeout)
 
34
        try:
 
35
            element_present = EC.presence_of_element_located(locator)
 
36
            wait.until(element_present)
 
37
        except TimeoutException:
 
38
            print ("Timed out waiting for "+locator[1]+" page")
 
39
 
 
40
    def wait_for_page_title(self, title, timeout=PAGE_LOAD_TIMEOUT):
 
41
        """Wait for the target page title for given timeout,
 
42
        default timeout is as per PAGE_LOAD_TIMEOUT variable"""
 
43
        wait = WebDriverWait(self.driver, timeout)
 
44
        try:
 
45
 
 
46
            find_title = EC.title_is(title)
 
47
            wait.until(find_title)
 
48
 
 
49
        except TimeoutException:
 
50
            print ("Timed out waiting for "+title+ " page")
 
51
 
 
52
    def wait_for_visible(self, locator, timeout=PAGE_LOAD_TIMEOUT):
 
53
        """Wait for the element to be visible for given timeout,
 
54
        default timeout is as per PAGE_LOAD_TIMEOUT variable"""
 
55
        wait = WebDriverWait(self.driver, timeout)
 
56
        try:
 
57
            element_visible = EC.visibility_of_element_located((By.XPATH, "/html/body/nav/div/div[1]/span"))
 
58
            wait.until(element_visible)
 
59
        except TimeoutException:
 
60
            print ("Timed out waiting for element to be visible")
 
61
 
 
62
    def wait_for_clickable(self, locator, timeout=PAGE_LOAD_TIMEOUT):
 
63
        """Wait for the element to be visible for given timeout,
 
64
        default timeout is as per PAGE_LOAD_TIMEOUT variable"""
 
65
        wait = WebDriverWait(self.driver, timeout)
 
66
        try:
 
67
            element_clickable = EC.element_to_be_clickable((By.XPATH, "/html/body/nav/div/div[1]/span"))
 
68
            wait.until(element_clickable)
 
69
        except TimeoutException:
 
70
            print ("Timed out waiting for element to be clickable")
 
71
 
 
72
    def login(self):
 
73
        """Login to web interface, following the UI flow which end user would use"""
 
74
        driver = self.driver
 
75
        driver.get(STORE_URL)
 
76
 
 
77
        signin_link = driver.find_element_by_link_text("Sign in or register")
 
78
        signin_link.click()
 
79
        self.wait_for_element((By.ID, 'login-form'))
 
80
        login_form = driver.find_element_by_id("login-form")
 
81
        email = login_form.find_element_by_id("id_email")
 
82
        email.send_keys(LOGIN_EMAIL)
 
83
        password = login_form.find_element_by_id("id_password")
 
84
        password.send_keys(LOGIN_PASSWORD)
 
85
        login_form.submit()
 
86
        self.wait_for_element((By.NAME, 'decideform'))
 
87
        decide_form = driver.find_element_by_name("decideform")
 
88
        decide_form.submit()
 
89
        self.wait_for_page_title("Your packages")
 
90
 
 
91
    def logout(self):
 
92
        """Logout of web interface, following the UI flow which end user would use"""
 
93
        driver = self.driver
 
94
        self.wait_for_element((By.CLASS_NAME, "b-dropdown__arrow_light"))
 
95
        #
 
96
        # extremely ugly way to reach the desired element by xpath, a bug will be
 
97
        # raised to uniquely identify the dropdown arrows
 
98
        self.wait_for_clickable((By.XPATH, "/html/body/nav/div/div[1]/span"))
 
99
        down_arrow = driver.find_element_by_xpath("/html/body/nav/div/div[1]/span")
 
100
        #
 
101
        # There is a bug with Firefox webdriver when clicking elements,
 
102
        # as sometime click doesn't have any effect, to workaround that
 
103
        # multiple attempts are made. However everything works fine with chrome webdriver.
 
104
 
 
105
        for t in range(0,2):
 
106
            wait = WebDriverWait(self.driver, 1)
 
107
            try:
 
108
                down_arrow.click()
 
109
                element_present = EC.presence_of_element_located((By.CLASS_NAME, "b-dropdown_open"))
 
110
                wait.until(element_present)
 
111
                break
 
112
            except TimeoutException:
 
113
                print ("Failed to open the accounts dropdown menu card, Trying again")
 
114
 
 
115
 
 
116
        self.wait_for_visible((By.LINK_TEXT, "Log out"))
 
117
        logout_link = driver.find_element_by_link_text("Log out")
 
118
        logout_link.click()
 
119
 
 
120
        self.wait_for_page_title("Sign in to see your packages")
 
121
 
 
122
    def tearDown(self):
 
123
        self.driver.close()
 
124
        self.driver.quit()
 
125
 
 
126
    @classmethod
 
127
    def tearDownClass(cls):
 
128
        pass