简体   繁体   English

testcafe 中的 clientScript() function 不适用于所有页面

[英]clientScript() function in testcafe isn't working on all pages

According to Testcafe's documentation, I should be able to inject a clientScript into all pages: https://testcafe.io/documentation/402843/guides/advanced-guides/inject-client-scripts#add-client-scripts-to-all-tests根据 Testcafe 的文档,我应该能够将 clientScript 注入所有页面: https://testcafe.io/documentation/402843/guides/advanced-guides/inject-client-scripts#add-client-scripts-to-all -测试

I currently have it set up to inject this script so that it can dismiss notifications that pop up in our application which overlay buttons that we need to interact with.我目前已将其设置为注入此脚本,以便它可以消除在我们的应用程序中弹出的通知,这些通知覆盖了我们需要与之交互的按钮。

 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'] }) }

When I run this code in the console and then trigger a notification, it works just fine.当我在控制台中运行此代码然后触发通知时,它工作得很好。 When I run a testcafe suite I still end up seeing notifications (that asynchronously pop up), cover the button that I need to interact with, and never close.当我运行一个 testcafe 套件时,我仍然会看到通知(异步弹出),覆盖我需要与之交互的按钮,并且永远不会关闭。

When does the code actually get injected?代码何时真正被注入? Is it every page load?是每页加载吗?

Video of the script working fine via the console: https://www.loom.com/share/1a5b96d054a345748e4f018bc56af413通过控制台运行正常的脚本视频: https://www.loom.com/share/1a5b96d054a345748e4f018bc56af413

在此处输入图像描述

在此处输入图像描述

The Client Script injection should work even after a new page is loaded.即使在加载新页面后,客户端脚本注入也应该起作用。 The following code snippet demonstrates that the script is injected:下面的代码片段演示了脚本被注入:

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 });


If this code does not help, please create a separate issue in the TestCafe official repository using the following template and share an example where the problem is reproduced: https://github.com/DevExpress/testcafe/issues/new?assignees=&labels=TYPE%3A+bug&template=bug_report.yaml .如果此代码没有帮助,请使用以下模板在 TestCafe 官方存储库中创建一个单独的问题,并分享重现问题的示例: https://github.com/DevExpress/testcafe/issues/new?assignees=&labels =TYPE%3A+bug&template=bug_report.yaml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM