Skip to content

Commit 3916036

Browse files
chavareraravishankar
andauthored
fill form function added (#18)
* fill form function added * Added test case to verify select_dropdown function * table parser added * input,textarea,fileupload,checkbox,radiobutton added * form filling method added * testcases added for dropdown and table * Added firefox tests Co-authored-by: ravishankar <ravishankar@advarisk.com>
1 parent d7c166b commit 3916036

File tree

7 files changed

+419
-239
lines changed

7 files changed

+419
-239
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/parser.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from s_tool.exceptions import SToolException
12
from s_tool.utils import get_element
23

34

@@ -14,7 +15,7 @@ def select_options(element, swap=None, text_exclude=[]):
1415
"""
1516
option_dict = dict()
1617
if element and hasattr(element, "tag_name") and element.tag_name == "select":
17-
options = get_element(element, "tag_name", "option", many=True)
18+
options = get_element(element, "option", "tag_name", many=True)
1819
for option in options:
1920
func = option.get_attribute
2021
text, value = func("text"), func("value")
@@ -24,3 +25,31 @@ def select_options(element, swap=None, text_exclude=[]):
2425
option_dict[value] = text
2526

2627
return option_dict
28+
29+
30+
def get_table(table):
31+
"""Return list of rows including header and footer of given table
32+
33+
Args:
34+
A selenium table element
35+
36+
Returns:
37+
return list of row list,
38+
return [] if table not valid
39+
40+
"""
41+
results = []
42+
if table and hasattr(table, "tag_name") and table.tag_name == "table":
43+
for rows in table.find_elements_by_tag_name("tr"):
44+
cell_data = []
45+
for cell in rows.find_elements_by_xpath("td | th"):
46+
colspan = cell.get_attribute("colspan")
47+
cell_text = cell.text
48+
if colspan:
49+
cell_data.extend([cell_text] + [""] * (int(colspan) - 1))
50+
else:
51+
cell_data.append(cell_text)
52+
results.append(cell_data)
53+
else:
54+
raise SToolException("INVALIDTABLE")
55+
return results

s_tool/utils.py

Lines changed: 68 additions & 4 deletions
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

@@ -44,7 +45,7 @@ def get_locator(locator_type: str, locator_text: str) -> tuple:
4445

4546

4647
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
4849
):
4950
"""Get element using locator type and locator text
5051
@@ -139,7 +140,7 @@ def take_screenshot(driver: webdriver, element: tuple = None) -> Union[bytes, No
139140
"""
140141
if element and isinstance(element, tuple):
141142
locator_type, locator_text = element
142-
ele = get_element(driver, locator_type, locator_text)
143+
ele = get_element(driver, locator_text, locator_type)
143144
if ele:
144145
return ele.screenshot_as_png
145146
return None
@@ -181,7 +182,70 @@ def hide_show_elements(driver: webdriver, elements: list, hide: bool = None) ->
181182
"""
182183
for element_locator in elements:
183184
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)
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, 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")

tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from os.path import abspath
2+
3+
index_file = "tests/index.html"
4+
TEST_URL = f"file://{abspath(index_file)}"

0 commit comments

Comments
 (0)