Vai al contenuto principale

newWindow

Apre una nuova finestra o scheda nel browser (predefinita a una nuova finestra se non specificato). Questo comando è la funzione equivalente a window.open(). Questo comando non funziona in ambienti mobili.

Nota: Quando richiami questo comando passi automaticamente alla nuova finestra o scheda.

Utilizzo
browser.newWindow(url, { type, windowName, windowFeatures })
Parametri
NomeTipoDettagli
urlstringURL del sito web da aprire
options
opzionale
NewWindowOptionsopzioni del comando newWindow
options.type
opzionale
stringtipo di nuova finestra: 'tab' o 'window'
options.windowName
opzionale
Stringnome della nuova finestra
options.windowFeatures
opzionale
Stringcaratteristiche della finestra aperta (es. dimensione, posizione, barre di scorrimento, ecc.)
Esempi
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"
});
Restituisce
  • <Object> return: Un oggetto contenente l'handle della finestra e il tipo di nuova finestra {handle: string, type: string} handle - L'ID dell'handle della finestra della nuova scheda o finestra, type - Il tipo della nuova finestra, 'tab' o 'window'
Genera errore
  • Error: Se url non è valido, se il comando viene utilizzato su dispositivi mobili, o se type non è 'tab' o 'window'.

Welcome! How can I help?

WebdriverIO AI Copilot