t.switchToWindow Method

Switches to a specific browser window. Chainable.


Activates the window that corresponds to the window descriptor.

t.switchToWindow(windowDescriptor) → this | Promise<any>
Parameter Type Description
windowDescriptor Object Descriptor object obtained from an open browser window.



test('Switch to a different window', async t => {
    const homepage      = await t.getCurrentWindow();
    const documentation = await t.openWindow('https://devexpress.github.io/testcafe/documentation');

    await t.switchToWindow(homepage)


Activates the first window that matches the criteria passed to the filterFn function.

Parameter Type Description
filterFn Function The predicate used to select windows.

The filterFn predicate accepts the following parameters:

Parameter Type Description
url Object The URL
title String The window title

The url object has the same structure as its Node.js counterpart. It can contain the following properties:

Property Description Example
protocol Protocol used 'https:'
host Hostname 'www.devexpress.com'
port Port number '80'
pathname Location relative to the host’s root folder '/products/testcafestudio/'
href The complete URL 'https://www.devexpress.com/products/testcafestudio/'


fixture `TestController.switchToWindow`

test('Switching between different windows', async t => {
    await t.openWindow('https://devexpress.github.io/testcafe')
        .switchToWindow(w => w.url.host === 'testcafe.io')
        .switchToWindow(w => w.title === 'Example Domain' && w.url.host === 'www.example.com');