|
1 | | -from selenium.common.exceptions import NoSuchElementException |
| 1 | +from selenium.common.exceptions import NoSuchElementException, TimeoutException |
2 | 2 | from selenium.webdriver.common.by import By |
| 3 | +from selenium.webdriver.support import expected_conditions as EC |
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
3 | 5 |
|
4 | 6 | from utils.driver_exceptions import SToolException |
5 | 7 |
|
@@ -38,7 +40,8 @@ def get_locator(locator_type, locator_text): |
38 | 40 |
|
39 | 41 | locator_text : attribute value |
40 | 42 | """ |
41 | | - return getattr(By, locator_type), locator_text |
| 43 | + locator = locator_type.upper() |
| 44 | + return getattr(By, locator), locator_text |
42 | 45 |
|
43 | 46 |
|
44 | 47 | def get_element(driver, locator_type, locator_text, many=None): |
@@ -67,7 +70,115 @@ def get_element(driver, locator_type, locator_text, many=None): |
67 | 70 | is_multiple = 's' if many else '' |
68 | 71 | func = getattr(driver, f'find_element{is_multiple}') |
69 | 72 | return func(*locator) |
70 | | - except NoSuchElementException as _: |
| 73 | + except NoSuchElementException: |
71 | 74 | return None |
72 | 75 | else: |
73 | 76 | raise SToolException("INVALID_SELECTOR") |
| 77 | + |
| 78 | + |
| 79 | +def click(driver, locator_type, locator_text, click_time=10): |
| 80 | + """Return True if element clicked otherwise return None |
| 81 | +
|
| 82 | + Args: |
| 83 | + locator_type : provide any attribute type |
| 84 | + id,class_name,tag_name |
| 85 | + xpath, css_selector |
| 86 | +
|
| 87 | + locator_text : attribute value |
| 88 | +
|
| 89 | + Returns: |
| 90 | + True : If element clicked |
| 91 | + None : Not clicked or Not Found |
| 92 | + """ |
| 93 | + try: |
| 94 | + elem_locator = get_locator(locator_type, locator_text) |
| 95 | + element = WebDriverWait(driver, click_time).until( |
| 96 | + EC.element_to_be_clickable(elem_locator)) |
| 97 | + element.click() |
| 98 | + return True |
| 99 | + except TimeoutException: |
| 100 | + return None |
| 101 | + except Exception as ex: |
| 102 | + raise SToolException(ex) |
| 103 | + |
| 104 | + |
| 105 | +def get_cookies(driver): |
| 106 | + """Accept driver object and return cookies in dictionary |
| 107 | +
|
| 108 | + Args: |
| 109 | + driver : A selenium WebDriver |
| 110 | +
|
| 111 | + Returns: |
| 112 | + cookies_dict : return cookies in dicionary format if |
| 113 | + no cookies return an empty dictionary |
| 114 | + """ |
| 115 | + cookies = driver.get_cookies() |
| 116 | + cookies_dict = {cookie['name']: cookie['value'] for cookie in cookies} |
| 117 | + return cookies_dict or {} |
| 118 | + |
| 119 | + |
| 120 | +def take_screenshot(driver, element=None): |
| 121 | + """take screenshot of given element if element is |
| 122 | + not given take a full page screeenshot and return |
| 123 | + data in bytes |
| 124 | +
|
| 125 | + Args: |
| 126 | + driver : selenium Webdriver |
| 127 | + element : default None, provide element locator |
| 128 | + example : element=('id','element_id') |
| 129 | +
|
| 130 | + Returns: |
| 131 | + returns byte object,if element not present |
| 132 | + it will return None. |
| 133 | +
|
| 134 | + full screenshot will work only in headless mode. |
| 135 | + """ |
| 136 | + if element and isinstance(element, tuple): |
| 137 | + locator_type, locator_text = element |
| 138 | + ele = get_element(driver, locator_type, locator_text) |
| 139 | + if ele: |
| 140 | + return ele.screenshot_as_png |
| 141 | + return None |
| 142 | + else: |
| 143 | + width = driver.execute_script("return document.body.offsetWidth") |
| 144 | + height = driver.execute_script("return document.body.offsetHeight") |
| 145 | + driver.set_window_size(width, height) |
| 146 | + return driver.get_screenshot_as_png() |
| 147 | + |
| 148 | + |
| 149 | +def display_element(driver, element, hide=None): |
| 150 | + """ hide or show single element |
| 151 | +
|
| 152 | + Args: |
| 153 | + driver : selenium webdriver |
| 154 | + element : an selenium element |
| 155 | + hide : default value is None, to hide element |
| 156 | + hide=1 to display hidden element |
| 157 | + Returns: |
| 158 | + None |
| 159 | + """ |
| 160 | + |
| 161 | + hide_or_show = 'block' if hide else 'None' |
| 162 | + driver.execute_script( |
| 163 | + f"arguments[0].style.display = '{hide_or_show}';", element) |
| 164 | + |
| 165 | + |
| 166 | +def hide_show_elements(driver, elements, hide=None): |
| 167 | + """hide or show multiple elements |
| 168 | +
|
| 169 | + Args: |
| 170 | + driver : selenium webdriver |
| 171 | + elements : list of tuples,[('locator_type','value')] |
| 172 | + example : [('id','id_value')] |
| 173 | + hide : default value is None, to hide element |
| 174 | + hide=1 to display hidden element |
| 175 | +
|
| 176 | + Returns: |
| 177 | + None |
| 178 | + """ |
| 179 | + for element_locator in elements: |
| 180 | + locator_type, locator_value = element_locator |
| 181 | + element_list = get_element(driver, locator_type, locator_value, 1) |
| 182 | + if element_list: |
| 183 | + for element in element_list: |
| 184 | + display_element(driver, element, hide) |
0 commit comments