跳至主要內容

newWindow

在瀏覽器中開啟新的視窗或分頁(如果未指定,則預設為新視窗)。此命令等同於 window.open() 函式。此命令在行動環境中無效。

注意: 當呼叫此命令時,您會自動切換到新的視窗或分頁。

用法
browser.newWindow(url, { type, windowName, windowFeatures })
參數
名稱類型詳細資料
url字串要開啟的網站 URL
options
選填
NewWindowOptionsnewWindow 命令選項
options.type
選填
字串新視窗的類型:'tab' 或 'window'
options.windowName
選填
字串新視窗的名稱
options.windowFeatures
選填
字串開啟視窗的功能 (例如大小、位置、捲軸等)
範例
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://webdriverio.dev.org.tw', {
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://webdriverio.dev.org.tw', {
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"
});
拋出
  • 拋出:如果 url 無效、如果此命令在行動裝置上使用,或 type 不是 'tab' 或 'window'。

歡迎!我能如何幫您?

WebdriverIO AI Copilot