55from utilities .settings import SCREENSHOT_PATH
66from selenium .webdriver .chrome .options import Options
77
8-
98headless_options = Options ()
109headless_options .add_argument ("--headless" )
1110
12-
1311class WebBrowser :
14-
15- def __init__ (self , browser , implicit_wait = 3 , screenshot_path = SCREENSHOT_PATH ):
12+ """
13+ Class representing a web browser instance.
14+ """
15+
16+ def __init__ (self , browser : str , implicit_wait : int = 3 , screenshot_path : str = SCREENSHOT_PATH ):
17+ """
18+ Initialize the WebBrowser instance.
19+
20+ Args:
21+ browser (str): The browser to use.
22+ implicit_wait (int): The implicit wait time.
23+ screenshot_path (str): The path to save screenshots.
24+ """
1625 self .driver = self .initialize_browser (browser )
1726 self .implicit_wait = implicit_wait
1827 self .screenshot_path = screenshot_path
1928
20- def initialize_browser (self , browser ):
21- # Initialize the WebDriver instance
29+ def initialize_browser (self , browser : str ) -> webdriver :
30+ """
31+ Initialize the WebDriver instance.
32+
33+ Args:
34+ browser (str): The browser to initialize.
35+
36+ Returns:
37+ webdriver: The WebDriver instance.
38+ """
2239 if browser == 'Chrome' :
2340 return webdriver .Chrome ()
2441 elif browser == 'Edge' :
@@ -32,43 +49,139 @@ def initialize_browser(self, browser):
3249 else :
3350 raise Exception (f'Browser "{ browser } " is not supported' )
3451
35- def open_browser (self , url ):
52+ def open_browser (self , url : str ) -> None :
53+ """
54+ Open the browser and navigate to the given URL.
55+
56+ Args:
57+ url (str): The URL to navigate to.
58+
59+ Returns:
60+ None
61+ """
3662 self .driver .get (url )
3763 self .driver .maximize_window ()
3864
39- def close_browser (self ):
65+ def close_browser (self ) -> None :
66+ """
67+ Close the browser.
68+
69+ Returns:
70+ None
71+ """
4072 self .driver .quit ()
4173
42- def get_element (self , by_locator ):
74+ def get_element (self , by_locator : tuple ):
75+ """
76+ Get the web element identified by the locator.
77+
78+ Args:
79+ by_locator (tuple): The locator strategy and value.
80+
81+ Returns:
82+ web element: The web element.
83+ """
4384 return WebDriverWait (self .driver , self .implicit_wait ).until (EC .visibility_of_element_located (by_locator ))
4485
45- def get_element_text (self , by_locator ):
86+ def get_element_text (self , by_locator : tuple ) -> str :
87+ """
88+ Get the text of the web element identified by the locator.
89+
90+ Args:
91+ by_locator (tuple): The locator strategy and value.
92+
93+ Returns:
94+ str: The text of the web element.
95+ """
4696 return self .get_element (by_locator ).text
4797
48- def type_element (self , by_locator , text ):
98+ def type_element (self , by_locator : tuple , text : str ) -> None :
99+ """
100+ Type text into the web element identified by the locator.
101+
102+ Args:
103+ by_locator (tuple): The locator strategy and value.
104+ text (str): The text to type into the web element.
105+
106+ Returns:
107+ None
108+ """
49109 self .get_element (by_locator ).send_keys (text )
50110
51- def click_element (self , by_locator ):
111+ def click_element (self , by_locator : tuple ) -> None :
112+ """
113+ Click on the web element identified by the locator.
114+
115+ Args:
116+ by_locator (tuple): The locator strategy and value.
117+
118+ Returns:
119+ None
120+ """
52121 self .get_element (by_locator ).click ()
53122
54- def send_keys (self , by_locator , key ):
123+ def send_keys (self , by_locator : tuple , key ) -> None :
124+ """
125+ Send keys to the web element identified by the locator.
126+
127+ Args:
128+ by_locator (tuple): The locator strategy and value.
129+ key: The keys to send.
130+
131+ Returns:
132+ None
133+ """
55134 self .get_element (by_locator ).send_keys (key )
56135
57- def is_element_visible (self , by_locator ):
136+ def is_element_visible (self , by_locator : tuple ) -> bool :
137+ """
138+ Check if the web element identified by the locator is visible.
139+
140+ Args:
141+ by_locator (tuple): The locator strategy and value.
142+
143+ Returns:
144+ bool: True if the element is visible, False otherwise.
145+ """
58146 return self .get_element (by_locator ).is_displayed ()
59147
60- def check_if_element_exists (self , by_locator ):
148+ def check_if_element_exists (self , by_locator : tuple ) -> bool :
149+ """
150+ Check if the web element identified by the locator exists.
151+
152+ Args:
153+ by_locator (tuple): The locator strategy and value.
154+
155+ Returns:
156+ bool: True if the element exists, False otherwise.
157+ """
61158 try :
62159 self .driver .find_element (* by_locator )
63160 return True
64161
65- # NoSuchElementException thrown if not present
66162 except NoSuchElementException :
67163 return False
68164
69- def find_elements (self , by_locator ):
165+ def find_elements (self , by_locator : tuple ):
166+ """
167+ Find all web elements identified by the locator.
168+
169+ Args:
170+ by_locator (tuple): The locator strategy and value.
171+
172+ Returns:
173+ list: List of web elements.
174+ """
70175 return self .driver .find_elements (* by_locator )
71176
72- def take_screenshot (self , name ):
73- # take screenshot
177+ def take_screenshot (self , name : str ) -> None :
178+ """
179+ Take a screenshot and save it with the given name.
180+
181+ Args:
182+ name (str): The name to save the screenshot.
183+
184+ Returns:
185+ None
186+ """
74187 self .driver .save_screenshot (self .screenshot_path + name + '.png' )
0 commit comments