@@ -437,24 +437,22 @@ def release(self):
437437class WebDriverTestDriverProtocolPart (TestDriverProtocolPart ):
438438 def setup (self ):
439439 self .webdriver = self .parent .webdriver
440- with open (os .path .join (here , "testharness_webdriver_resume.js" )) as f :
441- self .script_resume = f .read ()
442440
443- def get_next_message (self , url , test_window ):
441+ def get_next_message (self , url , script_resume , test_window ):
444442 if hasattr (self .parent , "bidi_script" ):
445443
446- return self ._get_next_message_bidi (url , test_window )
444+ return self ._get_next_message_bidi (url , script_resume , test_window )
447445 else :
448- return self ._get_next_message_classic (url )
446+ return self ._get_next_message_classic (url , script_resume )
449447
450- def _get_next_message_classic (self , url ):
448+ def _get_next_message_classic (self , url , script_resume ):
451449 """
452450 Get the next message from the test_driver using the classic WebDriver async script execution. This will block
453451 the event loop until the test_driver send a message.
454452 """
455- return self .parent .base .execute_script (self . script_resume , asynchronous = True , args = [strip_server (url )])
453+ return self .parent .base .execute_script (script_resume , asynchronous = True , args = [strip_server (url )])
456454
457- def _get_next_message_bidi (self , url , test_window ):
455+ def _get_next_message_bidi (self , url , script_resume , test_window ):
458456 """
459457 Get the next message from the test_driver using async call. This will not block the event loop, which allows for
460458 processing the events from the test_runner to test_driver while waiting for the next test_driver commands.
@@ -470,7 +468,7 @@ def _get_next_message_bidi(self, url, test_window):
470468 %s
471469 }).apply(null, args);
472470 })
473- }""" % self . script_resume
471+ }""" % script_resume
474472
475473 bidi_url_argument = {
476474 "type" : "string" ,
@@ -837,64 +835,19 @@ def run_func(self):
837835 self .result_flag .set ()
838836
839837
840- class WebDriverTestharnessExecutor (TestharnessExecutor ):
841- supports_testdriver = True
842- protocol_cls = WebDriverProtocol
843-
844- def __init__ (self , logger , browser , server_config , timeout_multiplier = 1 ,
845- close_after_done = True , capabilities = None , debug_info = None ,
846- cleanup_after_test = True , ** kwargs ):
847- """WebDriver-based executor for testharness.js tests"""
848- TestharnessExecutor .__init__ (self , logger , browser , server_config ,
849- timeout_multiplier = timeout_multiplier ,
850- debug_info = debug_info )
851- self .protocol = self .protocol_cls (self , browser , capabilities )
852- with open (os .path .join (here , "window-loaded.js" )) as f :
853- self .window_loaded_script = f .read ()
854-
855-
856- self .close_after_done = close_after_done
857- self .cleanup_after_test = cleanup_after_test
858-
859- def is_alive (self ):
860- return self .protocol .is_alive ()
861-
862- def on_environment_change (self , new_environment ):
863- if new_environment ["protocol" ] != self .last_environment ["protocol" ]:
864- self .protocol .testharness .load_runner (new_environment ["protocol" ])
865-
866- def do_test (self , test ):
867- url = self .test_url (test )
868-
869- success , data = WebDriverRun (self .logger ,
870- self .do_testharness ,
871- self .protocol ,
872- url ,
873- test .timeout * self .timeout_multiplier ,
874- self .extra_timeout ).run ()
875-
876- if success :
877- data , extra = data
878- return self .convert_result (test , data , extra = extra )
879838
880- return (test .make_result (* data ), [])
881839
882- def do_testharness (self , protocol , url , timeout ):
883-
884-
885- protocol .testharness .close_old_windows ()
840+ class TestDriverExecutorMixin :
841+ def __init__ (self , script_resume : str ):
842+ self .script_resume = script_resume
886843
844+ def run_testdriver (self , protocol , url , timeout ):
887845
888846 if hasattr (protocol , 'bidi_events' ):
889847
890848 protocol .loop .run_until_complete (protocol .bidi_events .unsubscribe_all ())
891849
892-
893850 test_window = self .get_or_create_test_window (protocol )
894- self .protocol .base .set_window (test_window )
895-
896- protocol .base .execute_script (self .window_loaded_script , asynchronous = True )
897-
898851
899852 unexpected_exceptions = []
900853
@@ -948,7 +901,8 @@ async def process_bidi_event(method, params):
948901
949902 raise unexpected_exceptions [0 ]
950903
951- test_driver_message = protocol .testdriver .get_next_message (url , test_window )
904+ test_driver_message = protocol .testdriver .get_next_message (url , self .script_resume ,
905+ test_window )
952906 self .logger .debug ("Receive message from testdriver: %s" % test_driver_message )
953907
954908
@@ -981,36 +935,100 @@ async def process_bidi_event(method, params):
981935
982936 protocol .loop .run_until_complete (protocol .bidi_events .unsubscribe_all ())
983937
984- extra = {}
985- if leak_part := getattr (protocol , "leak" , None ):
986- testharness_window = protocol .base .current_window
987- extra_windows = set (protocol .base .window_handles ())
988- extra_windows -= {protocol .testharness .runner_handle , testharness_window }
989- protocol .testharness .close_windows (extra_windows )
990- try :
991- protocol .base .set_window (testharness_window )
992- if counters := leak_part .check ():
993- extra ["leak_counters" ] = counters
994- except webdriver_error .NoSuchWindowException :
995- pass
996- finally :
997- protocol .base .set_window (protocol .testharness .runner_handle )
998-
999-
1000-
1001-
1002-
1003- if self .cleanup_after_test :
1004- protocol .testharness .close_old_windows ()
1005-
1006938 if len (unexpected_exceptions ) > 0 :
1007939
1008940 raise unexpected_exceptions [0 ]
1009941
1010- return rv , extra
942+ return rv
1011943
1012944 def get_or_create_test_window (self , protocol ):
1013- return protocol .base .create_window ()
945+ return protocol .base .current_window
946+
947+
948+ class WebDriverTestharnessExecutor (TestharnessExecutor , TestDriverExecutorMixin ):
949+ supports_testdriver = True
950+ protocol_cls = WebDriverProtocol
951+
952+ def __init__ (self , logger , browser , server_config , timeout_multiplier = 1 ,
953+ close_after_done = True , capabilities = None , debug_info = None ,
954+ cleanup_after_test = True , ** kwargs ):
955+ """WebDriver-based executor for testharness.js tests"""
956+ TestharnessExecutor .__init__ (self , logger , browser , server_config ,
957+ timeout_multiplier = timeout_multiplier ,
958+ debug_info = debug_info )
959+ self .protocol = self .protocol_cls (self , browser , capabilities )
960+ with open (os .path .join (here , "testharness_webdriver_resume.js" )) as f :
961+ script_resume = f .read ()
962+ TestDriverExecutorMixin .__init__ (self , script_resume )
963+ with open (os .path .join (here , "window-loaded.js" )) as f :
964+ self .window_loaded_script = f .read ()
965+
966+ self .close_after_done = close_after_done
967+ self .cleanup_after_test = cleanup_after_test
968+
969+ def is_alive (self ):
970+ return self .protocol .is_alive ()
971+
972+ def on_environment_change (self , new_environment ):
973+ if new_environment ["protocol" ] != self .last_environment ["protocol" ]:
974+ self .protocol .testharness .load_runner (new_environment ["protocol" ])
975+
976+ def do_test (self , test ):
977+ url = self .test_url (test )
978+
979+ success , data = WebDriverRun (self .logger ,
980+ self .do_testharness ,
981+ self .protocol ,
982+ url ,
983+ test .timeout * self .timeout_multiplier ,
984+ self .extra_timeout ).run ()
985+
986+ if success :
987+ data , extra = data
988+ return self .convert_result (test , data , extra = extra )
989+
990+ return (test .make_result (* data ), [])
991+
992+ def do_testharness (self , protocol , url , timeout ):
993+ try :
994+
995+
996+ protocol .testharness .close_old_windows ()
997+ raw_results = self .run_testdriver (protocol , url , timeout )
998+ extra = {}
999+ if counters := self ._check_for_leaks (protocol ):
1000+ extra ["leak_counters" ] = counters
1001+ return raw_results , extra
1002+ finally :
1003+
1004+
1005+
1006+
1007+ if self .cleanup_after_test :
1008+ protocol .testharness .close_old_windows ()
1009+
1010+ def _check_for_leaks (self , protocol ):
1011+ leak_part = getattr (protocol , "leak" , None )
1012+ if not leak_part :
1013+ return None
1014+ testharness_window = protocol .base .current_window
1015+ extra_windows = set (protocol .base .window_handles ())
1016+ extra_windows -= {protocol .testharness .runner_handle , testharness_window }
1017+ protocol .testharness .close_windows (extra_windows )
1018+ try :
1019+ protocol .base .set_window (testharness_window )
1020+ return leak_part .check ()
1021+ except webdriver_error .NoSuchWindowException :
1022+ return None
1023+ finally :
1024+ protocol .base .set_window (protocol .testharness .runner_handle )
1025+
1026+ def get_or_create_test_window (self , protocol ):
1027+ test_window = protocol .base .create_window ()
1028+ protocol .base .set_window (test_window )
1029+
1030+ protocol .base .execute_script (self .window_loaded_script , asynchronous = True )
1031+ return test_window
10141032
10151033
10161034class WebDriverRefTestExecutor (RefTestExecutor ):
0 commit comments