Según la documentación de Testcafe, debería poder inyectar un script de cliente en todas las páginas: https://testcafe.io/documentation/402843/guides/advanced-guides/inject-client-scripts#add-client-scripts-to-all -pruebas
Actualmente lo tengo configurado para inyectar este script para que pueda descartar las notificaciones que aparecen en nuestra aplicación que superponen los botones con los que necesitamos interactuar.
const notifications_div = document.querySelector('.notifications') if (notifications_div) { // Creates the MutationObserver and triggers the callback const mutationObserver = new MutationObserver(() => { // Checks to see if the style is set to 'block' if (notifications_div.style.display == 'block') { // Set the dismiss button to a variable each time since the previous will no longer exist. let dismiss_button = document.querySelector("a[data-turbo-method='delete']") // Click the dismiss button; Timeout is needed to avoid race condition errors. setTimeout(() => { dismiss_button.click(); }, 3000); // Hide the notifications_div again since it never truly goes away; Timeout is needed to avoid race condition errors. setTimeout(() => { notifications_div.style.display = 'none'; }, 3000); } }) // Starts the observation of the notifications_div and checks for a change on 'style' mutationObserver.observe(notifications_div, { attributes: true, attributeOldValue: true, attributeFilter: ['style'] }) }Cuando ejecuto este código en la consola y luego activo una notificación, funciona bien. Cuando ejecuto una suite de testcafe, todavía termino viendo notificaciones (que aparecen de forma asincrónica), cubro el botón con el que necesito interactuar y nunca cierro.
¿Cuándo se inyecta realmente el código? ¿Es cada carga de página?
Video del script funcionando bien a través de la consola: https://www.loom.com/share/1a5b96d054a345748e4f018bc56af413
La inyección de Client Script debería funcionar incluso después de cargar una nueva página. El siguiente fragmento de código demuestra que se inyecta el script:
fixture`f` .page`http://example.com`; const clientScript = ` console.log('location: ' + window.location.href); `; test ('My test', async t => { await t.navigateTo('http://google.com'); await t.debug(); }) .clientScripts({ content: clientScript });Si este código no ayuda, cree un problema separado en el repositorio oficial de TestCafe usando la siguiente plantilla y comparta un ejemplo donde se reproduzca el problema: https://github.com/DevExpress/testcafe/issues/new?assignees=&labels =TYPE%3A+bug&template=bug_report.yaml .