|
3 | 3 | from selenium import webdriver
|
4 | 4 | from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
5 | 5 | from selenium.webdriver.common.by import By
|
| 6 | +from selenium.webdriver.firefox.webdriver import WebDriver |
6 | 7 | from selenium.webdriver.support import expected_conditions as EC
|
7 |
| -from selenium.webdriver.support.ui import WebDriverWait |
| 8 | +from selenium.webdriver.support.ui import Select, WebDriverWait |
8 | 9 |
|
9 | 10 | from s_tool.exceptions import SToolException
|
10 | 11 |
|
@@ -44,7 +45,7 @@ def get_locator(locator_type: str, locator_text: str) -> tuple:
|
44 | 45 |
|
45 | 46 |
|
46 | 47 | def get_element(
|
47 |
| - driver: webdriver, locator_type: str, locator_text: str, many: bool = None |
| 48 | + driver: webdriver, locator_text: str, locator_type: str = "id", many: bool = None |
48 | 49 | ):
|
49 | 50 | """Get element using locator type and locator text
|
50 | 51 |
|
@@ -139,7 +140,7 @@ def take_screenshot(driver: webdriver, element: tuple = None) -> Union[bytes, No
|
139 | 140 | """
|
140 | 141 | if element and isinstance(element, tuple):
|
141 | 142 | locator_type, locator_text = element
|
142 |
| - ele = get_element(driver, locator_type, locator_text) |
| 143 | + ele = get_element(driver, locator_text, locator_type) |
143 | 144 | if ele:
|
144 | 145 | return ele.screenshot_as_png
|
145 | 146 | return None
|
@@ -181,7 +182,70 @@ def hide_show_elements(driver: webdriver, elements: list, hide: bool = None) ->
|
181 | 182 | """
|
182 | 183 | for element_locator in elements:
|
183 | 184 | locator_type, locator_value = element_locator
|
184 |
| - element_list = get_element(driver, locator_type, locator_value, many=True) |
| 185 | + element_list = get_element(driver, locator_value, locator_type, many=True) |
185 | 186 | if element_list:
|
186 | 187 | for element in element_list:
|
187 | 188 | display_element(driver, element, hide)
|
| 189 | + |
| 190 | + |
| 191 | +def select_option(element, _value, _by=0): |
| 192 | + """Select Dropdown option |
| 193 | +
|
| 194 | + Args: |
| 195 | + element : selenium element |
| 196 | + _value : str,value,text |
| 197 | + _by : an int value from range(3) |
| 198 | +
|
| 199 | + 0: default select option using by value |
| 200 | + 1: select using visible text |
| 201 | + 2: select using index but also provide _value as int |
| 202 | + Returns: |
| 203 | + None |
| 204 | + """ |
| 205 | + |
| 206 | + if not element and element.tag_name != "select": |
| 207 | + raise SToolException("INVALIDELEMENT") |
| 208 | + |
| 209 | + elif _by not in [0, 1, 2]: |
| 210 | + raise SToolException("INVALIDSELECTOR") |
| 211 | + |
| 212 | + elif _by == 2 and type(_value) is not int: |
| 213 | + raise SToolException("INVALIDVALUE") |
| 214 | + |
| 215 | + else: |
| 216 | + select = Select(element) |
| 217 | + select_type = { |
| 218 | + 0: getattr(select, "select_by_value"), |
| 219 | + 1: getattr(select, "select_by_visible_text"), |
| 220 | + 2: getattr(select, "select_by_index"), |
| 221 | + } |
| 222 | + |
| 223 | + select_type[_by](_value) |
| 224 | + |
| 225 | + |
| 226 | +def fill(driver: WebDriver, kwargs: dict) -> None: |
| 227 | + """Fill information in html element using name attribute |
| 228 | +
|
| 229 | + Args: |
| 230 | + driver : selenium Webdriver |
| 231 | + kwargs : dict,{name:value_to_select_or_enter} |
| 232 | +
|
| 233 | + _by : default 0 , used for select dropdown |
| 234 | +
|
| 235 | + """ |
| 236 | + |
| 237 | + for name, value in kwargs.items(): |
| 238 | + element = get_element(driver, name, "name") |
| 239 | + if element.tag_name == "select": |
| 240 | + # Select Dropdown value |
| 241 | + select_option(element, value, _by=0) |
| 242 | + elif element.get_attribute("type") == "radio": |
| 243 | + # Click on radio element using value |
| 244 | + radio_element = get_element(driver, f'//input[@value="{value}"]', "xpath") |
| 245 | + radio_element.click() |
| 246 | + elif element.tag_name == "input": |
| 247 | + # input,textarea add values |
| 248 | + element.clear() |
| 249 | + element.send_keys(value) |
| 250 | + else: |
| 251 | + raise SToolException("NOTIMPLEMENTED") |
0 commit comments