Superfreak3
Superfreak3

Reputation: 47

Checking for a Pop then Clicking to Continue with TestCafe

After login, if the user is already logged on, a pop up is displayed. I want to use a do while loop. New to javascript and testcafe so this should probably be considered pseudo code...

do
{
    try
    {
        if (await this.continueBtn.exists)
        {
            await t.click(this.continueBtn);
        }
    }
    catch
    {
        break;
    }
}
while (await this.longinName.exists);

So, I'm not sure if the awaits are right or needed, but hopefully you get the idea of what I'm trying to do. User enters login information and clicks login button. If the user is already logged in, a pop-up will be displayed with the continue button. In the background, or under the pop-up the login name control is still visible/exists (maybe I should just use visible, tried, didn't work for me). So, while that is displayed I would like to keep a watch out for the pop-up and click continue to login in.

This 'code' will be in a page object class and after going through some tutorials, this. convention was used so not sure if that is correct either.

Thanks for any help in advance!

Upvotes: 0

Views: 286

Answers (2)

thenewjames
thenewjames

Reputation: 1164

You can try something like this:

await t
  .expect(continueBtn.exists)
  .ok('Dialog did not show up', { timeout: 5000 });
  .click(continueBtn)

This would be equivalent to if dialog shows up within 5 seconds, click on the continue button, else error out.

Upvotes: 1

Superfreak3
Superfreak3

Reputation: 47

That code actually works! If there is a better way, please let me know, but I had a typo in the .click event. In the actual test class code it was .clich.

Sorry about that. Hope the code snippet helps someone or receives corrections if there is a more efficient way of doing things.

Upvotes: 1

Related Questions