无法使用柏树切换到新的选项卡或窗口
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Change Content - Javascript</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<a id="myelement" href="javascript:void(0);" onclick="openNewTab()" "="">Open New Tab </a>
<script type="text/javascript">//<![CDATA[
function openNewTab() {
window.openNew({
target:'test_blank',
url: 'https://www.google.co.in/'
});
}
function openNew(option) {
const form = document.createElement("form");
form.target = option.target || "_blank";
form.action = option.url;
form.method = "GET";
document.body.appendChild(form);
form.submit();
//debugger;
$(form).remove();
}
//]]>
</script>
</body>
</html>
我有一个场景,我必须单击一个链接,该链接打开一个新的选项卡/窗口,因为赛普拉斯不支持多个选项卡,我在下面找到了下面的方法,但它不起作用,它打开了新标签,但无法切换新标签和我的测试失败了,错误:
期望重定向至少一次被调用一次,但从未被调用。
cy.
visit('https://qa.abc.com/xyz/documents?action_id=1');
cy
.window().then((win) => {
cy.spy(win, 'open').as('redirect');
});
cy
.get(':nth-child(1) > [style="width: 228px;"] > .text-ellipsis')
.click();
cy
.get('@redirect')
.should('be.called');
注意:重定向的URL是动态的,并与JavaScript结合,因此无法从控制台中获取URL也无法从链接中删除属性。
这是附带的屏幕截图:在此处输入图像描述
上面是示例html文件,它重复了该文件问题:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Change Content - Javascript</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<a id="myelement" href="javascript:void(0);" onclick="openNewTab()" "="">Open New Tab </a>
<script type="text/javascript">//<![CDATA[
function openNewTab() {
window.openNew({
target:'test_blank',
url: 'https://www.google.co.in/'
});
}
function openNew(option) {
const form = document.createElement("form");
form.target = option.target || "_blank";
form.action = option.url;
form.method = "GET";
document.body.appendChild(form);
form.submit();
//debugger;
$(form).remove();
}
//]]>
</script>
</body>
</html>
I have one scenario where I have to click on one link, that opens a new tab/window, As cypress does not support multiple tabs, I have found below workaround but its not working, It opens the new tab, but not able to switch the new tab and my test is failing with the error :
expected redirect to have been called at least once, but it was never called.
cy.
visit('https://qa.abc.com/xyz/documents?action_id=1');
cy
.window().then((win) => {
cy.spy(win, 'open').as('redirect');
});
cy
.get(':nth-child(1) > [style="width: 228px;"] > .text-ellipsis')
.click();
cy
.get('@redirect')
.should('be.called');
Note: Redirected url is dynamic and bind with the javascript, so not able to fetch the url from console also not able to remove the attribute from the link.
Here is the attached screenshot: enter image description here
Above is the sample HTML file, Which replicates the issue:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重定向非常不寻常。
您可以使用页面加载上的截距来阻止新选项卡。
另一个截距将验证重定向将变为纠正URL。
The redirect is quite unusual.
You can block the new tab by using an intercept on the page load.
Another intercept will verify the redirect goes to correct URL.
此方法还可以用于重定向到多个重定向,以及当您通过单击按钮而不是锚定或锚定为目标属性时将您重定向到其他页面时。
This approach will also work for redirecting to multiple redirection and when the you are redirected to other page by clicking on a button and not anchor, or when anchor does not have target property.