t.selectText Method

Selects text from input elements. Chainable.

t.selectText(selector [, startPos] [, endPos] [, options]) → this | Promise<any>
Parameter Type Description Default
selector Function | String | Selector | Snapshot | Promise The target page element (See Select Target Elements).
startPos (optional) Number The start position of the selection. A zero-based integer. 0
endPos (optional) Number The end position of the selection. A zero-based integer. Length of the visible text content.
options (optional) Object Additional action options (see Options).

Note

You can use t.selectText for <textarea> and contentEditable elements as well. However, the t.selectTextAreaContent and t.selectEditableContent actions allow you to specify the selection range in a way that is more relevant for these elements.

The following example demonstrates text selection in an input element.

import { ClientFunction, Selector } from 'testcafe';

fixture`TestController.selectText`
    .page`https://devexpress.github.io/testcafe/example/`;

const developerNameInput = Selector('#developer-name');

const getElementSelectionStart = ClientFunction(selector => selector().selectionStart);
const getElementSelectionEnd   = ClientFunction(selector => selector().selectionEnd);

test('Select text within input', async t => {
    await t
        .typeText(developerNameInput, 'Test Cafe', { caretPos: 0 })
        .selectText(developerNameInput, 7, 1);

    await t
        .expect(await getElementSelectionStart(developerNameInput)).eql(1)
        .expect(await getElementSelectionEnd(developerNameInput)).eql(7);
});

Note

If the startPos value is greater than the endPos value, the action will perform a backward selection.

You can also select <textarea> content with the t.selectTextareaContent method and editable content with the t.selectEditableContent method.

Select Target Elements

Use the selector parameter to identify the target of a DOM event.

You can pass any of the following objects as a selector:

  • A CSS selector string.

    test('Click submit', async t => {
    
        // Click will be performed on the first element
        // that matches the CSS selector.
        await t.click('#submit-button');
    });
    
  • A selector.

    import { Selector } from 'testcafe';
    
    fixture`Parameter as Selector`
        .page`https://devexpress.github.io/testcafe/example/`;
    
    const lastCheckbox = Selector('fieldset p:last-child [type="checkbox"]');
    
    test('Click last checkbox', async t => {
    
        // Click will be performed on the element selected by
        // the 'getLastItem' selector.
        await t.click(lastCheckbox);
    });
    
  • A client-side function that returns a DOM element.

    test('Click first child of the body', async t => {
    
        // Click will be performed on the element returned by the function,
        // which is the third child of the document's body.
        await t.click(() => document.body.children[0]);
    });
    
  • A DOM node snapshot.

    import { Selector } from 'testcafe';
    
    fixture`Parameter as DOM node snapshot`
        .page`https://devexpress.github.io/testcafe/example/`;
    
    test('Click preferred interface', async t => {
        const preferredInterface = await Selector('#preferred-interface');
    
        // Click will be performed on the element whose snapshot
        // is specified. This is an element with the '#preferred-interface' ID.
        await t.click(preferredInterface);
    });
    
  • A Promise returned by a selector.

    import { Selector } from 'testcafe';
    
    const submitButton = Selector('#submit-button');
    
    fixture`Parameter as Promise by Selector`
        .page`https://devexpress.github.io/testcafe/example/`;
    
    test('Click submit', async t => {
        // Click will be performed on the element specified by the selector
        // as soon as the promise is resolved.
        await t.click(submitButton());
    });
    

TestCafe waits for the target element to become visible before it executes an action. If this does not happen within the selector timeout, the test fails.

TestCafe cannot interact with background elements. If a different element overlaps the action target, TestCafe waits for this element to disappear. If this does not happen within the selector timeout, TestCafe performs the action with the element on top of the original target. Learn more about stacking and z-index on MDN.

Note

The upload action is an exception to this rule — it does not check the visibility of the target input.

Options

Basic action options provide additional parameters for the t.pressKey, t.selectText, t.selectTextAreaContent and t.selectEditableContent actions.

export interface BasicOption {
    speed: number;
}
Parameter Type Description Default
speed Number The speed of action emulation. Number between 1 (the maximum speed) and 0.01 (the minimum speed). Overrides all other speed settings (CLI, Runner, and test code) for the duration of the action. 1
confidential Boolean Replace the action payload with asterisks in TestCafe Dashboard reports. true when the action target is a password type input; false otherwise.

Example

import { Selector } from 'testcafe';

const nameInput = Selector('#developer-name');

fixture`Action speed`
    .page`https://devexpress.github.io/testcafe/example/`;

test('Type name', async t => {
    await t
        .typeText(nameInput, 'Peter')
        .typeText(nameInput, ' Parker', { speed: 0.1 });
});