@@ -61,26 +61,35 @@ function findContainerWithExecute(container: ElementBase): WebdriverIO.Browser {
6161}
6262
6363async function injectDOMTestingLibrary ( container : ElementBase ) {
64- const containerWithExecute = findContainerWithExecute ( container ) ;
65- const shouldInjectDTL = await containerWithExecute . execute ( function ( ) {
64+ const browser = findContainerWithExecute ( container ) ;
65+ const shouldInjectDTL = await browser . execute ( function ( ) {
6666 return ! window . TestingLibraryDom ;
6767 } ) ;
6868
6969 if ( shouldInjectDTL ) {
70- await containerWithExecute . execute ( function ( library ) {
71- // add DOM Testing Library to page as a script tag to support Firefox
70+ await browser . execute ( function ( library ) {
7271 if ( navigator . userAgent . indexOf ( "Firefox" ) !== - 1 ) {
73- const script = window . document . createElement ( "script" ) ;
74- script . textContent = library ;
75- window . document . head . append ( script ) ;
76- window . eval ( library ) ;
72+ try {
73+ // Inject via inline-script
74+ const script = window . document . createElement ( "script" ) ;
75+ script . textContent = library ;
76+ window . document . head . append ( script ) ;
77+ if ( ! window . TestingLibraryDom ) {
78+ // Inject via eval
79+ window . eval ( library ) ;
80+ }
81+ } catch ( error ) {
82+ throw new Error (
83+ `The DOM Testing Library cannot be injected on certain domains, particularly "${ window . location . host } ", due to restrictions imposed by the Content-Security-Policy (CSP) header.` ,
84+ ) ;
85+ }
7786 } else {
7887 eval ( library ) ;
7988 }
8089 } , DOM_TESTING_LIBRARY_UMD ) ;
8190 }
8291
83- await containerWithExecute . execute ( function ( config : Partial < Config > ) {
92+ await browser . execute ( function ( config : Partial < Config > ) {
8493 window . TestingLibraryDom . configure ( config ) ;
8594 } , _config ) ;
8695}
0 commit comments