Search code examples
testingautomated-testse2e-testingweb-testingtestcafe

Run command in the browser's console during the test in TestCafe


I would like to include this command during the test in TestCafe. I found that I can execute JavaScript code using client-functions and t.eval but I have no idea how to proceed with this.

Command that need to be executed automatically during the test:

document.querySelector(".Watchlist--form").submit()

How can I do this?


Solution

  • If it is impossible to use test actions to submit your form, you will need to create a Client Function to do it:

    import { Selector, ClientFunction } from 'testcafe';
    
    const submitAction = ClientFunction(() => {
        document.querySelector(".Watchlist--form").submit();
    });
    
    fixture `My fixture`
        .page `http://example.com`;
    
    test('My test', async t => {
        // Some actions and assertions before the submit action
        await t
            .click(Selector(...))
            ...
            .expect(...).ok();
    
        // Submit action
        await submitAction();
    
        // Some actions and assertions after the submit action
        await t
            .click(Selector(...))
            ...
            .expect(...).ok();
    });