@@ -39,6 +39,7 @@ def test_anything(self):
39
39
from selenium .common import exceptions as selenium_exceptions
40
40
from selenium .webdriver .common .by import By
41
41
from selenium .webdriver .common .keys import Keys
42
+ from selenium .webdriver .remote .remote_connection import LOGGER
42
43
from selenium .webdriver .support .ui import Select
43
44
from seleniumbase import config as sb_config
44
45
from seleniumbase .common import decorators
@@ -58,6 +59,7 @@ def test_anything(self):
58
59
logging .getLogger ("requests" ).setLevel (logging .ERROR )
59
60
logging .getLogger ("urllib3" ).setLevel (logging .ERROR )
60
61
urllib3 .disable_warnings ()
62
+ LOGGER .setLevel (logging .WARNING )
61
63
ENI_Exception = selenium_exceptions .ElementNotInteractableException
62
64
63
65
@@ -76,6 +78,7 @@ def __init__(self, *args, **kwargs):
76
78
self .__last_page_load_url = "data:,"
77
79
self .__last_page_screenshot = None
78
80
self .__last_page_screenshot_png = None
81
+ self .__added_pytest_html_extra = None
79
82
self .__delayed_assert_count = 0
80
83
self .__delayed_assert_failures = []
81
84
self .__device_width = None
@@ -4108,9 +4111,7 @@ def setUp(self, masterqa_mode=False):
4108
4111
self .is_pytest = False
4109
4112
if self .is_pytest :
4110
4113
# pytest-specific code
4111
- test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
4112
- self .__class__ .__name__ ,
4113
- self ._testMethodName )
4114
+ test_id = self .__get_test_id ()
4114
4115
self .browser = sb_config .browser
4115
4116
self .data = sb_config .data
4116
4117
self .slow_mode = sb_config .slow_mode
@@ -4216,10 +4217,10 @@ def setUp(self, masterqa_mode=False):
4216
4217
4217
4218
# Verify that SeleniumBase is installed successfully
4218
4219
if not hasattr (self , "browser" ):
4219
- raise Exception ("""SeleniumBase plugins did not load! """
4220
- """Please reinstall using:\n """
4221
- """ >>> "pip install -r requirements.txt" <<< \n """
4222
- """ >>> "python setup.py install" <<< """ )
4220
+ raise Exception ("""SeleniumBase plugins DID NOT load!\n \n """
4221
+ """*** Please REINSTALL SeleniumBase using: > \n """
4222
+ """ >>> "pip install -r requirements.txt"\n """
4223
+ """ >>> "python setup.py install" """ )
4223
4224
if self .settings_file :
4224
4225
settings_parser .set_settings (self .settings_file )
4225
4226
# Mobile Emulator device metrics: CSS Width, CSS Height, & Pixel-Ratio
@@ -4346,27 +4347,29 @@ def __insert_test_result(self, state, err):
4346
4347
self .testcase_manager .update_testcase_data (data_payload )
4347
4348
4348
4349
def __add_pytest_html_extra (self ):
4349
- try :
4350
- if self .with_selenium :
4351
- if not self .__last_page_screenshot :
4352
- self .__set_last_page_screenshot ()
4353
- if self .report_on :
4354
- extra_url = {}
4355
- extra_url ['name' ] = 'URL'
4356
- extra_url ['format' ] = 'url'
4357
- extra_url ['content' ] = self .get_current_url ()
4358
- extra_url ['mime_type' ] = None
4359
- extra_url ['extension' ] = None
4360
- extra_image = {}
4361
- extra_image ['name' ] = 'Screenshot'
4362
- extra_image ['format' ] = 'image'
4363
- extra_image ['content' ] = self .__last_page_screenshot
4364
- extra_image ['mime_type' ] = 'image/png'
4365
- extra_image ['extension' ] = 'png'
4366
- self ._html_report_extra .append (extra_url )
4367
- self ._html_report_extra .append (extra_image )
4368
- except Exception :
4369
- pass
4350
+ if not self .__added_pytest_html_extra :
4351
+ try :
4352
+ if self .with_selenium :
4353
+ if not self .__last_page_screenshot :
4354
+ self .__set_last_page_screenshot ()
4355
+ if self .report_on :
4356
+ extra_url = {}
4357
+ extra_url ['name' ] = 'URL'
4358
+ extra_url ['format' ] = 'url'
4359
+ extra_url ['content' ] = self .get_current_url ()
4360
+ extra_url ['mime_type' ] = None
4361
+ extra_url ['extension' ] = None
4362
+ extra_image = {}
4363
+ extra_image ['name' ] = 'Screenshot'
4364
+ extra_image ['format' ] = 'image'
4365
+ extra_image ['content' ] = self .__last_page_screenshot
4366
+ extra_image ['mime_type' ] = 'image/png'
4367
+ extra_image ['extension' ] = 'png'
4368
+ self .__added_pytest_html_extra = True
4369
+ self ._html_report_extra .append (extra_url )
4370
+ self ._html_report_extra .append (extra_image )
4371
+ except Exception :
4372
+ pass
4370
4373
4371
4374
def __quit_all_drivers (self ):
4372
4375
if self ._reuse_session and sb_config .shared_driver :
@@ -4392,19 +4395,55 @@ def __quit_all_drivers(self):
4392
4395
self ._default_driver = None
4393
4396
self ._drivers_list = []
4394
4397
4398
+ def __has_exception (self ):
4399
+ has_exception = False
4400
+ if sys .version_info [0 ] >= 3 and hasattr (self , '_outcome' ):
4401
+ if hasattr (self ._outcome , 'errors' ) and self ._outcome .errors :
4402
+ has_exception = True
4403
+ else :
4404
+ has_exception = sys .exc_info ()[1 ] is not None
4405
+ return has_exception
4406
+
4407
+ def __get_test_id (self ):
4408
+ test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
4409
+ self .__class__ .__name__ ,
4410
+ self ._testMethodName )
4411
+ return test_id
4412
+
4413
+ def __create_log_path_as_needed (self , test_logpath ):
4414
+ if not os .path .exists (test_logpath ):
4415
+ try :
4416
+ os .makedirs (test_logpath )
4417
+ except Exception :
4418
+ pass # Only reachable during multi-threaded runs
4419
+
4420
+ def save_teardown_screenshot (self ):
4421
+ """ (Should ONLY be used at the start of custom tearDown() methods.)
4422
+ This method takes a screenshot of the current web page for a
4423
+ failing test (or when running your tests with --save-screenshot).
4424
+ That way your tearDown() method can navigate away from the last
4425
+ page where the test failed, and still get the correct screenshot
4426
+ before performing tearDown() steps on other pages. If this method
4427
+ is not included in your custom tearDown() method, a screenshot
4428
+ will still be taken after the last step of your tearDown(), where
4429
+ you should be calling "super(SubClassOfBaseCase, self).tearDown()"
4430
+ """
4431
+ test_id = self .__get_test_id ()
4432
+ test_logpath = self .log_path + "/" + test_id
4433
+ self .__create_log_path_as_needed (test_logpath )
4434
+ if self .__has_exception () or self .save_screenshot_after_test :
4435
+ self .__set_last_page_screenshot ()
4436
+ if self .is_pytest :
4437
+ self .__add_pytest_html_extra ()
4438
+
4395
4439
def tearDown (self ):
4396
4440
"""
4397
4441
Be careful if a subclass of BaseCase overrides setUp()
4398
4442
You'll need to add the following line to the subclass's tearDown():
4399
4443
super(SubClassOfBaseCase, self).tearDown()
4400
4444
"""
4401
4445
self .__slow_mode_pause_if_active ()
4402
- has_exception = False
4403
- if sys .version_info [0 ] >= 3 and hasattr (self , '_outcome' ):
4404
- if hasattr (self ._outcome , 'errors' ) and self ._outcome .errors :
4405
- has_exception = True
4406
- else :
4407
- has_exception = sys .exc_info ()[1 ] is not None
4446
+ has_exception = self .__has_exception ()
4408
4447
if self .__delayed_assert_failures :
4409
4448
print (
4410
4449
"\n When using self.delayed_assert_*() methods in your tests, "
@@ -4414,18 +4453,9 @@ def tearDown(self):
4414
4453
self .process_delayed_asserts ()
4415
4454
else :
4416
4455
self .process_delayed_asserts (print_only = True )
4417
- self .is_pytest = None
4418
- try :
4419
- # This raises an exception if the test is not coming from pytest
4420
- self .is_pytest = sb_config .is_pytest
4421
- except Exception :
4422
- # Not using pytest (probably nosetests)
4423
- self .is_pytest = False
4424
4456
if self .is_pytest :
4425
4457
# pytest-specific code
4426
- test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
4427
- self .__class__ .__name__ ,
4428
- self ._testMethodName )
4458
+ test_id = self .__get_test_id ()
4429
4459
try :
4430
4460
with_selenium = self .with_selenium
4431
4461
except Exception :
@@ -4460,11 +4490,7 @@ def tearDown(self):
4460
4490
if self .with_testing_base and not has_exception and (
4461
4491
self .save_screenshot_after_test ):
4462
4492
test_logpath = self .log_path + "/" + test_id
4463
- if not os .path .exists (test_logpath ):
4464
- try :
4465
- os .makedirs (test_logpath )
4466
- except Exception :
4467
- pass # Only reachable during multi-threaded runs
4493
+ self .__create_log_path_as_needed (test_logpath )
4468
4494
if not self .__last_page_screenshot_png :
4469
4495
self .__set_last_page_screenshot ()
4470
4496
log_helper .log_screenshot (
@@ -4474,11 +4500,7 @@ def tearDown(self):
4474
4500
self .__add_pytest_html_extra ()
4475
4501
if self .with_testing_base and has_exception :
4476
4502
test_logpath = self .log_path + "/" + test_id
4477
- if not os .path .exists (test_logpath ):
4478
- try :
4479
- os .makedirs (test_logpath )
4480
- except Exception :
4481
- pass # Only reachable during multi-threaded runs
4503
+ self .__create_log_path_as_needed (test_logpath )
4482
4504
if ((not self .with_screen_shots ) and (
4483
4505
not self .with_basic_test_info ) and (
4484
4506
not self .with_page_source )):
@@ -4553,15 +4575,9 @@ def tearDown(self):
4553
4575
else :
4554
4576
# (Nosetests)
4555
4577
if has_exception :
4556
- test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
4557
- self .__class__ .__name__ ,
4558
- self ._testMethodName )
4578
+ test_id = self .__get_test_id ()
4559
4579
test_logpath = self .log_path + "/" + test_id
4560
- if not os .path .exists (test_logpath ):
4561
- try :
4562
- os .makedirs (test_logpath )
4563
- except Exception :
4564
- pass # Only reachable during multi-threaded runs
4580
+ self .__create_log_path_as_needed (test_logpath )
4565
4581
log_helper .log_test_failure_data (
4566
4582
self , test_logpath , self .driver , self .browser )
4567
4583
if len (self ._drivers_list ) > 0 :
@@ -4573,15 +4589,9 @@ def tearDown(self):
4573
4589
self .__last_page_screenshot_png )
4574
4590
log_helper .log_page_source (test_logpath , self .driver )
4575
4591
elif self .save_screenshot_after_test :
4576
- test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
4577
- self .__class__ .__name__ ,
4578
- self ._testMethodName )
4592
+ test_id = self .__get_test_id ()
4579
4593
test_logpath = self .log_path + "/" + test_id
4580
- if not os .path .exists (test_logpath ):
4581
- try :
4582
- os .makedirs (test_logpath )
4583
- except Exception :
4584
- pass # Only reachable during multi-threaded runs
4594
+ self .__create_log_path_as_needed (test_logpath )
4585
4595
if not self .__last_page_screenshot_png :
4586
4596
self .__set_last_page_screenshot ()
4587
4597
log_helper .log_screenshot (
0 commit comments