setup-selenium-testing 0.3.0__py3-none-any.whl → 1.0.0__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.
- setup_selenium/setup_selenium.py +0 -89
- {setup_selenium_testing-0.3.0.dist-info → setup_selenium_testing-1.0.0.dist-info}/METADATA +40 -24
- setup_selenium_testing-1.0.0.dist-info/RECORD +7 -0
- setup_selenium_testing-0.3.0.dist-info/RECORD +0 -7
- {setup_selenium_testing-0.3.0.dist-info → setup_selenium_testing-1.0.0.dist-info}/LICENSE +0 -0
- {setup_selenium_testing-0.3.0.dist-info → setup_selenium_testing-1.0.0.dist-info}/WHEEL +0 -0
setup_selenium/setup_selenium.py
CHANGED
@@ -2,14 +2,12 @@
|
|
2
2
|
|
3
3
|
from __future__ import annotations
|
4
4
|
|
5
|
-
import errno
|
6
5
|
import logging
|
7
6
|
import os as os
|
8
7
|
from enum import Enum
|
9
8
|
from typing import TYPE_CHECKING, Optional, Union
|
10
9
|
|
11
10
|
from selenium import __version__, webdriver
|
12
|
-
from selenium.common.exceptions import NoSuchWindowException, WebDriverException
|
13
11
|
from selenium.webdriver.chrome.service import Service as ChromeService
|
14
12
|
from selenium.webdriver.common.selenium_manager import SeleniumManager
|
15
13
|
from selenium.webdriver.edge.service import Service as EdgeService
|
@@ -18,7 +16,6 @@ from semantic_version import Version # type: ignore[import-untyped]
|
|
18
16
|
from typing_extensions import TypeAlias
|
19
17
|
|
20
18
|
if TYPE_CHECKING:
|
21
|
-
from collections.abc import Mapping
|
22
19
|
|
23
20
|
from selenium.webdriver import Chrome, Edge, Firefox
|
24
21
|
from selenium.webdriver.common.options import ArgOptions
|
@@ -68,27 +65,10 @@ class Browser(str, Enum):
|
|
68
65
|
################################################################################
|
69
66
|
################################################################################
|
70
67
|
class SetupSelenium:
|
71
|
-
DIMENSIONS: Mapping[str, tuple[int, int]] = {
|
72
|
-
# ratio 4:3
|
73
|
-
"1024": (1024, 768),
|
74
|
-
"1280": (1280, 960),
|
75
|
-
"1600": (1600, 1200),
|
76
|
-
"1920": (1920, 1440),
|
77
|
-
# ratio 16:9
|
78
|
-
"720": (1280, 720),
|
79
|
-
"1080": (1920, 1080),
|
80
|
-
"1440": (2560, 1440),
|
81
|
-
"2160": (3840, 2160), # 4k
|
82
|
-
"4320": (7680, 4320), # 8k
|
83
|
-
}
|
84
|
-
|
85
68
|
def __init__(
|
86
69
|
self,
|
87
70
|
browser: Browser = Browser.CHROME,
|
88
|
-
baseurl: str = "",
|
89
|
-
timeout: int = 15,
|
90
71
|
headless: bool = False,
|
91
|
-
window_size: str = "720",
|
92
72
|
enable_log_performance: bool = False,
|
93
73
|
enable_log_console: bool = False,
|
94
74
|
enable_log_driver: bool = False,
|
@@ -99,11 +79,6 @@ class SetupSelenium:
|
|
99
79
|
browser_path: str | None = None,
|
100
80
|
) -> None:
|
101
81
|
log_path = os.path.abspath(os.path.expanduser(log_path))
|
102
|
-
self.main_window_handle: str = ""
|
103
|
-
self.screenshot_path: str = self.make_screenshot_path(log_path)
|
104
|
-
self.log_path: str = log_path
|
105
|
-
self.timeout: int = timeout
|
106
|
-
self.baseurl: str = baseurl
|
107
82
|
|
108
83
|
if driver_path:
|
109
84
|
driver_path = os.path.abspath(os.path.expanduser(driver_path))
|
@@ -131,31 +106,6 @@ class SetupSelenium:
|
|
131
106
|
driver_path=driver_path,
|
132
107
|
)
|
133
108
|
|
134
|
-
# driver must be setup before the following
|
135
|
-
self.driver.set_window_position(0, 0)
|
136
|
-
self.set_window_size(window_size)
|
137
|
-
self.set_main_window_handle()
|
138
|
-
|
139
|
-
############################################################################
|
140
|
-
@staticmethod
|
141
|
-
def make_screenshot_path(
|
142
|
-
output_dir: str = "./logs", screenshots: str = "screenshots"
|
143
|
-
) -> str:
|
144
|
-
"""Set the output directory for where screenshots should go."""
|
145
|
-
output_dir = os.path.abspath(os.path.expanduser(output_dir))
|
146
|
-
if os.path.split(output_dir)[-1].lower() != screenshots:
|
147
|
-
output_dir = os.path.join(output_dir, screenshots)
|
148
|
-
|
149
|
-
try:
|
150
|
-
os.makedirs(output_dir)
|
151
|
-
except OSError as e:
|
152
|
-
if e.errno == errno.EEXIST and os.path.isdir(output_dir):
|
153
|
-
pass
|
154
|
-
else:
|
155
|
-
raise
|
156
|
-
|
157
|
-
return output_dir
|
158
|
-
|
159
109
|
############################################################################
|
160
110
|
@staticmethod
|
161
111
|
def log_options(options: ArgOptions) -> None:
|
@@ -637,42 +587,3 @@ class SetupSelenium:
|
|
637
587
|
logger.info(bsrmsg)
|
638
588
|
SetupSelenium.log_options(options)
|
639
589
|
return driver
|
640
|
-
|
641
|
-
############################################################################
|
642
|
-
def set_window_size(self, size: str = "720") -> None:
|
643
|
-
"""Helper to set the window size after driver has been instantiated."""
|
644
|
-
if size == "max":
|
645
|
-
self.driver.maximize_window()
|
646
|
-
return
|
647
|
-
|
648
|
-
width, height = SetupSelenium.DIMENSIONS.get(
|
649
|
-
size, SetupSelenium.DIMENSIONS.get(size, (1280, 720))
|
650
|
-
)
|
651
|
-
self.driver.set_window_size(width, height)
|
652
|
-
|
653
|
-
def set_main_window_handle(self, window: str | None = None) -> str:
|
654
|
-
"""
|
655
|
-
Maintains the initial window handle as an attribute
|
656
|
-
|
657
|
-
Most users will never utilize this. It's part of a legacy requirement for
|
658
|
-
an old test suite
|
659
|
-
"""
|
660
|
-
# does the main_window_handle exist and point to an available window?
|
661
|
-
if not window and not self.main_window_handle:
|
662
|
-
try:
|
663
|
-
window = self.driver.current_window_handle
|
664
|
-
except NoSuchWindowException:
|
665
|
-
try:
|
666
|
-
window = self.driver.window_handles[0]
|
667
|
-
except WebDriverException: # noqa: TRY203
|
668
|
-
# Have we closed all the windows?
|
669
|
-
raise
|
670
|
-
if window:
|
671
|
-
self.main_window_handle = window
|
672
|
-
return self.main_window_handle
|
673
|
-
|
674
|
-
############################################################################
|
675
|
-
def __repr__(self) -> str:
|
676
|
-
browser = self.driver.name if self.driver is not None else "NoBrowserSet"
|
677
|
-
url = self.baseurl
|
678
|
-
return f"{self.__class__.__name__} :: {browser} -> {url}"
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: setup-selenium-testing
|
3
|
-
Version: 0.
|
3
|
+
Version: 1.0.0
|
4
4
|
Summary: Setup Selenium for automation testing
|
5
5
|
License: MIT
|
6
6
|
Author: Marcel Wilson
|
@@ -50,7 +50,42 @@ in every project. Time to consolidate.
|
|
50
50
|
[](https://github.com/bandophahita/setup_selenium/actions/workflows/lint.yml)
|
51
51
|
|
52
52
|
|
53
|
-
#
|
53
|
+
# Instantiating SetupSelenium
|
54
|
+
|
55
|
+
This will automatically handle any downloading of drivers or browsers via `SeleniumManager`
|
56
|
+
|
57
|
+
```python
|
58
|
+
from setup_selenium import SetupSelenium
|
59
|
+
|
60
|
+
s = SetupSelenium(headless=True)
|
61
|
+
assert s.driver.service.is_connectable()
|
62
|
+
```
|
63
|
+
|
64
|
+
Advanced usage:
|
65
|
+
|
66
|
+
```python
|
67
|
+
from setup_selenium import Browser, SetupSelenium
|
68
|
+
|
69
|
+
s = SetupSelenium(Browser.FIREFOX, headless=True, driver_version="118.0.5993.70")
|
70
|
+
s = SetupSelenium(Browser.CHROME, headless=True, driver_version="118.0.5993.70",
|
71
|
+
driver_path="/path/to/webdriver"
|
72
|
+
)
|
73
|
+
```
|
74
|
+
|
75
|
+
> [!NOTE]
|
76
|
+
> Version and path arguments follow the logic of
|
77
|
+
> [SeleniumManager](https://www.selenium.dev/documentation/selenium_manager/).
|
78
|
+
> Caution is advised in cases where version and path do not match.
|
79
|
+
> See their documentation.
|
80
|
+
|
81
|
+
# Install Driver only
|
82
|
+
```python
|
83
|
+
from setup_selenium import Browser, SetupSelenium
|
84
|
+
|
85
|
+
driver_path, browser_path = SetupSelenium.install_driver(Browser.CHROME, driver_version="118.0.5993.70")
|
86
|
+
```
|
87
|
+
|
88
|
+
# Create driver only
|
54
89
|
|
55
90
|
```python
|
56
91
|
from setup_selenium import Browser, SetupSelenium
|
@@ -58,7 +93,8 @@ from setup_selenium import Browser, SetupSelenium
|
|
58
93
|
driver = SetupSelenium.create_driver(browser=Browser.CHROME, headless=True)
|
59
94
|
```
|
60
95
|
|
61
|
-
|
96
|
+
Advanced usage:
|
97
|
+
|
62
98
|
```python
|
63
99
|
from setup_selenium import Browser, SetupSelenium
|
64
100
|
|
@@ -75,19 +111,11 @@ driver = SetupSelenium.create_driver(
|
|
75
111
|
```
|
76
112
|
|
77
113
|
> [!NOTE]
|
78
|
-
> It is possible to enable the performance and console logging
|
114
|
+
> It is possible to enable the performance and console logging
|
79
115
|
> but only for chrome based browsers. This only enables the browser ability.
|
80
116
|
> It is up to the tester to handle logging the messages.
|
81
117
|
|
82
118
|
|
83
|
-
|
84
|
-
# Driver installation
|
85
|
-
```python
|
86
|
-
from setup_selenium import Browser, SetupSelenium
|
87
|
-
|
88
|
-
driver_path, browser_path = SetupSelenium.install_driver(Browser.CHROME, "118.0.5993.70")
|
89
|
-
```
|
90
|
-
|
91
119
|
# Custom logger
|
92
120
|
```python
|
93
121
|
import logging
|
@@ -97,18 +125,6 @@ set_logger(logging.getLogger("your_custom_logger"))
|
|
97
125
|
driver = SetupSelenium.create_driver(browser=Browser.CHROME, headless=True)
|
98
126
|
```
|
99
127
|
|
100
|
-
# Instantiating SetupSelenium
|
101
|
-
While it is possible to use the class directly caution is advised; as the class
|
102
|
-
will create the driver upon instantiation.
|
103
|
-
|
104
|
-
```python
|
105
|
-
from setup_selenium import SetupSelenium
|
106
|
-
|
107
|
-
s = SetupSelenium(headless=True)
|
108
|
-
assert s.driver.service.is_connectable()
|
109
|
-
```
|
110
|
-
|
111
|
-
|
112
128
|
# Automatic driver and browser installation
|
113
129
|
This package not only handles setup of the webdriver but also will
|
114
130
|
automatically install the webdriver and/or browser depending on your
|
@@ -0,0 +1,7 @@
|
|
1
|
+
setup_selenium/__init__.py,sha256=-w9Vvo72ZLaEyIEfc_Nh0bDDJz9W0pOy4rkRXurIJHI,63
|
2
|
+
setup_selenium/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
setup_selenium/setup_selenium.py,sha256=6IijZ0maMo7t3_W1vUyn0l2SnEi8vsZoVZRUeQmu5kA,21834
|
4
|
+
setup_selenium_testing-1.0.0.dist-info/LICENSE,sha256=KGdE-1D1chm3UNFtfE8x-EpVxhmv2zFx8oltbO8M1qE,1070
|
5
|
+
setup_selenium_testing-1.0.0.dist-info/METADATA,sha256=N0t4a2Zf4wGPcc6lVTtdpFDlSDQ2QsgCUF1lhsP6Kjs,5847
|
6
|
+
setup_selenium_testing-1.0.0.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
7
|
+
setup_selenium_testing-1.0.0.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
setup_selenium/__init__.py,sha256=-w9Vvo72ZLaEyIEfc_Nh0bDDJz9W0pOy4rkRXurIJHI,63
|
2
|
-
setup_selenium/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
setup_selenium/setup_selenium.py,sha256=WyYo_VsqD_Z5wo54K_B8FfXRzNCyd6M9Nw1kqkFPH3k,25170
|
4
|
-
setup_selenium_testing-0.3.0.dist-info/LICENSE,sha256=KGdE-1D1chm3UNFtfE8x-EpVxhmv2zFx8oltbO8M1qE,1070
|
5
|
-
setup_selenium_testing-0.3.0.dist-info/METADATA,sha256=JQ7bnIg03oQ-7rez5TjR-Bg_9eN7ub66LD8PK6WZMxA,5305
|
6
|
-
setup_selenium_testing-0.3.0.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
7
|
-
setup_selenium_testing-0.3.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|