setup-selenium-testing 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
+ """Setup selenium for testing"""
2
+
1
3
  from __future__ import annotations
2
4
 
3
5
  import errno
4
6
  import logging
5
7
  import os as os
6
8
  from enum import Enum
7
- from typing import TYPE_CHECKING, Union
9
+ from typing import TYPE_CHECKING, Optional, Union
8
10
 
9
11
  from selenium import webdriver
10
12
  from selenium.common.exceptions import NoSuchWindowException, WebDriverException
@@ -12,7 +14,7 @@ from selenium.webdriver.chrome.service import Service as ChromeService
12
14
  from selenium.webdriver.common.selenium_manager import SeleniumManager
13
15
  from selenium.webdriver.edge.service import Service as EdgeService
14
16
  from selenium.webdriver.firefox.service import Service as FirefoxService
15
- from semantic_version import Version # type: ignore
17
+ from semantic_version import Version # type: ignore[import-untyped]
16
18
  from typing_extensions import TypeAlias
17
19
 
18
20
  if TYPE_CHECKING:
@@ -22,6 +24,9 @@ if TYPE_CHECKING:
22
24
  from selenium.webdriver.common.options import ArgOptions
23
25
 
24
26
  T_WebDriver: TypeAlias = Union[Firefox, Chrome, Edge]
27
+ T_DrvOpts: TypeAlias = Union[
28
+ webdriver.FirefoxOptions, webdriver.ChromeOptions, webdriver.EdgeOptions
29
+ ]
25
30
 
26
31
  __all__ = ["SetupSelenium"]
27
32
 
@@ -38,12 +43,11 @@ logger = create_logger("sel")
38
43
 
39
44
 
40
45
  def set_logger(logr: logging.Logger) -> None:
41
- """
42
- Set the global logger with a custom logger
43
- """
46
+ """Set the global logger with a custom logger"""
44
47
  # Check if the logger is a valid logger
45
48
  if not isinstance(logr, logging.Logger):
46
- raise ValueError("The logger must be an instance of logging.Logger")
49
+ msg = "logger must be an instance of logging.Logger"
50
+ raise TypeError(msg)
47
51
 
48
52
  # Bind the logger input to the global logger
49
53
  global logger # noqa: PLW0603
@@ -132,9 +136,7 @@ class SetupSelenium:
132
136
  def make_screenshot_path(
133
137
  output_dir: str = "./logs", screenshots: str = "screenshots"
134
138
  ) -> str:
135
- """
136
- Set the output directory for where screenshots should go.
137
- """
139
+ """Set the output directory for where screenshots should go."""
138
140
  output_dir = os.path.abspath(os.path.expanduser(output_dir))
139
141
  if os.path.split(output_dir)[-1].lower() != screenshots:
140
142
  output_dir = os.path.join(output_dir, screenshots)
@@ -152,6 +154,7 @@ class SetupSelenium:
152
154
  ############################################################################
153
155
  @staticmethod
154
156
  def log_options(options: ArgOptions) -> None:
157
+ """Logs the browser option in clean format"""
155
158
  opts = "\n".join(options.arguments)
156
159
  logger.debug(f"{opts}")
157
160
 
@@ -163,7 +166,7 @@ class SetupSelenium:
163
166
  browser_path: str | None = None,
164
167
  install_browser: bool = False,
165
168
  ) -> tuple[str, str]:
166
- """install the webdriver and browser if needed."""
169
+ """Install the webdriver and browser if needed."""
167
170
  browser = Browser[browser.upper()].lower()
168
171
  driver_version = driver_version or None
169
172
 
@@ -203,19 +206,24 @@ class SetupSelenium:
203
206
  log_dir: str = "./logs",
204
207
  binary: str | None = None,
205
208
  driver_path: str | None = None,
209
+ options: Optional[T_DrvOpts] = None,
206
210
  ) -> T_WebDriver:
211
+ """Instantiates the browser driver"""
207
212
  browser = browser.lower()
208
213
  driver: T_WebDriver
209
214
  if browser == Browser.FIREFOX:
215
+ assert options is None or isinstance(options, webdriver.FirefoxOptions)
210
216
  driver = SetupSelenium.firefox(
211
217
  headless=headless,
212
218
  enable_log_driver=enable_log_driver,
213
219
  log_dir=log_dir,
214
220
  binary=binary,
215
221
  driver_path=driver_path,
222
+ options=options,
216
223
  )
217
224
 
218
225
  elif browser == Browser.CHROME:
226
+ assert options is None or isinstance(options, webdriver.ChromeOptions)
219
227
  driver = SetupSelenium.chrome(
220
228
  headless=headless,
221
229
  enable_log_performance=enable_log_performance,
@@ -224,9 +232,11 @@ class SetupSelenium:
224
232
  log_dir=log_dir,
225
233
  binary=binary,
226
234
  driver_path=driver_path,
235
+ options=options,
227
236
  )
228
237
 
229
238
  elif browser == Browser.EDGE:
239
+ assert options is None or isinstance(options, webdriver.EdgeOptions)
230
240
  driver = SetupSelenium.edge(
231
241
  headless=headless,
232
242
  enable_log_performance=enable_log_performance,
@@ -235,15 +245,18 @@ class SetupSelenium:
235
245
  log_dir=log_dir,
236
246
  binary=binary,
237
247
  driver_path=driver_path,
248
+ options=options,
238
249
  )
239
250
 
240
251
  else:
241
- raise ValueError(f"Unknown browser: {browser}")
252
+ msg = f"Unknown browser: {browser}"
253
+ raise ValueError(msg)
242
254
 
243
255
  return driver
244
256
 
245
257
  @staticmethod
246
258
  def firefox_options() -> webdriver.FirefoxOptions:
259
+ """Default options for firefox"""
247
260
  options = webdriver.FirefoxOptions()
248
261
  options.set_capability("unhandledPromptBehavior", "ignore")
249
262
 
@@ -261,8 +274,9 @@ class SetupSelenium:
261
274
  log_dir: str = "./logs",
262
275
  driver_path: str | None = None,
263
276
  binary: str | None = None,
264
- options: webdriver.FirefoxOptions = None,
277
+ options: webdriver.FirefoxOptions | None = None,
265
278
  ) -> webdriver.Firefox:
279
+ """Instantiates firefox geockodriver"""
266
280
  options = options or SetupSelenium.firefox_options()
267
281
  if binary:
268
282
  options.binary_location = binary
@@ -301,6 +315,7 @@ class SetupSelenium:
301
315
 
302
316
  @staticmethod
303
317
  def chrome_options() -> webdriver.ChromeOptions:
318
+ """Default options for chrome"""
304
319
  logger.debug("Setting up chrome options")
305
320
  # The list of options set below mostly came from this StackOverflow post
306
321
  # https://stackoverflow.com/q/48450594/2532408
@@ -332,8 +347,9 @@ class SetupSelenium:
332
347
  log_dir: str = "./logs",
333
348
  driver_path: str | None = None,
334
349
  binary: str | None = None,
335
- options: webdriver.ChromeOptions = None,
350
+ options: webdriver.ChromeOptions | None = None,
336
351
  ) -> webdriver.Chrome:
352
+ """Instantiates chromedriver"""
337
353
  options = options or SetupSelenium.chrome_options()
338
354
  if binary:
339
355
  options.binary_location = binary
@@ -374,12 +390,12 @@ class SetupSelenium:
374
390
  service = ChromeService(
375
391
  executable_path=driver_path,
376
392
  service_args=args,
377
- log_output=logpath,
393
+ log_output=logpath, # type: ignore[arg-type]
378
394
  )
379
395
  else:
380
396
  service = ChromeService(
381
397
  service_args=args,
382
- log_output=logpath,
398
+ log_output=logpath, # type: ignore[arg-type]
383
399
  )
384
400
 
385
401
  driver = webdriver.Chrome(service=service, options=options)
@@ -405,6 +421,7 @@ class SetupSelenium:
405
421
 
406
422
  @staticmethod
407
423
  def set_throttle(driver: webdriver.Chrome):
424
+ """Experimental settings to slow down browser"""
408
425
  # experimental settings to slow down browser
409
426
  # @formatter:off
410
427
  # fmt: off
@@ -433,6 +450,7 @@ class SetupSelenium:
433
450
 
434
451
  @staticmethod
435
452
  def edge_options() -> webdriver.EdgeOptions:
453
+ """Default options for edgedriver"""
436
454
  logger.debug("Setting up edge options")
437
455
  # The list of options set below mostly came from this StackOverflow post
438
456
  # https://stackoverflow.com/q/48450594/2532408
@@ -447,8 +465,6 @@ class SetupSelenium:
447
465
  # edgedriver crashes without these two in linux
448
466
  "--no-sandbox",
449
467
  "--disable-dev-shm-usage",
450
- # it's possible we no longer need to do these
451
- # "--disable-gpu", # https://stackoverflow.com/q/51959986/2532408
452
468
  )
453
469
  options = webdriver.EdgeOptions()
454
470
  for opt in opts:
@@ -464,8 +480,9 @@ class SetupSelenium:
464
480
  log_dir: str = "./logs",
465
481
  driver_path: str | None = None,
466
482
  binary: str | None = None,
467
- options: webdriver.EdgeOptions = None,
483
+ options: webdriver.EdgeOptions | None = None,
468
484
  ) -> webdriver.Edge:
485
+ """Instantiates edgedriver"""
469
486
  options = options or SetupSelenium.edge_options()
470
487
  if binary:
471
488
  options.binary_location = binary
@@ -507,12 +524,12 @@ class SetupSelenium:
507
524
  service = EdgeService(
508
525
  executable_path=driver_path,
509
526
  service_args=args,
510
- log_output=logpath,
527
+ log_output=logpath, # type: ignore[arg-type]
511
528
  )
512
529
  else:
513
530
  service = EdgeService(
514
531
  service_args=args,
515
- log_output=logpath,
532
+ log_output=logpath, # type: ignore[arg-type]
516
533
  )
517
534
  driver = webdriver.Edge(service=service, options=options)
518
535
 
@@ -540,6 +557,7 @@ class SetupSelenium:
540
557
 
541
558
  ############################################################################
542
559
  def set_window_size(self, size: str = "720") -> None:
560
+ """Helper to set the window size after driver has been instantiated."""
543
561
  if size == "max":
544
562
  self.driver.maximize_window()
545
563
  return
@@ -550,17 +568,22 @@ class SetupSelenium:
550
568
  self.driver.set_window_size(width, height)
551
569
 
552
570
  def set_main_window_handle(self, window: str | None = None) -> str:
553
- if not window:
554
- # does the main_window_handle exist and point to an available window?
555
- if not self.main_window_handle:
571
+ """
572
+ maintains the initial window handle as an attribute
573
+
574
+ Most users will never utilize this. It's part of a legacy requirement for
575
+ an old test suite
576
+ """
577
+ # does the main_window_handle exist and point to an available window?
578
+ if not window and not self.main_window_handle:
579
+ try:
580
+ window = self.driver.current_window_handle
581
+ except NoSuchWindowException:
556
582
  try:
557
- window = self.driver.current_window_handle
558
- except NoSuchWindowException:
559
- try:
560
- window = self.driver.window_handles[0]
561
- except WebDriverException:
562
- # Have we closed all the windows?
563
- raise
583
+ window = self.driver.window_handles[0]
584
+ except WebDriverException: # noqa: TRY302
585
+ # Have we closed all the windows?
586
+ raise
564
587
  if window:
565
588
  self.main_window_handle = window
566
589
  return self.main_window_handle
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: setup-selenium-testing
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: Setup Selenium for automation testing
5
5
  Home-page: https://github.com/bandophahita/setup_selenium
6
6
  License: MIT
@@ -21,17 +21,14 @@ Classifier: Programming Language :: Python :: 3 :: Only
21
21
  Provides-Extra: dev
22
22
  Provides-Extra: test
23
23
  Requires-Dist: black ; extra == "dev"
24
- Requires-Dist: coverage ; extra == "dev"
25
- Requires-Dist: flake8 ; extra == "dev"
24
+ Requires-Dist: isort ; extra == "dev"
26
25
  Requires-Dist: mypy ; extra == "dev"
27
- Requires-Dist: pylint ; extra == "dev"
28
26
  Requires-Dist: pytest ; extra == "dev" or extra == "test"
29
27
  Requires-Dist: ruff ; extra == "dev"
30
28
  Requires-Dist: selenium (>=4.7.0)
31
29
  Requires-Dist: semantic-version
32
- Requires-Dist: setuptools ; extra == "dev"
33
30
  Requires-Dist: tox ; extra == "dev" or extra == "test"
34
- Requires-Dist: typing-extensions (>=4.7.1,<5.0.0)
31
+ Requires-Dist: typing-extensions
35
32
  Project-URL: Repository, https://github.com/bandophahita/setup_selenium
36
33
  Description-Content-Type: text/markdown
37
34
 
@@ -0,0 +1,7 @@
1
+ setup_selenium/__init__.py,sha256=Wem42Sg7sr4nq1d_qVeSrxee5YXllJvfex3sBdoiCRM,64
2
+ setup_selenium/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ setup_selenium/selenium_module.py,sha256=4nLGWMXQsUwQPxJWMuq8XnR_qNHQGhyF7ta71SomzrQ,21006
4
+ setup_selenium_testing-0.1.3.dist-info/LICENSE,sha256=KGdE-1D1chm3UNFtfE8x-EpVxhmv2zFx8oltbO8M1qE,1070
5
+ setup_selenium_testing-0.1.3.dist-info/METADATA,sha256=CsHV_im2TaGQtkc2Oe85GAdpusQ6PIiTcEfA53IM_ZI,5385
6
+ setup_selenium_testing-0.1.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ setup_selenium_testing-0.1.3.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- setup_selenium/__init__.py,sha256=Wem42Sg7sr4nq1d_qVeSrxee5YXllJvfex3sBdoiCRM,64
2
- setup_selenium/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- setup_selenium/selenium_module.py,sha256=VjTD-PvkBX7L3IV0sZixDiZxXn9R0cHiLEHYMrQcAqk,19786
4
- setup_selenium_testing-0.1.1.dist-info/LICENSE,sha256=KGdE-1D1chm3UNFtfE8x-EpVxhmv2zFx8oltbO8M1qE,1070
5
- setup_selenium_testing-0.1.1.dist-info/METADATA,sha256=9tt5YPpODqG0xM759xjjtuXxQcSsyo1elplCsmPpES4,5526
6
- setup_selenium_testing-0.1.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- setup_selenium_testing-0.1.1.dist-info/RECORD,,