新窗口
在浏览器中打开新窗口或新标签页(如果未指定,默认为新窗口)。
此命令相当于 window.open()
函数。此命令在移动环境中不起作用。
注意: 调用此命令时,你会自动切换到新的窗口或标签页。
用法
browser.newWindow(url, { type, windowName, windowFeatures })
参数
名称 | 类型 | 详情 |
---|---|---|
url | string | 要打开的网站 URL |
options 可选 | NewWindowOptions | newWindow 命令选项 |
options.type 可选 | string | 新窗口类型:'tab' 或 'window' |
options.windowName 可选 | String | 新窗口的名称 |
options.windowFeatures 可选 | String | 打开窗口的特性(例如大小、位置、滚动条等) |
示例
newWindowSync.js
it('should open a new window', async () => {
await browser.url('https://google.com')
console.log(await browser.getTitle()) // outputs: "Google"
const result = await browser.newWindow('https://webdriver.io', {
windowName: 'WebdriverIO window',
windowFeature: 'width=420,height=230,resizable,scrollbars=yes,status=1',
})
console.log(await browser.getTitle()) // outputs: "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
console.log(result.type) // outputs: "window"
const handles = await browser.getWindowHandles()
await browser.switchToWindow(handles[1])
await browser.closeWindow()
await browser.switchToWindow(handles[0])
console.log(await browser.getTitle()) // outputs: "Google"
});
newTabSync.js
it('should open a new tab', async () => {
await browser.url('https://google.com')
console.log(await browser.getTitle()) // outputs: "Google"
await browser.newWindow('https://webdriver.io', {
type:'tab',
windowName: 'WebdriverIO window',
windowFeature: 'width=420,height=230,resizable,scrollbars=yes,status=1',
})
console.log(await browser.getTitle()) // outputs: "WebdriverIO · Next-gen browser and mobile automation test framework for Node.js"
console.log(result.type) // outputs: "tab"
const handles = await browser.getWindowHandles()
await browser.switchToWindow(handles[1])
await browser.closeWindow()
await browser.switchToWindow(handles[0])
console.log(await browser.getTitle()) // outputs: "Google"
});
返回值
- <Object>
return
: 包含窗口句柄和新窗口类型的对象{handle: string, type: string}
handle - 新标签页或窗口的窗口句柄 ID,type - 新窗口的类型,'tab' 或 'window'
抛出
- Error: 如果
url
无效,命令在移动设备上使用,或type
不是 'tab' 或 'window'。