我正在为我正在开发的网站进行测试自动化。我正在使用 Windows powershell 创建脚本来执行此操作。我的问题是我需要单击打开另一个窗口的链接,我需要以某种方式引用该窗口。
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://localhost:4611")
$ie.visible = $true
$doc = $ie.document
$link = $doc.getElementByID('link')
这就是我获得浏览器和链接的引用的地方。然后我点击链接:
$link.click()
这会打开一个新窗口,其中包含我需要测试的内容。我如何获得对这个新窗口的引用?从技术上讲,它不是第一个窗口的子窗口。
我尝试将链接的点击设置为这样的引用,但它不起作用:
$test = new-object -com "InternetExplorer.Application"
$test = $link.click()
UPDATE:这是 JavaScript 函数 openwindow,它被调用来打开窗口
function openwindow(url, name) {
if (typeof openwindow.winrefs == 'undefined') {
openwindow.winrefs = {};
}
if (typeof openwindow.winrefs[name] == 'undefined' || openwindow.winrefs[name].closed) {
openwindow.winrefs[name] = window.open(url, name, 'scrollbars=yes,menubar=no,height=515,width=700,resizable=no,toolbar=no,status=no');
} else {
openwindow.winrefs[name].focus();
};
};
该函数在如下代码行中被调用
column.For(i => "<a href='" + link + i.id + "?status=new' target='pat" + i.id + "'id'enc' onclick='openwindow(this.href,this.target);return false'>
最终更新:我最终的做法略有不同。我创建了一个新的 Internet Explorer 对象,并从链接中获取 href,设置所有选项,然后像 javascript 一样使用 powershell 导航到窗口。
$ie2 = new-object -com "InternetExplorer"
$ie2.visible = $true
$ie2.menubar = $false
$ie2.height = 515
$ie2.width = 700
$ie2.resizable = $false
$link = $doc.getelementbyid('link')
$url = $link.href
$ie2.navigate($url)
我非常感谢@sunliffe 帮助我解决这个问题。