|
2 | 2 |
|
3 | 3 | <a id="syntax_formats"></a>
|
4 | 4 |
|
5 |
| -<h2><img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="40"> The 23 Syntax Formats / Design Patterns</h2> |
| 5 | +<h2><img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="40"> The 25 Syntax Formats / Design Patterns</h2> |
6 | 6 |
|
7 | 7 | <h3>🔠 SeleniumBase supports multiple ways of structuring tests:</h3>
|
8 | 8 |
|
|
32 | 32 | <li><a href="#sb_sf_21"><strong>21. SeleniumBase SB (Python context manager)</strong></a></li>
|
33 | 33 | <li><a href="#sb_sf_22"><strong>22. The driver manager (via context manager)</strong></a></li>
|
34 | 34 | <li><a href="#sb_sf_23"><strong>23. The driver manager (via direct import)</strong></a></li>
|
| 35 | +<li><a href="#sb_sf_24"><strong>24. CDP driver (async/await API. No Selenium)</strong></a></li> |
| 36 | +<li><a href="#sb_sf_25"><strong>25. CDP driver (SB-CDP sync API. No Selenium)</strong></a></li> |
35 | 37 | </ul>
|
36 | 38 | </blockquote>
|
37 | 39 |
|
@@ -550,12 +552,12 @@ class MiaClasseDiTest(CasoDiProva):
|
550 | 552 | self.apri("https://it.wikipedia.org/wiki/")
|
551 | 553 | self.verificare_testo("Wikipedia")
|
552 | 554 | self.verificare_elemento('a[title="Lingua italiana"]')
|
553 |
| - self.digitare("#searchInput", "Pizza") |
554 |
| - self.fare_clic("#searchButton") |
| 555 | + self.digitare('input[name="search"]', "Pizza") |
| 556 | + self.fare_clic("#searchform button") |
555 | 557 | self.verificare_testo("Pizza", "#firstHeading")
|
556 | 558 | self.verificare_elemento('figure img[src*="pizza"]')
|
557 |
| - self.digitare("#searchInput", "Colosseo") |
558 |
| - self.fare_clic("#searchButton") |
| 559 | + self.digitare('input[name="search"]', "Colosseo") |
| 560 | + self.fare_clic("#searchform button") |
559 | 561 | self.verificare_testo("Colosseo", "#firstHeading")
|
560 | 562 | self.verificare_elemento('figure img[src*="Colosseo"]')
|
561 | 563 | self.indietro()
|
@@ -876,6 +878,21 @@ with SB(test=True, rtf=True, demo=True) as sb:
|
876 | 878 |
|
877 | 879 | (See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_test_scripts.py">examples/raw_test_scripts.py</a> for the test.)
|
878 | 880 |
|
| 881 | +Here's another example, which uses [CDP Mode](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/ReadMe.md) from the SeleniumBase SB format: |
| 882 | + |
| 883 | +```python |
| 884 | +from seleniumbase import SB |
| 885 | + |
| 886 | +with SB(uc=True, test=True) as sb: |
| 887 | + url = "www.planetminecraft.com/account/sign_in/" |
| 888 | + sb.activate_cdp_mode(url) |
| 889 | + sb.sleep(2) |
| 890 | + sb.cdp.gui_click_element("#turnstile-widget div") |
| 891 | + sb.sleep(2) |
| 892 | +``` |
| 893 | + |
| 894 | +(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_planetmc.py">examples/cdp_mode/raw_planetmc.py</a> for the test.) |
| 895 | + |
879 | 896 | <a id="sb_sf_22"></a>
|
880 | 897 | <h2><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 22. The driver manager (via context manager)</h2>
|
881 | 898 |
|
@@ -994,6 +1011,87 @@ The ``Driver()`` manager format can be used as a drop-in replacement for virtual
|
994 | 1011 |
|
995 | 1012 | When using the ``Driver()`` format, you may need to activate a Virtual Display on your own if you want to run headed tests in a headless Linux environment. (See https://github.com/mdmintz/sbVirtualDisplay for details.) One such example of this is using an authenticated proxy, which is configured via a Chrome extension that is generated at runtime. (Note that regular headless mode in Chrome doesn't support extensions.)
|
996 | 1013 |
|
| 1014 | +<a id="sb_sf_24"></a> |
| 1015 | +<h2><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 24. CDP driver (async/await API. No Selenium)</h2> |
| 1016 | + |
| 1017 | +This format provides a pure CDP way of using SeleniumBase (without Selenium or a test runner). The async/await API is used. Here's an example: |
| 1018 | + |
| 1019 | +```python |
| 1020 | +import asyncio |
| 1021 | +import time |
| 1022 | +from seleniumbase.undetected import cdp_driver |
| 1023 | + |
| 1024 | + |
| 1025 | +async def main(): |
| 1026 | + driver = await cdp_driver.cdp_util.start_async() |
| 1027 | + page = await driver.get("about:blank") |
| 1028 | + await page.set_locale("en") |
| 1029 | + await page.get("https://www.priceline.com/") |
| 1030 | + time.sleep(3) |
| 1031 | + print(await page.evaluate("document.title")) |
| 1032 | + element = await page.select('[data-testid*="endLocation"]') |
| 1033 | + await element.click_async() |
| 1034 | + time.sleep(1) |
| 1035 | + await element.send_keys_async("Boston") |
| 1036 | + time.sleep(2) |
| 1037 | + |
| 1038 | +if __name__ == "__main__": |
| 1039 | + loop = asyncio.new_event_loop() |
| 1040 | + loop.run_until_complete(main()) |
| 1041 | +``` |
| 1042 | + |
| 1043 | +(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_async.py">examples/cdp_mode/raw_async.py</a> for the test.) |
| 1044 | + |
| 1045 | +<a id="sb_sf_25"></a> |
| 1046 | +<h2><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 25. CDP driver (SB-CDP sync API. No Selenium)</h2> |
| 1047 | + |
| 1048 | +This format provides a pure CDP way of using SeleniumBase (without Selenium or a test runner). The expanded SB-CDP sync API is used. Here's an example: |
| 1049 | + |
| 1050 | +```python |
| 1051 | +import asyncio |
| 1052 | +from seleniumbase.core import sb_cdp |
| 1053 | +from seleniumbase.undetected import cdp_driver |
| 1054 | + |
| 1055 | + |
| 1056 | +def main(): |
| 1057 | + url0 = "about:blank" # Set Locale code from here first |
| 1058 | + url1 = "https://www.priceline.com/" # (The "real" URL) |
| 1059 | + loop = asyncio.new_event_loop() |
| 1060 | + driver = cdp_driver.cdp_util.start_sync() |
| 1061 | + page = loop.run_until_complete(driver.get(url0)) |
| 1062 | + sb = sb_cdp.CDPMethods(loop, page, driver) |
| 1063 | + sb.set_locale("en") # This test expects English locale |
| 1064 | + sb.open(url1) |
| 1065 | + sb.sleep(2.5) |
| 1066 | + sb.internalize_links() # Don't open links in a new tab |
| 1067 | + sb.click("#link_header_nav_experiences") |
| 1068 | + sb.sleep(3.5) |
| 1069 | + sb.remove_elements("msm-cookie-banner") |
| 1070 | + sb.sleep(1.5) |
| 1071 | + location = "Amsterdam" |
| 1072 | + where_to = 'div[data-automation*="experiences"] input' |
| 1073 | + button = 'button[data-automation*="experiences-search"]' |
| 1074 | + sb.gui_click_element(where_to) |
| 1075 | + sb.press_keys(where_to, location) |
| 1076 | + sb.sleep(1) |
| 1077 | + sb.gui_click_element(button) |
| 1078 | + sb.sleep(3) |
| 1079 | + print(sb.get_title()) |
| 1080 | + print("************") |
| 1081 | + for i in range(8): |
| 1082 | + sb.scroll_down(50) |
| 1083 | + sb.sleep(0.2) |
| 1084 | + cards = sb.select_all('h2[data-automation*="product-list-card"]') |
| 1085 | + for card in cards: |
| 1086 | + print("* %s" % card.text) |
| 1087 | + |
| 1088 | + |
| 1089 | +if __name__ == "__main__": |
| 1090 | + main() |
| 1091 | +``` |
| 1092 | + |
| 1093 | +(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_cdp.py">examples/cdp_mode/raw_cdp.py</a> for the test.) |
| 1094 | + |
997 | 1095 | --------
|
998 | 1096 |
|
999 | 1097 | <h3 align="left"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.github.io/img/sb_logo_10.png" title="SeleniumBase" width="280" /></a></h3>
|
0 commit comments