Skip to content

Driver options and more #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/behave_bdd/features/simple.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: SeleniumBase scenarios for the Simple App

Scenario: Verify Simple App (log in / sign out)
Given Open "seleniumbase.io/simple/login"
And Clear Session Storage
And Type "demo_user" into "#username"
And Type "secret_pass" into "#password"
And Click 'a:contains("Sign in")'
And Assert exact text "Welcome!" in "h1"
And Assert element "img#image1"
And Highlight "#image1"
And Click link "Sign out"
And Assert text "signed out" in "#top_message"
6 changes: 3 additions & 3 deletions examples/hack_the_planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ def test_all_your_base_are_belong_to_us(self):
self.open("https://wordpress.com/")
zoom_out = "h1.is-page-header{zoom: 0.8;-moz-transform: scale(0.8);}"
self.add_css_style(zoom_out)
zoom_in = "div.lp-is-cta-blue{zoom: 1.4;-moz-transform: scale(1.4);}"
zoom_in = "a.wp-element-button{zoom: 1.4;-moz-transform: scale(1.4);}"
self.add_css_style(zoom_in)
self.set_text_content("h1.is-page-header", aybabtu)
self.set_text_content("main div.lp-is-cta-blue", "Use SeleniumBase!")
self.set_text_content("a.wp-element-button", "Use SeleniumBase!")
self.highlight("h1.is-page-header", loops=6, scroll=False)
self.highlight("main div.lp-is-cta-blue", loops=4, scroll=False)
self.highlight("a.wp-element-button", loops=4, scroll=False)

self.open("https://seleniumbase.com/")
self.set_text_content("h1", aybabtu)
Expand Down
4 changes: 2 additions & 2 deletions examples/raw_browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

driver = Driver(browser="chrome", headless=False)
try:
driver.get("https://seleniumbase.io/apps/calculator")
driver.open("seleniumbase.io/apps/calculator")
driver.click('[id="4"]')
driver.click('[id="2"]')
driver.assert_text("42", "#output")
Expand All @@ -13,7 +13,7 @@

driver = Driver()
try:
driver.get("https://seleniumbase.github.io/demo_page")
driver.open("seleniumbase.github.io/demo_page")
driver.highlight("h2")
driver.type("#myTextInput", "Automation")
driver.click("#checkBox1")
Expand Down
6 changes: 3 additions & 3 deletions examples/raw_driver_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
from seleniumbase import DriverContext

with DriverContext() as driver:
driver.get("https://seleniumbase.github.io/")
driver.open("seleniumbase.github.io/")
driver.highlight('img[alt="SeleniumBase"]', loops=6)

with DriverContext(browser="chrome", incognito=True) as driver:
driver.get("https://seleniumbase.io/apps/calculator")
driver.open("seleniumbase.io/apps/calculator")
driver.click('[id="4"]')
driver.click('[id="2"]')
driver.assert_text("42", "#output")
driver.highlight("#output", loops=6)

with DriverContext() as driver:
driver.get("https://seleniumbase.github.io/demo_page")
driver.open("seleniumbase.github.io/demo_page")
driver.highlight("h2")
driver.type("#myTextInput", "Automation")
driver.click("#checkBox1")
Expand Down
12 changes: 12 additions & 0 deletions examples/raw_login_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from seleniumbase import DriverContext

with DriverContext() as driver:
driver.open("seleniumbase.io/simple/login")
driver.type("#username", "demo_user")
driver.type("#password", "secret_pass")
driver.click('a:contains("Sign in")')
driver.assert_exact_text("Welcome!", "h1")
driver.assert_element("img#image1")
driver.highlight("#image1")
driver.click_link("Sign out")
driver.assert_text("signed out", "#top_message")
15 changes: 15 additions & 0 deletions examples/raw_login_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from seleniumbase import Driver

driver = Driver()
try:
driver.open("seleniumbase.io/simple/login")
driver.type("#username", "demo_user")
driver.type("#password", "secret_pass")
driver.click('a:contains("Sign in")')
driver.assert_exact_text("Welcome!", "h1")
driver.assert_element("img#image1")
driver.highlight("#image1")
driver.click_link("Sign out")
driver.assert_text("signed out", "#top_message")
finally:
driver.quit()
12 changes: 12 additions & 0 deletions examples/raw_login_sb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from seleniumbase import SB

with SB() as sb:
sb.open("seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
sb.assert_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.highlight("#image1")
sb.click_link("Sign out")
sb.assert_text("signed out", "#top_message")
2 changes: 1 addition & 1 deletion examples/raw_uc_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
if sb.is_element_visible('iframe[src*="challenge"]'):
with sb.frame_switch('iframe[src*="challenge"]'):
sb.click("area")
sb.click("span.mark")
sb.sleep(4)
sb.activate_demo_mode()
sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)
25 changes: 25 additions & 0 deletions examples/sb_fixture_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# "sb" pytest fixture test in a method with no class
def test_sb_fixture_with_no_class(sb):
sb.open("seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
sb.assert_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.highlight("#image1")
sb.click_link("Sign out")
sb.assert_text("signed out", "#top_message")


# "sb" pytest fixture test in a method inside a class
class Test_SB_Fixture:
def test_sb_fixture_inside_class(self, sb):
sb.open("seleniumbase.io/simple/login")
sb.type("#username", "demo_user")
sb.type("#password", "secret_pass")
sb.click('a:contains("Sign in")')
sb.assert_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.highlight("#image1")
sb.click_link("Sign out")
sb.assert_text("signed out", "#top_message")
6 changes: 6 additions & 0 deletions examples/test_override_sb_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def sb(request):
from selenium import webdriver
from seleniumbase import BaseCase
from seleniumbase import config as sb_config
from seleniumbase.core import session_helper

class BaseClass(BaseCase):
def get_new_driver(self, *args, **kwargs):
Expand All @@ -31,6 +32,11 @@ def tearDown(self):
super().tearDown()

if request.cls:
if sb_config.reuse_class_session:
the_class = str(request.cls).split(".")[-1].split("'")[0]
if the_class != sb_config._sb_class:
session_helper.end_reused_class_session_as_needed()
sb_config._sb_class = the_class
request.cls.sb = BaseClass("base_method")
request.cls.sb.setUp()
request.cls.sb._needs_tearDown = True
Expand Down
4 changes: 2 additions & 2 deletions examples/test_sb_fixture.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# "sb" pytest fixture test in a method with no class
def test_sb_fixture_with_no_class(sb):
sb.open("https://seleniumbase.io/help_docs/install/")
sb.open("seleniumbase.io/help_docs/install/")
sb.type('input[aria-label="Search"]', "GUI Commander")
sb.click('mark:contains("Commander")')
sb.assert_title_contains("GUI / Commander")
Expand All @@ -9,7 +9,7 @@ def test_sb_fixture_with_no_class(sb):
# "sb" pytest fixture test in a method inside a class
class Test_SB_Fixture:
def test_sb_fixture_inside_class(self, sb):
sb.open("https://seleniumbase.io/help_docs/install/")
sb.open("seleniumbase.io/help_docs/install/")
sb.type('input[aria-label="Search"]', "GUI Commander")
sb.click('mark:contains("Commander")')
sb.assert_title_contains("GUI / Commander")
2 changes: 1 addition & 1 deletion examples/test_scrape_bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ScrapeBingTests(BaseCase):
def test_scrape_bing(self):
self.open(r"https://www.bing.com/search?q=SeleniumBase%20GitHub")
self.open("www.bing.com/search?q=SeleniumBase+GitHub&qs=n&form=QBRE")
self.wait_for_element("main h2 a")
soup = self.get_beautiful_soup()
titles = [item.text for item in soup.select("main h2 a")]
Expand Down
15 changes: 15 additions & 0 deletions examples/test_simple_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)


class TestSimpleLogin(BaseCase):
def test_simple_login(self):
self.open("seleniumbase.io/simple/login")
self.type("#username", "demo_user")
self.type("#password", "secret_pass")
self.click('a:contains("Sign in")')
self.assert_exact_text("Welcome!", "h1")
self.assert_element("img#image1")
self.highlight("#image1")
self.click_link("Sign out")
self.assert_text("signed out", "#top_message")
2 changes: 1 addition & 1 deletion examples/uc_cdp_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_display_cdp_events(self):
except Exception:
if self.is_element_visible('iframe[src*="challenge"]'):
with self.frame_switch('iframe[src*="challenge"]'):
self.click("area")
self.click("span.mark")
else:
self.fail_me()
try:
Expand Down
2 changes: 1 addition & 1 deletion examples/verify_undetected.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_browser_is_undetected(self):
except Exception:
if self.is_element_visible('iframe[src*="challenge"]'):
with self.frame_switch('iframe[src*="challenge"]'):
self.click("area")
self.click("span.mark")
else:
self.fail_me()
try:
Expand Down
8 changes: 8 additions & 0 deletions help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ self.send_keys(selector, text, by="css selector", timeout=None)
# Duplicates:
# self.add_text(selector, text, by="css selector", timeout=None)

self.press_keys(selector, text, by="css selector", timeout=None)

self.submit(selector, by="css selector")

self.clear(selector, by="css selector", timeout=None)
Expand Down Expand Up @@ -467,8 +469,12 @@ self.get_downloads_folder()

self.get_browser_downloads_folder()

self.get_downloaded_files(regex=None, browser=False)

self.get_path_of_downloaded_file(file, browser=False)

self.get_data_from_downloaded_file(file, timeout=None, browser=False)

self.is_downloaded_file_present(file, browser=False)

self.is_downloaded_file_regex_present(regex, browser=False)
Expand All @@ -480,6 +486,8 @@ self.assert_downloaded_file(file, timeout=None, browser=False)

self.assert_downloaded_file_regex(regex, timeout=None, browser=False)

self.assert_data_in_downloaded_file(data, file, timeout=None, browser=False)

self.assert_true(expr, msg=None)

self.assert_false(expr, msg=None)
Expand Down
16 changes: 11 additions & 5 deletions help_docs/syntax_formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def sb(request):
from selenium import webdriver
from seleniumbase import BaseCase
from seleniumbase import config as sb_config
from seleniumbase.core import session_helper

class BaseClass(BaseCase):
def get_new_driver(self, *args, **kwargs):
Expand All @@ -326,6 +327,11 @@ def sb(request):
super().tearDown()

if request.cls:
if sb_config.reuse_class_session:
the_class = str(request.cls).split(".")[-1].split("'")[0]
if the_class != sb_config._sb_class:
session_helper.end_reused_class_session_as_needed()
sb_config._sb_class = the_class
request.cls.sb = BaseClass("base_method")
request.cls.sb.setUp()
request.cls.sb._needs_tearDown = True
Expand Down Expand Up @@ -877,18 +883,18 @@ This pure Python format gives you a raw <code translate="no">webdriver</code> in
from seleniumbase import DriverContext

with DriverContext() as driver:
driver.get("https://seleniumbase.github.io/")
driver.open("seleniumbase.github.io/")
driver.highlight('img[alt="SeleniumBase"]', loops=6)

with DriverContext(browser="chrome", incognito=True) as driver:
driver.get("https://seleniumbase.io/apps/calculator")
driver.open("seleniumbase.io/apps/calculator")
driver.click('[id="4"]')
driver.click('[id="2"]')
driver.assert_text("42", "#output")
driver.highlight("#output", loops=6)

with DriverContext() as driver:
driver.get("https://seleniumbase.github.io/demo_page")
driver.open("seleniumbase.github.io/demo_page")
driver.highlight("h2")
driver.type("#myTextInput", "Automation")
driver.click("#checkBox1")
Expand All @@ -908,7 +914,7 @@ from seleniumbase import Driver

driver = Driver(browser="chrome", headless=False)
try:
driver.get("https://seleniumbase.io/apps/calculator")
driver.open("seleniumbase.io/apps/calculator")
driver.click('[id="4"]')
driver.click('[id="2"]')
driver.assert_text("42", "#output")
Expand All @@ -918,7 +924,7 @@ finally:

driver = Driver()
try:
driver.get("https://seleniumbase.github.io/demo_page")
driver.open("seleniumbase.github.io/demo_page")
driver.highlight("h2")
driver.type("#myTextInput", "Automation")
driver.click("#checkBox1")
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ paginate==0.5.6
pyquery==2.0.0
readtime==3.0.0
mkdocs==1.5.3
mkdocs-material==9.3.2
mkdocs-material==9.4.1
mkdocs-exclude-search==0.6.5
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.2
34 changes: 34 additions & 0 deletions sbase/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,40 @@ def jquery_type(context, text, selector):
sb.jquery_type(selector, text)


@step("Press keys '{text}' in '{selector}'")
@step('Press keys "{text}" in "{selector}"')
@step("Press keys '{text}' in \"{selector}\"")
@step('Press keys "{text}" in \'{selector}\'')
@step("Press keys '{text}' into '{selector}'")
@step('Press keys "{text}" into "{selector}"')
@step("Press keys '{text}' into \"{selector}\"")
@step('Press keys "{text}" into \'{selector}\'')
@step("In '{selector}' press keys '{text}'")
@step('In "{selector}" press keys "{text}"')
@step("In '{selector}' press keys \"{text}\"")
@step('In "{selector}" press keys \'{text}\'')
@step("Into '{selector}' press keys '{text}'")
@step('Into "{selector}" press keys "{text}"')
@step("Into '{selector}' press keys \"{text}\"")
@step('Into "{selector}" press keys \'{text}\'')
@step("Find '{selector}' and press keys '{text}'")
@step('Find "{selector}" and press keys "{text}"')
@step("Find '{selector}' and press keys \"{text}\"")
@step('Find "{selector}" and press keys \'{text}\'')
@step("User presses keys '{text}' in '{selector}'")
@step('User presses keys "{text}" in "{selector}"')
@step("User presses keys '{text}' in \"{selector}\"")
@step('User presses keys "{text}" in \'{selector}\'')
@step("User presses keys '{text}' into '{selector}'")
@step('User presses keys "{text}" into "{selector}"')
@step("User presses keys '{text}' into \"{selector}\"")
@step('User presses keys "{text}" into \'{selector}\'')
def press_keys(context, text, selector):
sb = context.sb
text = normalize_text(text)
sb.press_keys(selector, text)


@step("Find '{selector}' and set {attribute} to '{value}'")
@step('Find "{selector}" and set {attribute} to "{value}"')
@step("Find '{selector}' and set {attribute} to \"{value}\"")
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.18.8"
__version__ = "4.18.9"
18 changes: 18 additions & 0 deletions seleniumbase/behave/behave_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ def generate_gherkin(srt_actions):
sb_actions.append(
"jQuery type '%s' in '%s'" % (text, action[1])
)
elif action[0] == "pkeys":
text = action[2].replace("\n", "\\n")
if '"' not in text and '"' not in action[1]:
sb_actions.append(
'Press keys "%s" in "%s"' % (text, action[1])
)
elif '"' in text and '"' not in action[1]:
sb_actions.append(
'Press keys \'%s\' in "%s"' % (text, action[1])
)
elif '"' not in text and '"' in action[1]:
sb_actions.append(
'Press keys "%s" in \'%s\'' % (text, action[1])
)
elif '"' in text and '"' in action[1]:
sb_actions.append(
"Press keys '%s' in '%s'" % (text, action[1])
)
elif action[0] == "hover":
if '"' not in action[1]:
sb_actions.append('Hover "%s"' % action[1])
Expand Down
Loading