Skip to content

Commit 032be50

Browse files
committed
ENH: Implement selenium 4
1 parent 7c50ab2 commit 032be50

25 files changed

+399
-4
lines changed

botcity/web/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,14 +1363,14 @@ def mouse_move(self, x, y):
13631363
if self.browser == Browser.FIREFOX:
13641364
# Reset coordinates if the page has gone stale. Only required for Firefox
13651365
if self._html_elem is None:
1366-
self._html_elem = self._driver.find_element_by_tag_name('body')
1366+
self._html_elem = self._driver.find_element(By.TAG_NAME, 'body')
13671367
self._x = 0
13681368
self._y = 0
13691369
else:
13701370
try:
13711371
self._html_elem.is_enabled()
13721372
except StaleElementReferenceException:
1373-
self._html_elem = self._driver.find_element_by_tag_name('body')
1373+
self._html_elem = self._driver.find_element(By.NAME, 'body')
13741374
self._x = 0
13751375
self._y = 0
13761376

conftest.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import os
2+
import json
3+
import pytest
4+
import typing
5+
import platform
6+
7+
from botcity.web import WebBot, Browser, By, browsers
8+
9+
OS_NAME = platform.system()
10+
11+
PROJECT_DIR = os.path.abspath('')
12+
FAKE_BIN_PATH = os.path.join(PROJECT_DIR, 'fake.bin')
13+
TEST_PAGE = "https://lf2a.github.io/webpage-test/test.html"
14+
INDEX_PAGE = "https://lf2a.github.io/webpage-test/"
15+
16+
17+
def setup_chrome(headless: bool) -> WebBot:
18+
web = WebBot(headless)
19+
web.browser = Browser.CHROME
20+
21+
if OS_NAME == 'Windows':
22+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'chromedriver.exe')
23+
elif OS_NAME == 'Linux':
24+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'chromedriver')
25+
elif OS_NAME == 'Darwin':
26+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'chromedriver')
27+
else:
28+
raise ValueError(f'OS [{OS_NAME}] not supported.')
29+
return web
30+
31+
32+
def setup_firefox(headless: bool) -> WebBot:
33+
web = WebBot(headless)
34+
web.browser = Browser.FIREFOX
35+
36+
if OS_NAME == 'Windows':
37+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'geckodriver.exe')
38+
elif OS_NAME == 'Linux':
39+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'geckodriver')
40+
elif OS_NAME == 'Darwin':
41+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'geckodriver')
42+
else:
43+
raise ValueError(f'OS [{OS_NAME}] not supported.')
44+
return web
45+
46+
47+
def setup_edge(headless: bool) -> WebBot:
48+
web = WebBot(headless)
49+
web.browser = Browser.EDGE
50+
51+
opt = browsers.edge.default_options(headless=headless, download_folder_path=web.download_folder_path)
52+
opt.set_capability('platform', 'ANY') # WINDOWS is default value:
53+
54+
if OS_NAME == 'Windows':
55+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'msedgedriver.exe')
56+
elif OS_NAME == 'Linux':
57+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'msedgedriver')
58+
elif OS_NAME == 'Darwin':
59+
web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'msedgedriver')
60+
else:
61+
raise ValueError(f'OS [{OS_NAME}] not supported.')
62+
web.options = opt
63+
return web
64+
65+
66+
@pytest.fixture
67+
def web(request):
68+
69+
browser = request.config.getoption("--browser") or Browser.CHROME
70+
is_headless = request.config.getoption("--headless")
71+
72+
if browser == 'chrome':
73+
web = setup_chrome(is_headless)
74+
elif browser == 'firefox':
75+
web = setup_firefox(is_headless)
76+
elif browser == 'edge':
77+
web = setup_edge(is_headless)
78+
else:
79+
raise ValueError(f'Browser [{browser}] not supported.')
80+
yield web
81+
web.stop_browser()
82+
83+
84+
def get_event_result(id_event: str, web: WebBot) -> typing.Dict:
85+
event_result = web.find_element(id_event, By.ID)
86+
return json.loads(event_result.text)
87+
88+
89+
def pytest_addoption(parser):
90+
parser.addoption('--headless', action='store_const', const=True)
91+
parser.addoption('--browser', action='store', default='chrome')
92+

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
botcity-framework-base>=0.2.2
22
beautifulsoup4
3-
msedge-selenium-tools==3.141.3
3+
msedge-selenium-tools==3.141.4
44
numpy
55
opencv-python
66
pillow
7-
selenium==3.141
7+
selenium==4.6.0

resources/git.png

2.03 KB
Loading

resources/hello_world.png

4.93 KB
Loading

resources/mario3.png

4.65 KB
Loading

resources/mario4.png

3.65 KB
Loading

resources/mario5.png

3.39 KB
Loading

resources/mario_desktop.png

3.36 KB
Loading

resources/mouse.png

1.09 KB
Loading

0 commit comments

Comments
 (0)