在 TestCafe 中切换到 iframe 上下文后找不到元素

wop*_*low 3 testing iframe automated-tests e2e-testing testcafe

原问题:

使用

browser.switchToFrame(iframeEl);
Run Code Online (Sandbox Code Playgroud)

我可以切换到特定的 iframe 窗口,没关系 - 单击元素等工作。

但是,无论出于何种原因,使用Selector时,iframe是当前的背景下,不能正常工作。我怀疑这是因为switchToFrame浏览器实例上的一个方法,我正在使用Selector以这种方式导入的函数:

import { Selector } from 'testcafe';
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 如果我想使用 testcafe(例如读取其 HTML 属性)在 iframe 中选择特定元素 - 我应该如何处理它?我错过了什么吗?

来自 GitHub 线程的更多详细信息:

更多细节:我正在创建一个带有 remote 的srciframe,然后我向该 iframe 注入了一些 HTML、CSS 和 JavaScript。我 100% 确定iframe将具有与我请求的选择器匹配的 DOM 元素 - 但仍然出现错误:Cannot obtain information about the node because the specified selector does not match any node in the DOM tree.

我的代码大致如下:

const iframeEl = await Selector('a-iframe_inner[name="something"]');
    if (await iframeEl.count === 0) {
      await this.fBrowser.switchToMainWindow();
    } else {
      await this.fBrowser.switchToIframe(iframeEl);
    }

const something = await Selector('.something');
Run Code Online (Sandbox Code Playgroud)

并在一行await Selector代码中断。在我的其他测试中,我还访问了 iframe 并使用await browser.click(someOtherThing);它单击了一些元素,它可以完美地工作。我还可以轻松读取 iframe 的控制台状态。

我怀疑iframe元素的内容可能还没有准备好,但我想知道如何才能等到它准备好?我试过timeoutSelector通话设置一个选项,但它没有改变任何东西。您能否分享有关如何在切换到 iframe 上下文后延迟获取选择器的任何提示?

解决方案:

事实证明,这确实是我的一个错误。对不起。对于未来的一代:Iframe 切换和使用选择器应该可以正常工作,至少在 TestCafe 中v0.20.1。只要确保您的选择器匹配并且您确实在 iframe 上下文中,而不仅仅是认为您在那里

Ale*_*aev 5

无需做任何特殊的事情即可Selectors在 iframe 中工作。它应该按预期工作。如果他们不希望使用此表单在官方存储库中创建错误报告,如果您提供一个演示该问题的示例,我将不胜感激。至于你的问题,我无法在一个简单的例子中重现这个问题。测试页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>Click me</h1>

<iframe id="frame" src="http://example.com" style="width: 500px; height: 500px;"></iframe>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

测试代码:

import { Selector } from 'testcafe';

fixture `Selector in iframe`
    .page `../pages/index.html`;

const selector = Selector('h1');

test('test', async t => {
    await t.click(selector);

    console.log(await selector.innerText);

    await t.switchToIframe('#frame');

    await t.click(selector);

    console.log(await selector.innerText);
});
Run Code Online (Sandbox Code Playgroud)

所有点击按预期工作,我能得到innerTextSelector成功。此外,我建议您检查页面上真正存在的元素是Selector指现有元素,并且该元素的宽度和高度大于零。

  • 感谢您的回答!当您一直在回复时,我在 TestCafe 存储库中创建了一个问题,对我的案例进行了更明确的描述。我会相应地更新问题。同时,长话短说 - 有没有办法以某种方式延迟 Selctor 以确保它可用?选择器的 `timeout` 选项在我的情况下不起作用,我想知道为什么以及是否还有其他选项可以等到它出现。我 100% 肯定,它存在于 iframe *在某些时候* 问题链接:https://github.com/DevExpress/testcafe/issues/3478 (2认同)
  • `timeout` 选项或 `--selector-timeout` 命令行选项也应该有效。让我们在github线程中继续讨论。 (2认同)