t.switchToWindow Method

Switches to a specific browser window. Chainable.

t.switchToWindow(window)

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.

Example:

fixture`TestController.switchToWindow`
    .page('https://devexpress.github.io/testcafe/');

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)
        .switchToWindow(documentation);
});

t.switchToWindow(predicate)

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

t.switchToWindow(filterFn);
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/'

Example:

fixture `TestController.switchToWindow`
    .page('https://www.example.com/');

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