4
4
by giving page elements enough time to load before taking action on them.
5
5
"""
6
6
7
+ import getpass
7
8
import json
8
9
import logging
9
10
import os
10
11
import pytest
11
12
import sys
12
13
import time
13
14
import unittest
15
+ import uuid
14
16
from pyvirtualdisplay import Display
15
17
from seleniumbase .config import settings
18
+ from seleniumbase .core .application_manager import ApplicationManager
19
+ from seleniumbase .core .testcase_manager import ExecutionQueryPayload
20
+ from seleniumbase .core .testcase_manager import TestcaseDataPayload
21
+ from seleniumbase .core .testcase_manager import TestcaseManager
16
22
from seleniumbase .core import browser_launcher
17
23
from seleniumbase .core import log_helper
24
+ from seleniumbase .fixtures import constants
18
25
from selenium .webdriver .remote .webdriver import WebDriver
19
26
from selenium .webdriver .common .by import By
20
27
import page_actions
@@ -325,10 +332,15 @@ def setUp(self):
325
332
# Not using pytest (probably nosetests)
326
333
self .is_pytest = False
327
334
if self .is_pytest :
335
+ test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
336
+ self .__class__ .__name__ ,
337
+ self ._testMethodName )
328
338
self .with_selenium = pytest .config .option .with_selenium
329
339
self .headless = pytest .config .option .headless
330
340
self .headless_active = False
331
341
self .with_testing_base = pytest .config .option .with_testing_base
342
+ self .with_db_reporting = pytest .config .option .with_db_reporting
343
+ self .database_env = pytest .config .option .database_env
332
344
self .log_path = pytest .config .option .log_path
333
345
self .browser = pytest .config .option .browser
334
346
self .data = pytest .config .option .data
@@ -340,6 +352,52 @@ def setUp(self):
340
352
self .headless_active = True
341
353
if self .with_selenium :
342
354
self .driver = browser_launcher .get_driver (self .browser )
355
+ if self .with_db_reporting :
356
+ self .execution_guid = str (uuid .uuid4 ())
357
+ self .testcase_guid = None
358
+ self .execution_start_time = 0
359
+ self .case_start_time = 0
360
+ self .application = None
361
+ self .testcase_manager = None
362
+ self .error_handled = False
363
+ self .testcase_manager = TestcaseManager (self .database_env )
364
+ #
365
+ exec_payload = ExecutionQueryPayload ()
366
+ exec_payload .execution_start_time = int (time .time () * 1000 )
367
+ self .execution_start_time = exec_payload .execution_start_time
368
+ exec_payload .guid = self .execution_guid
369
+ exec_payload .username = getpass .getuser ()
370
+ self .testcase_manager .insert_execution_data (exec_payload )
371
+ #
372
+ data_payload = TestcaseDataPayload ()
373
+ self .testcase_guid = str (uuid .uuid4 ())
374
+ data_payload .guid = self .testcase_guid
375
+ data_payload .execution_guid = self .execution_guid
376
+ if self .with_selenium :
377
+ data_payload .browser = self .browser
378
+ else :
379
+ data_payload .browser = "N/A"
380
+ data_payload .testcaseAddress = test_id
381
+ application = ApplicationManager .generate_application_string (
382
+ self ._testMethodName )
383
+ data_payload .env = application .split ('.' )[0 ]
384
+ data_payload .start_time = application .split ('.' )[1 ]
385
+ data_payload .state = constants .State .NOTRUN
386
+ self .testcase_manager .insert_testcase_data (data_payload )
387
+ self .case_start_time = int (time .time () * 1000 )
388
+
389
+ def __insert_test_result (self , state , err = None ):
390
+ data_payload = TestcaseDataPayload ()
391
+ data_payload .runtime = int (time .time () * 1000 ) - self .case_start_time
392
+ data_payload .guid = self .testcase_guid
393
+ data_payload .execution_guid = self .execution_guid
394
+ data_payload .state = state
395
+ if err is not None :
396
+ data_payload .message = err [1 ].__str__ ().split (
397
+ '''-------------------- >> '''
398
+ '''begin captured logging'''
399
+ ''' << --------------------''' , 1 )[0 ]
400
+ self .testcase_manager .update_testcase_data (data_payload )
343
401
344
402
def tearDown (self ):
345
403
"""
@@ -349,12 +407,12 @@ def tearDown(self):
349
407
super(SubClassOfBaseCase, self).tearDown()
350
408
"""
351
409
if self .is_pytest :
410
+ test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
411
+ self .__class__ .__name__ ,
412
+ self ._testMethodName )
352
413
if self .with_selenium :
353
414
# Save a screenshot if logging is on when an exception occurs
354
415
if self .with_testing_base and (sys .exc_info ()[1 ] is not None ):
355
- test_id = "%s.%s.%s" % (self .__class__ .__module__ ,
356
- self .__class__ .__name__ ,
357
- self ._testMethodName )
358
416
test_logpath = self .log_path + "/" + test_id
359
417
if not os .path .exists (test_logpath ):
360
418
os .makedirs (test_logpath )
@@ -370,3 +428,11 @@ def tearDown(self):
370
428
if self .headless :
371
429
if self .headless_active :
372
430
self .display .stop ()
431
+ if self .with_db_reporting :
432
+ if sys .exc_info ()[1 ] is not None :
433
+ self .__insert_test_result (constants .State .ERROR )
434
+ else :
435
+ self .__insert_test_result (constants .State .PASS )
436
+ runtime = int (time .time () * 1000 ) - self .execution_start_time
437
+ self .testcase_manager .update_execution_data (
438
+ self .execution_guid , runtime )
0 commit comments