1

I'm getting trouble with Cypress asynchronous mechanism. I have a custom command that is placed in this file

class HeaderPage {

    shopLink = 'a[href="/angularpractice/shop"]'
    homeLink = ''

    navigateToShopPage() {
        cy.get(this.shopLink).click()
    }

    sshToServer() {
        setTimeout(() => {
            console.log('Connecting')
        }, 5000)
        console.log('Connected')
    }
}

export default HeaderPage

The function sshToServer is simulated to pause 5000ms. So I want Cypress remaining test will be hold and wait for this function completed. How can I do that? Thanks in advance.

import HeaderPage from "../support/pageObjects/HeaderPage"

describe('Vefiry Alert and Confirm box', () => {

    const headerPage = new HeaderPage()

    it('Access home page', () => {
        cy.visit(Cypress.env('url') + 'AutomationPractice/')
    });

    it('SSH to server', () => {
        headerPage.sshToServer()
    });

    it('Verify content of Alert', () => {
        cy.get('#alertbtn').click()
        cy.on('window:alert', (alert) => {
            expect(alert).to.equal('Hello , share this practice page and share your knowledge')
        })
    });
konekoya
  • 1,995
  • 2
  • 18
  • 30
Hoang
  • 35
  • 3
  • What is the error that you're getting ? – Alapan Das Nov 02 '20 at 06:06
  • Could you provide a runnable example? I tried to repro this with your code, but there are some issues with your HeaderPage class – konekoya Nov 02 '20 at 09:32
  • @AlapanDas I didn't get the error. The problem is that Cypress complete its running, then after 5 seconds, I got the message from step 2 "SSH to server". It means Cypress didn't wait my function sshToServer() complete, it went through all remaining steps. – Hoang Nov 02 '20 at 09:40
  • 1
    @konekoya you can download my codes here, the test script names "alert_confirm.js" https://drive.google.com/file/d/1ZpeMD9jqP-89k5biolOGPvrueC0jXvbK/view?usp=sharing – Hoang Nov 02 '20 at 09:48

1 Answers1

0

You can issue a new cy.wrap command with a value null and calling your async function in the .then function. Cypress will resolve that async function automatically and then move on to the next test.

First, you should convert your sshToServer method to an async(promise) function:

sshToServer() {
  console.log('Connecting');
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('Connected');
      resolve();
    }, 5000);
  });
}

Then, in your spec:

it('SSH to server', { defaultCommandTimeout: 5000 }, () => {
  cy.wrap(null).then(() => headerPage.sshToServer());
});

Note that I have also used a bigger for the spec defaultCommandTimeout since the default timeout is 4 seconds which is shorter than your sshToServer method and would cause the test to fail if not using a bigger timeout.

konekoya
  • 1,995
  • 2
  • 18
  • 30
  • Sorry, it didn't work. The result is same. All of tests have been completed, then after 5s, the log is printed in console browser :( – Hoang Nov 02 '20 at 10:13
  • Yep, sorry. I didn't see the second log. I have updated the answer. And basically, the method should be a promise-based API and so Cypress can be aware of that – konekoya Nov 02 '20 at 10:16
  • 1
    Thank you so much, it worked :) But I think the statement "console.log('Connected')" is not correct, it will be printed first. – Hoang Nov 02 '20 at 10:51
  • Good catch. Updated – konekoya Nov 03 '20 at 01:54