Selector.with Method

Overwrites the selector options.

Use with to derive selectors with different settings from the same base selector.

selector.with( options ) → Selector

with returns a new selector with a different set of options that includes options from the original selector and new options that overwrite them.

The sample below shows how to overwrite the selector options so that it waits for the selected element to become visible.

import { Selector } from 'testcafe';

const elementWithId = Selector(id => document.getElementById(id));


test('Visible button exists', async t => {
    const visibleElementWithId = elementWithId.with({
        visibilityCheck: true,
    const visibleButton        = await visibleElementWithId('submit-button');

    await t.expect(visibleButton).ok();



Type: Object

If you need to call a selector from a Node.js callback, assign the current test controller to the boundTestRun option.

For more information, see Calling Selectors from Node.js Callbacks.


Type: Object

Use this option to pass functions, variables or objects to selectors initialized with a function. The dependencies object’s properties are added to the function’s scope as variables.

Use dependencies instead of the function’s arguments if you do not need to pass new values every time you call the selector.

The following sample demonstrates a selector (element) that uses a server-side object passed as a dependency (customId) to obtain a page element.

import { Selector } from 'testcafe';

const persistentId = { key: 'id' };

const element = Selector(() => {
    return document.querySelector(`[${persistentId.key}="submit-button"]`);
}, {
    dependencies: { persistentId },

fixture`Initialize selector with function on client side with dependencies`

test('Click on submit', async t => {

When dependencies are passed to a client function, TypeScript cannot find them during compilation. This happens because dependencies are added to the function’s scope at runtime and can cause an error:

Error: TypeScript compilation failed.
Cannot find name 'dependencyFoo'.

Add the // @ts-ignore TypeScript comment to suppress this error.


Type: Number

The time (in milliseconds) allowed for an element returned by the selector to appear in the DOM before the test fails.

If the visibilityCheck option is enabled, the element then must become visible within the timeout.

Default value: the timeout specified in the API method or the --selector-timeout command line option.


Type: Boolean

true to additionally require the returned element to become visible within options.timeout.

This option is in effect when TestCafe waits for the selector to return a page element. This includes situations when

  • a property is obtained from the selector;

    const width = await Selector('.form-bottom', { visibilityCheck: true }).clientWidth;
  • a selector property is passed to an assertion as its actual value;

    await t.expect(Selector('.form-bottom', { visibilityCheck: true }).clientWidth).eql(850);
  • a selector is evaluated with the await keyword;

    const snapshot = await Selector('.form-bottom', { visibilityCheck: true })();

If the target element is not visible, the selector throws an exception.

Note that when a selector is passed to a test action as the target element’s identifier, the target element should be visible regardless of the visibilityCheck option.

Unlike filter functions, the visibilityCheck option does not change the selector’s matched set.

Consider the following page:

    <div>This div is visible</div>
    <div style="display:none">This div not is visible</div>
    <div style="visibility:hidden">This div not is visible either</div>

When you use a selector with visibilityCheck enabled to determine if an element exists or to count matching elements, TestCafe also takes into account invisible elements.

const count = await Selector('div', { visibilityCheck: true }).count;

// returns 3 since the visibilityCheck option
// does not affect the selector's matched set

To filter page elements by their visibility, use filterVisible and filterHidden methods.

Default value: false