Refactoring and other updates

This commit is contained in:
Michael Mintz 2022-05-13 17:54:51 -04:00
parent 3a9b5f3f64
commit 69ae9f395b
6 changed files with 68 additions and 24 deletions

View File

@ -2004,6 +2004,7 @@ class BaseCase(unittest.TestCase):
)
hover_selector = self.convert_to_css_selector(hover_selector, hover_by)
hover_by = By.CSS_SELECTOR
original_click_selector = click_selector
click_selector, click_by = self.__recalculate_selector(
click_selector, click_by
)
@ -2019,6 +2020,10 @@ class BaseCase(unittest.TestCase):
if url and len(url) > 0:
if ("http:") in url or ("https:") in url or ("file:") in url:
if self.get_session_storage_item("pause_recorder") == "no":
if hover_by == By.XPATH:
hover_selector = original_selector
if click_by == By.XPATH:
click_selector = original_click_selector
time_stamp = self.execute_script("return Date.now();")
origin = self.get_origin()
the_selectors = [hover_selector, click_selector]
@ -9506,6 +9511,7 @@ class BaseCase(unittest.TestCase):
self.__assert_shadow_element_visible(selector)
return True
self.wait_for_element_visible(selector, by=by, timeout=timeout)
original_selector = selector
if self.demo_mode:
selector, by = self.__recalculate_selector(
selector, by, xp_ok=False
@ -9522,6 +9528,8 @@ class BaseCase(unittest.TestCase):
if url and len(url) > 0:
if ("http:") in url or ("https:") in url or ("file:") in url:
if self.get_session_storage_item("pause_recorder") == "no":
if by == By.XPATH:
selector = original_selector
time_stamp = self.execute_script("return Date.now();")
origin = self.get_origin()
action = ["as_el", selector, origin, time_stamp]
@ -9689,6 +9697,7 @@ class BaseCase(unittest.TestCase):
timeout = settings.SMALL_TIMEOUT
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
timeout = self.__get_new_timeout(timeout)
original_selector = selector
selector, by = self.__recalculate_selector(selector, by)
if self.__is_shadow_selector(selector):
self.__assert_shadow_text_visible(text, selector, timeout)
@ -9715,6 +9724,8 @@ class BaseCase(unittest.TestCase):
if url and len(url) > 0:
if ("http:") in url or ("https:") in url or ("file:") in url:
if self.get_session_storage_item("pause_recorder") == "no":
if by == By.XPATH:
selector = original_selector
time_stamp = self.execute_script("return Date.now();")
origin = self.get_origin()
text_selector = [text, selector]
@ -9735,6 +9746,7 @@ class BaseCase(unittest.TestCase):
timeout = settings.SMALL_TIMEOUT
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
timeout = self.__get_new_timeout(timeout)
original_selector = selector
selector, by = self.__recalculate_selector(selector, by)
if self.__is_shadow_selector(selector):
self.__assert_exact_shadow_text_visible(text, selector, timeout)
@ -9763,6 +9775,8 @@ class BaseCase(unittest.TestCase):
if url and len(url) > 0:
if ("http:") in url or ("https:") in url or ("file:") in url:
if self.get_session_storage_item("pause_recorder") == "no":
if by == By.XPATH:
selector = original_selector
time_stamp = self.execute_script("return Date.now();")
origin = self.get_origin()
text_selector = [text, selector]
@ -10001,12 +10015,16 @@ class BaseCase(unittest.TestCase):
timeout = settings.SMALL_TIMEOUT
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
timeout = self.__get_new_timeout(timeout)
original_selector = selector
selector, by = self.__recalculate_selector(selector, by)
self.wait_for_element_not_visible(selector, by=by, timeout=timeout)
if self.recorder_mode:
url = self.get_current_url()
if url and len(url) > 0:
if ("http:") in url or ("https:") in url or ("file:") in url:
if self.get_session_storage_item("pause_recorder") == "no":
if by == By.XPATH:
selector = original_selector
time_stamp = self.execute_script("return Date.now();")
origin = self.get_origin()
action = ["asenv", selector, origin, time_stamp]
@ -11437,12 +11455,13 @@ class BaseCase(unittest.TestCase):
# This raises an exception if the test is not coming from pytest
self.is_pytest = sb_config.is_pytest
except Exception:
# Not using pytest (probably nosetests)
# Not using pytest (could be nosetests, behave, or raw Python)
self.is_pytest = False
if self.is_pytest:
# pytest-specific code
test_id = self.__get_test_id()
self.test_id = test_id
self.is_behave = False
if hasattr(self, "_using_sb_fixture"):
self.test_id = sb_config._test_id
if hasattr(sb_config, "_sb_pdb_driver"):
@ -11589,7 +11608,7 @@ class BaseCase(unittest.TestCase):
self.testcase_manager = TestcaseManager(self.database_env)
#
exec_payload = ExecutionQueryPayload()
exec_payload.execution_start_time = int(time.time() * 1000)
exec_payload.execution_start_time = int(time.time() * 1000.0)
self.execution_start_time = exec_payload.execution_start_time
exec_payload.guid = self.execution_guid
exec_payload.username = getpass.getuser()
@ -11833,9 +11852,7 @@ class BaseCase(unittest.TestCase):
# Although the pytest clock starts before setUp() begins,
# the time-limit clock starts at the end of the setUp() method.
sb_config.start_time_ms = int(time.time() * 1000.0)
if not self.__start_time_ms:
# Call this once in case of multiple setUp() calls in the same test
self.__start_time_ms = sb_config.start_time_ms
self.__start_time_ms = sb_config.start_time_ms
def __set_last_page_screenshot(self):
"""self.__last_page_screenshot is only for pytest html report logs.
@ -11917,7 +11934,7 @@ class BaseCase(unittest.TestCase):
from seleniumbase.core.testcase_manager import TestcaseDataPayload
data_payload = TestcaseDataPayload()
data_payload.runtime = int(time.time() * 1000) - self.case_start_time
data_payload.runtime = int(time.time() * 1000.0) - self.case_start_time
data_payload.guid = self.testcase_guid
data_payload.execution_guid = self.execution_guid
data_payload.state = state
@ -12186,7 +12203,7 @@ class BaseCase(unittest.TestCase):
if hasattr(self, "_using_sb_fixture") and self.__will_be_skipped:
test_id = sb_config._test_id
if not init:
duration_ms = int(time.time() * 1000) - self.__start_time_ms
duration_ms = int(time.time() * 1000.0) - self.__start_time_ms
duration = float(duration_ms) / 1000.0
duration = "{:.2f}".format(duration)
sb_config._duration[test_id] = duration
@ -12707,7 +12724,7 @@ class BaseCase(unittest.TestCase):
self.__insert_test_result(
constants.State.PASSED, False
)
runtime = int(time.time() * 1000) - self.execution_start_time
runtime = int(time.time() * 1000.0) - self.execution_start_time
self.testcase_manager.update_execution_data(
self.execution_guid, runtime
)

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
SeleniumBase constants are stored in this file.
"""
@ -20,6 +21,22 @@ class Environment:
TEST = "test"
class ValidEnvs:
valid_envs = [
"qa",
"staging",
"develop",
"production",
"master",
"remote",
"local",
"alpha",
"beta",
"main",
"test",
]
class Files:
DOWNLOADS_FOLDER = "downloaded_files"
ARCHIVED_DOWNLOADS_FOLDER = "archived_files"
@ -38,7 +55,7 @@ class Recordings:
class Dashboard:
TITLE = "SeleniumBase Test Results Dashboard"
TITLE = "SeleniumBase Dashboard ⚪"
# STYLE_CSS = "https://seleniumbase.io/cdn/css/pytest_style.css"
STYLE_CSS = "assets/pytest_style.css" # Generated before tests
META_REFRESH_HTML = '<meta http-equiv="refresh" content="12">'

View File

@ -158,8 +158,8 @@ def hover_element(driver, element):
def timeout_exception(exception, message):
exception, message = s_utils.format_exc(exception, message)
raise exception(message)
exc, msg = shared_utils.format_exc(exception, message)
raise exc(msg)
def hover_and_click(
@ -302,7 +302,7 @@ def wait_for_element_present(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
element = driver.find_element(by=by, value=selector)
return element
@ -359,7 +359,7 @@ def wait_for_element_visible(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
element = driver.find_element(by=by, value=selector)
is_present = True
@ -652,7 +652,7 @@ def wait_for_attribute(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
element = driver.find_element(by=by, value=selector)
element_present = True
@ -728,7 +728,7 @@ def wait_for_element_absent(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
driver.find_element(by=by, value=selector)
now_ms = time.time() * 1000.0
@ -775,7 +775,7 @@ def wait_for_element_not_visible(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
element = driver.find_element(by=by, value=selector)
if element.is_displayed():
@ -823,7 +823,7 @@ def wait_for_text_not_visible(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
if not is_text_visible(driver, text, selector, by=by):
return True
now_ms = time.time() * 1000.0
@ -866,7 +866,7 @@ def wait_for_attribute_not_present(
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
if not is_attribute_present(
driver, selector, attribute, value=value, by=by
):
@ -1067,7 +1067,7 @@ def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT):
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
alert = driver.switch_to.alert
# Raises exception if no alert present
@ -1096,11 +1096,11 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
start_ms = time.time() * 1000.0
stop_ms = start_ms + (timeout * 1000.0)
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
driver.switch_to.frame(frame)
return True
except (NoSuchFrameException, TimeoutException):
except Exception:
if type(frame) is str:
by = None
if page_utils.is_xpath_selector(frame):
@ -1142,7 +1142,7 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
stop_ms = start_ms + (timeout * 1000.0)
if isinstance(window, int):
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
window_handle = driver.window_handles[window]
driver.switch_to.window(window_handle)
@ -1164,7 +1164,7 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
else:
window_handle = window
for x in range(int(timeout * 10)):
s_utils.check_if_time_limit_exceeded()
shared_utils.check_if_time_limit_exceeded()
try:
driver.switch_to.window(window_handle)
return True

View File

@ -47,7 +47,11 @@ class DBReporting(Plugin):
constants.Environment.DEVELOP,
constants.Environment.PRODUCTION,
constants.Environment.MASTER,
constants.Environment.REMOTE,
constants.Environment.LOCAL,
constants.Environment.ALPHA,
constants.Environment.BETA,
constants.Environment.MAIN,
constants.Environment.TEST,
),
default=constants.Environment.TEST,

View File

@ -313,6 +313,7 @@ def pytest_addoption(parser):
)
parser.addoption(
"--database_env",
"--database-env",
action="store",
dest="database_env",
choices=(
@ -321,7 +322,11 @@ def pytest_addoption(parser):
constants.Environment.DEVELOP,
constants.Environment.PRODUCTION,
constants.Environment.MASTER,
constants.Environment.REMOTE,
constants.Environment.LOCAL,
constants.Environment.ALPHA,
constants.Environment.BETA,
constants.Environment.MAIN,
constants.Environment.TEST,
),
default=constants.Environment.TEST,
@ -913,6 +918,7 @@ def pytest_addoption(parser):
)
sys_argv = sys.argv
arg_join = " ".join(sys.argv)
sb_config._browser_shortcut = None
# SeleniumBase does not support pytest-timeout due to hanging browsers.
@ -946,7 +952,6 @@ def pytest_addoption(parser):
or "--record" in sys_argv
or "--rec" in sys_argv
):
arg_join = " ".join(sys.argv)
if ("-n" in sys_argv) or (" -n=" in arg_join) or ("-c" in sys_argv):
raise Exception(
"\n\n Recorder Mode does NOT support multi-process mode (-n)!"

View File

@ -642,6 +642,7 @@ class SeleniumBrowser(Plugin):
'\n (Your browser choice was: "%s")\n' % browser
)
raise Exception(message)
test.test.is_nosetest = True
test.test.browser = self.options.browser
test.test.cap_file = self.options.cap_file
test.test.cap_string = self.options.cap_string