summaryrefslogtreecommitdiff
path: root/tests/testcases/common.py
blob: e5b4851342268aadc8b60101c209ebcb39a5761d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'''
Common functions used by test cases

'''


from time import sleep,strftime
import os
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC



# global pointer to chrome driver
driver = None

# keeps incrementing by 1 for screenshots saved
image_counter = 1

# global ZM portal data
testConfig = {
        'portal':None,
        'user':None,
        'password':None,
        'use_auth':False,
        'use_zm_auth':False,
        'use_basic_auth':False,
        'basic_user':None,
        'basic_password':None,
        'screenshot_dir':'./screenshots'

}

def log(s):
    print (strftime("%H:%M:%S") + ": " + s)


# central function to save a screenshot
def take_screenshot(id,fname):
    global image_counter
    log ('Taking screenshot')
    if fname == None:
        fname = id+'-image.png'
    fname = '{:03d}-'.format(image_counter)+fname
    driver.get_screenshot_as_file(testConfig['screenshot_dir']+'/'+fname)
    log ('Screenshot stored in '+fname)
    image_counter = image_counter + 1

# generic element clicker, will try retry times
# because sometimes, async loaders interfere
def _click_with_retry(element,max_retry=3):
        retry_count = 1
        while retry_count <= max_retry:
                try:
                        element.click()
                except:
                        log ('click error, try #{}...'.format(retry_count))
                        retry_count = retry_count + 1
                        sleep(2)
                else:
                        retry_count = max_retry + 1
                        pass


# makes sure we can see the element to avoid out of view issues
def _goto_element(e):
    driver.execute_script("arguments[0].scrollIntoView();", e)

# waits for an element to load
# allows you to also specify if you want a screenshot after it comes in
def _wait_for_id(id=id,dur=15, save_screenshot=False, save_screenshot_file=None):
    log ('Waiting for '+id+'...')
    WebDriverWait(driver, dur).until(EC.presence_of_element_located((By.ID, id)))
    # angular refresh?
    sleep(0.2)
    if save_screenshot:
        take_screenshot(id,save_screenshot_file)



def get_element_attributes(id=id, save_screenshot=False, save_screenshot_file=None):
    _wait_for_id(id=id, save_screenshot=save_screenshot, save_screenshot_file = save_screenshot_file)
    element = driver.find_element_by_id(id)
    return element

# handle ion-alerts. Only single button for now. May extend later if I need
def click_popup(txt='ok', save_screenshot=False, save_screenshot_file=None):
    log ('Waiting for popup...')
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'popup-buttons')))
    if save_screenshot:
        take_screenshot(None,'popup-results.png')
    buttons = driver.find_element_by_class_name('popup-buttons')
    buttons  = buttons.find_elements_by_tag_name('button')
    for element in buttons:
        if element.text.lower() == txt:
                log ('click_popup: clicking '+element.text)
                _click_with_retry(element)
               
        else:
                log ('click_popup: skipping '+element.text)
        
                
# handle ion-toggle
def tap_toggle(id, save_screenshot=False, save_screenshot_file=None):
    _wait_for_id(id=id, save_screenshot=save_screenshot, save_screenshot_file = save_screenshot_file)
    element = driver.find_element_by_id(id)
    _goto_element(element)
    element = element.find_element_by_tag_name('label')
    _click_with_retry(element)
   

# generates click event for any web element id
def click_item(id=id, save_screenshot=False, save_screenshot_file=None, retry=3):
    _wait_for_id(id=id, save_screenshot=save_screenshot, save_screenshot_file = save_screenshot_file)
    element = driver.find_element_by_id(id)
    _goto_element(element)
    _click_with_retry(element,retry)


    #sleep(wait)

# generated double click event for any web element id
def dbl_click_item(id=id, save_screenshot=False, save_screenshot_file=None):
    _wait_for_id(id=id, save_screenshot=save_screenshot, save_screenshot_file = save_screenshot_file)
    element = driver.find_element_by_id(id)
    actions = TouchActions(driver)
    actions.double_tap(element)
    actions.perform() 

# handles text input
def input_item(id=id,txt="you forgot to specify text", save_screenshot=False, save_screenshot_file=None):
    _wait_for_id(id=id, save_screenshot=save_screenshot, save_screenshot_file = save_screenshot_file)
    element = driver.find_element_by_id(id)
    _goto_element(element)
    element.clear()
    element.send_keys(txt)
    driver.hide_keyboard()
    sleep(1)
    #sleep(wait)