Bhagya Swamy
Bhagya Swamy

Reputation: 95

Intercepting a HTTP request in the middle of a test in Testcafe

I am writing a functional test using Testcafe. The test scenario is as below, There is a toggle button that activates/deactivates based on an API call

When I open my application, an API call is made that returns a value ON/OFF; based on that, the toggle switch is activated or deactivated.

I want to intercept that call when the user clicks on that toggle button again.

Long story short:

    await t.navigateTo(`${url}`);
    await t
        .click(myPage.toggleSwitch)
    .addRequestHooks(myPage.xyzAPI.respond([{ valueBar: "ON" }
    ]))
    .expect(myPage.toggleSwitch.checked)
    .eql(true);

});```

Upvotes: 0

Views: 410

Answers (1)

mlosev
mlosev

Reputation: 5227

I want to intercept that call when the user clicks on that toggle button again.

You need to add the target request hook before the click action. Also, before the click action, the actions chain should be broken.

    await t.navigateTo(`${url}`);

    await t.addRequestHooks(<hook that caught the API calls>);
    
    await t
        .click(myPage.toggleSwitch)
    .expect(myPage.toggleSwitch.checked).eql(true);

Upvotes: 1

Related Questions