Skip to content

Commit 3e5d40c

Browse files
author
ravishankar
committed
fill form function added
1 parent d7c166b commit 3e5d40c

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

s_tool/driver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from s_tool.utils import (
1010
click,
1111
current_url,
12+
fill,
1213
get_cookies,
1314
get_element,
1415
get_session,
@@ -82,14 +83,15 @@ def _load_driver(self):
8283

8384
def _load_methods(self):
8485
self.session = partial(get_session, self.driver)
85-
self.visit = partial(visit, self.driver)
86+
self.get = partial(visit, self.driver)
8687
self.text = partial(page_source, self.driver)
8788
self.url = partial(current_url, self.driver)
8889
self.element = partial(get_element, self.driver)
8990
self.click = partial(click, self.driver)
9091
self.cookies = partial(get_cookies, self.driver)
9192
self.screenshot = partial(take_screenshot, self.driver)
9293
self.hide = partial(hide_show_elements, self.driver)
94+
self.fill = partial(fill, self.driver)
9395

9496

9597
if __name__ == "__main__":

s_tool/utils.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from selenium import webdriver
44
from selenium.common.exceptions import NoSuchElementException, TimeoutException
55
from selenium.webdriver.common.by import By
6+
from selenium.webdriver.firefox.webdriver import WebDriver
67
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
89

910
from s_tool.exceptions import SToolException
1011

@@ -185,3 +186,66 @@ def hide_show_elements(driver: webdriver, elements: list, hide: bool = None) ->
185186
if element_list:
186187
for element in element_list:
187188
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, "xpath", f'//input[@value="{value}"]')
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")

tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_select():
99

1010
# Sample File run from local test folder
1111
index_file = "tests/index.html"
12-
obj.visit(f"file://{abspath(index_file)}")
12+
obj.get(f"file://{abspath(index_file)}")
1313

1414
# Test dropdown options with element id
1515
options_dicts = select_options(obj.element("id", "sel1"))

0 commit comments

Comments
 (0)