Skip to content

Commit be65fb0

Browse files
authored
Merge pull request #273 from seleniumbase/refactoring-and-new-methods
Refactoring and new methods
2 parents 90975cf + 817ef51 commit be65fb0

File tree

6 files changed

+54
-74
lines changed

6 files changed

+54
-74
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ nosetests.xml
3636
.idea
3737
.project
3838
.pydevproject
39+
.vscode
3940

4041
# Web Drivers
4142
chromedriver

help_docs/method_summary.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ self.scroll_to(selector, by=By.CSS_SELECTOR)
140140

141141
self.slow_scroll_to(selector, by=By.CSS_SELECTOR)
142142

143-
self.scroll_click(selector, by=By.CSS_SELECTOR) # DEPRECATED
144-
145143
self.click_xpath(xpath)
146144

147145
self.js_click(selector, by=By.CSS_SELECTOR)
@@ -166,6 +164,8 @@ self.ad_block()
166164

167165
self.get_domain_url(url)
168166

167+
self.get_beautiful_soup(source=None)
168+
169169
self.safe_execute_script(script)
170170

171171
self.download_file(file_url, destination_folder=None)
@@ -182,6 +182,14 @@ self.is_downloaded_file_present(file)
182182

183183
self.assert_downloaded_file(file)
184184

185+
self.assert_true(expr, msg=None)
186+
187+
self.assert_false(expr, msg=None)
188+
189+
self.assert_equal(first, second, msg=None)
190+
191+
self.assert_not_equal(first, second, msg=None)
192+
185193
self.assert_no_js_errors()
186194

187195
self.get_google_auth_password(totp_key=None)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ beautifulsoup4>=4.6.0
1818
colorama==0.4.1
1919
pyotp>=2.2.7
2020
boto>=2.49.0
21-
flake8==3.7.3
21+
flake8==3.7.4
2222
PyVirtualDisplay==0.2.1
2323
-e .

seleniumbase/fixtures/base_case.py

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ def test_anything(self):
3030
import time
3131
import unittest
3232
import uuid
33-
from bs4 import BeautifulSoup
3433
from seleniumbase import config as sb_config
3534
from seleniumbase.common import decorators
3635
from seleniumbase.config import settings
37-
from seleniumbase.core.application_manager import ApplicationManager
38-
from seleniumbase.core.testcase_manager import ExecutionQueryPayload
3936
from seleniumbase.core.testcase_manager import TestcaseDataPayload
4037
from seleniumbase.core.testcase_manager import TestcaseManager
4138
from seleniumbase.core import download_helper
@@ -196,9 +193,7 @@ def is_link_text_present(self, link_text):
196193
""" Returns True if the link text appears in the HTML of the page.
197194
The element doesn't need to be visible,
198195
such as elements hidden inside a dropdown selection. """
199-
self.wait_for_ready_state_complete()
200-
source = self.get_page_source()
201-
soup = BeautifulSoup(source, "html.parser")
196+
soup = self.get_beautiful_soup()
202197
html_links = soup.find_all('a')
203198
for html_link in html_links:
204199
if html_link.text.strip() == link_text.strip():
@@ -209,9 +204,7 @@ def get_link_attribute(self, link_text, attribute, hard_fail=True):
209204
""" Finds a link by link text and then returns the attribute's value.
210205
If the link text or attribute cannot be found, an exception will
211206
get raised if hard_fail is True (otherwise None is returned). """
212-
self.wait_for_ready_state_complete()
213-
source = self.get_page_source()
214-
soup = BeautifulSoup(source, "html.parser")
207+
soup = self.get_beautiful_soup()
215208
html_links = soup.find_all('a')
216209
for html_link in html_links:
217210
if html_link.text.strip() == link_text.strip():
@@ -340,8 +333,7 @@ def click_partial_link_text(self, partial_link_text,
340333
element = self.wait_for_partial_link_text(partial_link_text)
341334
element.click()
342335
return
343-
source = self.get_page_source()
344-
soup = BeautifulSoup(source, "html.parser")
336+
soup = self.get_beautiful_soup()
345337
html_links = soup.fetch('a')
346338
for html_link in html_links:
347339
if partial_link_text in html_link.text:
@@ -701,8 +693,7 @@ def is_element_in_an_iframe(self, selector, by=By.CSS_SELECTOR):
701693
selector, by = self.__recalculate_selector(selector, by)
702694
if self.is_element_present(selector, by=by):
703695
return False
704-
source = self.get_page_source()
705-
soup = BeautifulSoup(source, "html.parser")
696+
soup = self.get_beautiful_soup()
706697
iframe_list = soup.select('iframe')
707698
for iframe in iframe_list:
708699
iframe_identifier = None
@@ -727,8 +718,7 @@ def switch_to_frame_of_element(self, selector, by=By.CSS_SELECTOR):
727718
selector, by = self.__recalculate_selector(selector, by)
728719
if self.is_element_present(selector, by=by):
729720
return None
730-
source = self.get_page_source()
731-
soup = BeautifulSoup(source, "html.parser")
721+
soup = self.get_beautiful_soup()
732722
iframe_list = soup.select('iframe')
733723
for iframe in iframe_list:
734724
iframe_identifier = None
@@ -1438,12 +1428,15 @@ def slow_scroll_to(self, selector, by=By.CSS_SELECTOR,
14381428
selector, by=by, timeout=timeout)
14391429
self.__slow_scroll_to_element(element)
14401430

1431+
@decorators.deprecated("Use self.click() - It now scrolls before clicking")
14411432
def scroll_click(self, selector, by=By.CSS_SELECTOR):
14421433
# DEPRECATED - self.click() now scrolls to the element before clicking
1443-
# self.scroll_to(selector, by=by)
1434+
# self.scroll_to(selector, by=by) # Redundant
14441435
self.click(selector, by=by)
14451436

14461437
def click_xpath(self, xpath):
1438+
# Technically self.click() will automatically detect an xpath selector,
1439+
# so self.click_xpath() is just a longer name for the same action.
14471440
self.click(xpath, by=By.XPATH)
14481441

14491442
def js_click(self, selector, by=By.CSS_SELECTOR):
@@ -1552,13 +1545,24 @@ def ad_block(self):
15521545
except Exception:
15531546
pass # Don't fail test if ad_blocking fails
15541547

1548+
@decorators.deprecated("Use re.escape() instead! It does what you want!")
15551549
def jq_format(self, code):
1556-
# DEPRECATED - Use re.escape() instead, which does the action you want.
1550+
# DEPRECATED - re.escape() already does that thing you want!
15571551
return js_utils._jq_format(code)
15581552

15591553
def get_domain_url(self, url):
15601554
return page_utils.get_domain_url(url)
15611555

1556+
def get_beautiful_soup(self, source=None):
1557+
""" BeautifulSoup is a toolkit for dissecting an HTML document
1558+
and extracting what you need. It's great for screen-scraping! """
1559+
from bs4 import BeautifulSoup
1560+
if not source:
1561+
self.wait_for_ready_state_complete()
1562+
source = self.get_page_source()
1563+
soup = BeautifulSoup(source, "html.parser")
1564+
return soup
1565+
15621566
def safe_execute_script(self, script):
15631567
""" When executing a script that contains a jQuery command,
15641568
it's important that the jQuery library has been loaded first.
@@ -1611,6 +1615,18 @@ def assert_downloaded_file(self, file):
16111615
""" Asserts that the file exists in the Downloads Folder. """
16121616
assert os.path.exists(self.get_path_of_downloaded_file(file))
16131617

1618+
def assert_true(self, expr, msg=None):
1619+
self.assertTrue(expr, msg=None)
1620+
1621+
def assert_false(self, expr, msg=None):
1622+
self.assertFalse(expr, msg=None)
1623+
1624+
def assert_equal(self, first, second, msg=None):
1625+
self.assertEqual(first, second, msg=None)
1626+
1627+
def assert_not_equal(self, first, second, msg=None):
1628+
self.assertNotEqual(first, second, msg=None)
1629+
16141630
def assert_no_js_errors(self):
16151631
""" Asserts that there are no JavaScript "SEVERE"-level page errors.
16161632
Works ONLY for Chrome (non-headless) and Chrome-based browsers.
@@ -1885,39 +1901,6 @@ def select_option_by_value(self, dropdown_selector, option,
18851901
dropdown_by=dropdown_by, option_by="value",
18861902
timeout=timeout)
18871903

1888-
@decorators.deprecated("Use self.select_option_by_text() instead!")
1889-
def pick_select_option_by_text(self, dropdown_selector, option,
1890-
dropdown_by=By.CSS_SELECTOR,
1891-
timeout=settings.SMALL_TIMEOUT):
1892-
""" Selects an HTML <select> option by option text. """
1893-
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1894-
timeout = self.__get_new_timeout(timeout)
1895-
self.__select_option(dropdown_selector, option,
1896-
dropdown_by=dropdown_by, option_by="text",
1897-
timeout=timeout)
1898-
1899-
@decorators.deprecated("Use self.select_option_by_index() instead!")
1900-
def pick_select_option_by_index(self, dropdown_selector, option,
1901-
dropdown_by=By.CSS_SELECTOR,
1902-
timeout=settings.SMALL_TIMEOUT):
1903-
""" Selects an HTML <select> option by option index. """
1904-
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1905-
timeout = self.__get_new_timeout(timeout)
1906-
self.__select_option(dropdown_selector, option,
1907-
dropdown_by=dropdown_by, option_by="index",
1908-
timeout=timeout)
1909-
1910-
@decorators.deprecated("Use self.select_option_by_value() instead!")
1911-
def pick_select_option_by_value(self, dropdown_selector, option,
1912-
dropdown_by=By.CSS_SELECTOR,
1913-
timeout=settings.SMALL_TIMEOUT):
1914-
""" Selects an HTML <select> option by option value. """
1915-
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1916-
timeout = self.__get_new_timeout(timeout)
1917-
self.__select_option(dropdown_selector, option,
1918-
dropdown_by=dropdown_by, option_by="value",
1919-
timeout=timeout)
1920-
19211904
############
19221905

19231906
def generate_referral(self, start_page, destination_page):
@@ -2495,12 +2478,6 @@ def delayed_assert_element(self, selector, by=By.CSS_SELECTOR,
24952478
self.__add_delayed_assert_failure()
24962479
return False
24972480

2498-
@decorators.deprecated("Use self.delayed_assert_element() instead!")
2499-
def check_assert_element(self, selector, by=By.CSS_SELECTOR,
2500-
timeout=settings.MINI_TIMEOUT):
2501-
""" DEPRECATED - Use self.delayed_assert_element() instead. """
2502-
return self.delayed_assert_element(selector, by=by, timeout=timeout)
2503-
25042481
def delayed_assert_text(self, text, selector="html", by=By.CSS_SELECTOR,
25052482
timeout=settings.MINI_TIMEOUT):
25062483
""" A non-terminating assertion for text from an element on a page.
@@ -2522,12 +2499,6 @@ def delayed_assert_text(self, text, selector="html", by=By.CSS_SELECTOR,
25222499
self.__add_delayed_assert_failure()
25232500
return False
25242501

2525-
@decorators.deprecated("Use self.delayed_assert_text() instead!")
2526-
def check_assert_text(self, text, selector="html", by=By.CSS_SELECTOR,
2527-
timeout=settings.MINI_TIMEOUT):
2528-
""" DEPRECATED - Use self.delayed_assert_text() instead. """
2529-
return self.delayed_assert_text(text, selector, by=by, timeout=timeout)
2530-
25312502
def process_delayed_asserts(self, print_only=False):
25322503
""" To be used with any test that uses delayed_asserts, which are
25332504
non-terminating verifications that only raise exceptions
@@ -2552,11 +2523,6 @@ def process_delayed_asserts(self, print_only=False):
25522523
else:
25532524
raise Exception(exception_output)
25542525

2555-
@decorators.deprecated("Use self.process_delayed_asserts() instead!")
2556-
def process_checks(self, print_only=False):
2557-
""" DEPRECATED - Use self.process_delayed_asserts() instead. """
2558-
self.process_delayed_asserts(print_only=print_only)
2559-
25602526
############
25612527

25622528
def __js_click(self, selector, by=By.CSS_SELECTOR):
@@ -2606,8 +2572,7 @@ def __get_href_from_link_text(self, link_text, hard_fail=True):
26062572

26072573
def __click_dropdown_link_text(self, link_text, link_css):
26082574
""" When a link may be hidden under a dropdown menu, use this. """
2609-
source = self.get_page_source()
2610-
soup = BeautifulSoup(source, "html.parser")
2575+
soup = self.get_beautiful_soup()
26112576
drop_down_list = soup.select('[class*=dropdown]')
26122577
for item in soup.select('[class*=HeaderMenu]'):
26132578
drop_down_list.append(item)
@@ -2769,6 +2734,10 @@ def setUp(self):
27692734
# Use Selenium Grid (Use --server=127.0.0.1 for localhost Grid)
27702735
self.use_grid = True
27712736
if self.with_db_reporting:
2737+
from seleniumbase.core.application_manager import (
2738+
ApplicationManager)
2739+
from seleniumbase.core.testcase_manager import (
2740+
ExecutionQueryPayload)
27722741
import getpass
27732742
self.execution_guid = str(uuid.uuid4())
27742743
self.testcase_guid = None

seleniumbase/fixtures/js_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
import time
88
from selenium.common.exceptions import WebDriverException
9+
from seleniumbase.common import decorators
910
from seleniumbase.config import settings
1011
from seleniumbase.fixtures import constants
1112

@@ -628,6 +629,7 @@ def slow_scroll_to_element(driver, element, browser):
628629
time.sleep(0.162)
629630

630631

632+
@decorators.deprecated("Use re.escape() instead, which does what you want!")
631633
def _jq_format(code):
632634
"""
633635
DEPRECATED - Use re.escape() instead, which performs the intended action.

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.19.0',
20+
version='1.19.1',
2121
description='Easy, Fast, Reliable Browser Automation & Testing Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',
@@ -71,7 +71,7 @@
7171
'colorama==0.4.1',
7272
'pyotp>=2.2.7',
7373
'boto>=2.49.0',
74-
'flake8==3.7.3',
74+
'flake8==3.7.4',
7575
'PyVirtualDisplay==0.2.1',
7676
],
7777
packages=[

0 commit comments

Comments
 (0)