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 中选择特定元素 - 我应该如何处理它?我错过了什么吗?
更多细节:我正在创建一个带有 remote 的src
iframe,然后我向该 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
元素的内容可能还没有准备好,但我想知道如何才能等到它准备好?我试过timeout
为Selector
通话设置一个选项,但它没有改变任何东西。您能否分享有关如何在切换到 iframe 上下文后延迟获取选择器的任何提示?
事实证明,这确实是我的一个错误。对不起。对于未来的一代:Iframe 切换和使用选择器应该可以正常工作,至少在 TestCafe 中v0.20.1
。只要确保您的选择器匹配并且您确实在 iframe 上下文中,而不仅仅是认为您在那里
无需做任何特殊的事情即可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)
所有点击按预期工作,我能得到innerText
的Selector
成功。此外,我建议您检查页面上真正存在的元素是Selector
指现有元素,并且该元素的宽度和高度大于零。
归档时间: |
|
查看次数: |
2070 次 |
最近记录: |