Skip to content

Commit edeb2db

Browse files
authored
Merge pull request #2533 from seleniumbase/optimizations-and-test-updates
Optimizations and test updates
2 parents b2ec928 + 18dd9f1 commit edeb2db

17 files changed

+128
-113
lines changed

examples/presenter/multi_uc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
@pytest.mark.parametrize("", [[]] * 3)
99
def test_multi_threaded(sb):
10-
sb.driver.uc_open_with_tab("https://nowsecure.nl/#relax")
10+
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
1111
sb.set_window_rect(randint(0, 755), randint(38, 403), 700, 500)
1212
try:
13-
sb.assert_text("OH YEAH, you passed!", "h1", timeout=4)
13+
sb.assert_text("Discord Bots", "h1", timeout=2)
1414
sb.post_message("Selenium wasn't detected!", duration=4)
1515
sb._print("\n Success! Website did not detect Selenium! ")
1616
except Exception:
17-
sb.driver.uc_open_with_tab("https://nowsecure.nl/#relax")
17+
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
1818
try:
19-
sb.assert_text("OH YEAH, you passed!", "h1", timeout=4)
19+
sb.assert_text("Discord Bots", "h1", timeout=2)
2020
sb.post_message("Selenium wasn't detected!", duration=4)
2121
sb._print("\n Success! Website did not detect Selenium! ")
2222
except Exception:

examples/presenter/uc_presentation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ def test_presentation(self):
3030
self.get_new_driver(undetectable=True)
3131
try:
3232
self.driver.uc_open_with_reconnect(
33-
"https://nowsecure.nl/#relax", reconnect_time=3
33+
"https://top.gg/", reconnect_time=4
3434
)
3535
try:
36-
self.assert_text("OH YEAH, you passed!", "h1", timeout=4)
36+
self.assert_text("Discord Bots", "h1", timeout=3)
3737
self.post_message("Selenium wasn't detected!", duration=4)
3838
except Exception:
39-
self.clear_all_cookies()
4039
self.driver.uc_open_with_reconnect(
41-
"https://nowsecure.nl/#relax", reconnect_time=3
40+
"https://top.gg/", reconnect_time=5
4241
)
43-
self.assert_text("OH YEAH, you passed!", "h1", timeout=4)
42+
self.assert_text("Discord Bots", "h1", timeout=2)
4443
self.post_message("Selenium wasn't detected!", duration=4)
4544
finally:
4645
self.quit_extra_driver()

examples/raw_cdp_logging.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from rich.pretty import pprint
2+
from seleniumbase import Driver
3+
4+
driver = Driver(uc=True, log_cdp=True)
5+
try:
6+
driver.get("https://seleniumbase.io/apps/invisible_recaptcha")
7+
driver.sleep(3)
8+
pprint(driver.get_log("performance"))
9+
finally:
10+
driver.quit()

examples/raw_driver_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""Can run with "python". (pytest not needed)."""
1+
"""DriverContext() example. (Runs with "python")."""
22
from seleniumbase import DriverContext
33

44
with DriverContext() as driver:
5-
driver.open("seleniumbase.github.io/")
5+
driver.open("seleniumbase.io/")
66
driver.highlight('img[alt="SeleniumBase"]', loops=6)
77

88
with DriverContext(browser="chrome", incognito=True) as driver:
@@ -13,7 +13,7 @@
1313
driver.highlight("#output", loops=6)
1414

1515
with DriverContext() as driver:
16-
driver.open("seleniumbase.github.io/demo_page")
16+
driver.open("seleniumbase.io/demo_page")
1717
driver.highlight("h2")
1818
driver.type("#myTextInput", "Automation")
1919
driver.click("#checkBox1")
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
"""Driver() test. Runs with "python". (pytest not needed)."""
1+
"""Driver() manager example. (Runs with "python")."""
22
from seleniumbase import Driver
33

4+
driver = Driver()
5+
try:
6+
driver.open("seleniumbase.io/demo_page")
7+
driver.highlight("h2")
8+
driver.type("#myTextInput", "Automation")
9+
driver.click("#checkBox1")
10+
driver.highlight("img", loops=6)
11+
finally:
12+
driver.quit()
13+
414
driver = Driver(browser="chrome", headless=False)
515
try:
616
driver.open("seleniumbase.io/apps/calculator")
@@ -10,13 +20,3 @@
1020
driver.highlight("#output", loops=6)
1121
finally:
1222
driver.quit()
13-
14-
driver = Driver()
15-
try:
16-
driver.open("seleniumbase.github.io/demo_page")
17-
driver.highlight("h2")
18-
driver.type("#myTextInput", "Automation")
19-
driver.click("#checkBox1")
20-
driver.highlight("img", loops=6)
21-
finally:
22-
driver.quit()

examples/raw_sb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Context Manager Test. Runs with "python". (pytest not needed)."""
1+
"""SB() context manager example. (Runs with "python")."""
22
from seleniumbase import SB
33

44
with SB() as sb: # By default, browser="chrome" if not set.

examples/raw_uc_mode.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22
from seleniumbase import SB
33

44
with SB(uc=True, test=True) as sb:
5-
sb.driver.uc_open_with_tab("https://nowsecure.nl/#relax")
6-
sb.sleep(1.2)
7-
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
5+
sb.driver.uc_open_with_reconnect("https://top.gg/", 4)
6+
if not sb.is_text_visible("Discord Bots", "h1"):
87
sb.get_new_driver(undetectable=True)
9-
sb.driver.uc_open_with_reconnect(
10-
"https://nowsecure.nl/#relax", reconnect_time=3
11-
)
12-
sb.sleep(1.2)
13-
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
14-
if sb.is_element_visible('iframe[src*="challenge"]'):
15-
with sb.frame_switch('iframe[src*="challenge"]'):
16-
sb.click("span.mark")
17-
sb.sleep(2)
18-
sb.activate_demo_mode()
19-
sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)
8+
sb.driver.uc_open_with_reconnect("https://top.gg/", 5)
9+
sb.activate_demo_mode() # Highlight + show assertions
10+
sb.assert_text("Discord Bots", "h1", timeout=3)

examples/uc_cdp_events.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pprint import pformat
1+
from rich.pretty import pprint
22
from seleniumbase import BaseCase
33
BaseCase.main(__name__, __file__, "--uc", "--uc-cdp", "-s")
44

@@ -9,38 +9,18 @@ def add_cdp_listener(self):
99
# self.driver.add_cdp_listener("*", lambda data: print(pformat(data)))
1010
self.driver.add_cdp_listener(
1111
"Network.requestWillBeSentExtraInfo",
12-
lambda data: print(pformat(data))
12+
lambda data: pprint(data)
1313
)
1414

1515
def verify_success(self):
1616
self.assert_text("OH YEAH, you passed!", "h1", timeout=6.25)
1717
self.sleep(1)
1818

19-
def fail_me(self):
20-
self.fail('Selenium was detected! Try using: "pytest --uc"')
21-
2219
def test_display_cdp_events(self):
2320
if not (self.undetectable and self.uc_cdp_events):
2421
self.get_new_driver(undetectable=True, uc_cdp_events=True)
25-
self.driver.get("https://nowsecure.nl/#relax")
26-
try:
27-
self.verify_success()
28-
except Exception:
29-
self.clear_all_cookies()
30-
self.get_new_driver(undetectable=True, uc_cdp_events=True)
31-
self.driver.get("https://nowsecure.nl/#relax")
32-
try:
33-
self.verify_success()
34-
except Exception:
35-
if self.is_element_visible('iframe[src*="challenge"]'):
36-
with self.frame_switch('iframe[src*="challenge"]'):
37-
self.click("span.mark")
38-
else:
39-
self.fail_me()
40-
try:
41-
self.verify_success()
42-
except Exception:
43-
self.fail_me()
22+
self.driver.uc_open_with_tab("https://nowsecure.nl/#relax")
23+
self.verify_success()
4424
self.add_cdp_listener()
4525
self.refresh()
4626
self.sleep(1)

examples/verify_undetected.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ def test_browser_is_undetected(self):
1010
if not self.undetectable:
1111
self.get_new_driver(undetectable=True)
1212
self.driver.uc_open_with_reconnect(
13-
"https://nowsecure.nl/#relax", reconnect_time=3
13+
"https://top.gg/", reconnect_time=4
1414
)
15-
self.sleep(1.2)
16-
if not self.is_text_visible("OH YEAH, you passed!", "h1"):
15+
if not self.is_text_visible("Discord Bots", "h1"):
1716
self.get_new_driver(undetectable=True)
1817
self.driver.uc_open_with_reconnect(
19-
"https://nowsecure.nl/#relax", reconnect_time=3
18+
"https://top.gg/", reconnect_time=5
2019
)
21-
self.sleep(1.2)
22-
if not self.is_text_visible("OH YEAH, you passed!", "h1"):
23-
if self.is_element_visible('iframe[src*="challenge"]'):
24-
with self.frame_switch('iframe[src*="challenge"]'):
25-
self.click("span.mark")
26-
self.sleep(2)
27-
self.assert_text("OH YEAH, you passed!", "h1", timeout=3)
20+
self.assert_text("Discord Bots", "h1", timeout=3)
21+
self.set_messenger_theme(theme="air", location="top_center")
2822
self.post_message("Selenium wasn't detected!", duration=2.8)
2923
self._print("\n Success! Website did not detect Selenium! ")

help_docs/syntax_formats.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -832,17 +832,16 @@ This format provides a pure Python way of using SeleniumBase without a test runn
832832
```python
833833
from seleniumbase import SB
834834

835-
with SB() as sb: # By default, browser="chrome" if not set.
836-
sb.open("https://seleniumbase.github.io/realworld/login")
835+
with SB() as sb:
836+
sb.open("seleniumbase.io/simple/login")
837837
sb.type("#username", "demo_user")
838838
sb.type("#password", "secret_pass")
839-
sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
840-
sb.assert_text("Welcome!", "h1")
841-
sb.highlight("img#image1") # A fancier assert_element() call
842-
sb.click('a:contains("This Page")') # Use :contains() on any tag
843-
sb.click_link("Sign out") # Link must be "a" tag. Not "button".
844-
sb.assert_element('a:contains("Sign in")')
845-
sb.assert_exact_text("You have been signed out!", "#top_message")
839+
sb.click('a:contains("Sign in")')
840+
sb.assert_exact_text("Welcome!", "h1")
841+
sb.assert_element("img#image1")
842+
sb.highlight("#image1")
843+
sb.click_link("Sign out")
844+
sb.assert_text("signed out", "#top_message")
846845
```
847846

848847
(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_sb.py">examples/raw_sb.py</a> for the test.)
@@ -881,11 +880,11 @@ with SB(test=True, rtf=True, demo=True) as sb:
881880
This pure Python format gives you a raw <code translate="no">webdriver</code> instance in a <code translate="no">with</code> block. The SeleniumBase Driver Manager will automatically make sure that your driver is compatible with your browser version. It gives you full access to customize driver options via method args or via the command-line. The driver will automatically call <code translate="no">quit()</code> after the code leaves the <code translate="no">with</code> block. Here are some examples:
882881

883882
```python
884-
"""Can run with "python". (pytest not needed)."""
883+
"""DriverContext() example. (Runs with "python")."""
885884
from seleniumbase import DriverContext
886885

887886
with DriverContext() as driver:
888-
driver.open("seleniumbase.github.io/")
887+
driver.open("seleniumbase.io/")
889888
driver.highlight('img[alt="SeleniumBase"]', loops=6)
890889

891890
with DriverContext(browser="chrome", incognito=True) as driver:
@@ -896,7 +895,7 @@ with DriverContext(browser="chrome", incognito=True) as driver:
896895
driver.highlight("#output", loops=6)
897896

898897
with DriverContext() as driver:
899-
driver.open("seleniumbase.github.io/demo_page")
898+
driver.open("seleniumbase.io/demo_page")
900899
driver.highlight("h2")
901900
driver.type("#myTextInput", "Automation")
902901
driver.click("#checkBox1")
@@ -911,9 +910,19 @@ with DriverContext() as driver:
911910
Another way of running Selenium tests with pure ``python`` (as opposed to using ``pytest`` or ``pynose``) is by using this format, which bypasses [BaseCase](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/fixtures/base_case.py) methods while still giving you a flexible driver with a manager. SeleniumBase includes helper files such as [page_actions.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/fixtures/page_actions.py), which may help you get around some of the limitations of bypassing ``BaseCase``. Here's an example:
912911

913912
```python
914-
"""Driver() test. Runs with "python". (pytest not needed)."""
913+
"""Driver() example. (Runs with "python")."""
915914
from seleniumbase import Driver
916915

916+
driver = Driver()
917+
try:
918+
driver.open("seleniumbase.io/demo_page")
919+
driver.highlight("h2")
920+
driver.type("#myTextInput", "Automation")
921+
driver.click("#checkBox1")
922+
driver.highlight("img", loops=6)
923+
finally:
924+
driver.quit()
925+
917926
driver = Driver(browser="chrome", headless=False)
918927
try:
919928
driver.open("seleniumbase.io/apps/calculator")
@@ -923,19 +932,9 @@ try:
923932
driver.highlight("#output", loops=6)
924933
finally:
925934
driver.quit()
926-
927-
driver = Driver()
928-
try:
929-
driver.open("seleniumbase.github.io/demo_page")
930-
driver.highlight("h2")
931-
driver.type("#myTextInput", "Automation")
932-
driver.click("#checkBox1")
933-
driver.highlight("img", loops=6)
934-
finally:
935-
driver.quit()
936935
```
937936

938-
(From <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_browser_launcher.py">examples/raw_browser_launcher.py</a>)
937+
(From <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_driver_manager.py">examples/raw_driver_manager.py</a>)
939938

940939
Here's how the [selenium-wire](https://github.com/wkeeling/selenium-wire) integration may look when using the ``Driver()`` format:
941940

@@ -951,6 +950,22 @@ finally:
951950
driver.quit()
952951
```
953952

953+
Here's another `selenium-wire` example with the `Driver()` format:
954+
955+
```python
956+
from seleniumbase import Driver
957+
958+
def intercept_response(request, response):
959+
print(request.headers)
960+
961+
driver = Driver(wire=True)
962+
try:
963+
driver.response_interceptor = intercept_response
964+
driver.get("https://wikipedia.org")
965+
finally:
966+
driver.quit()
967+
```
968+
954969
Here's an example of basic login with the ``Driver()`` format:
955970

956971
```python

0 commit comments

Comments
 (0)