I am running below test but it seems like it stuck at the resolving first promise and never been able to get out of it. The interesting thing is that if the same test has been run through the Chrome in UI mode (not in headless mode) then the test runs fine otherwise it fails. It always fails with Electron78 and also fails if test is kicked off with command line in Chrome as well.
I'm using Cypress V3.8.2, Node v12.10.0.
I have modified the original test and pasting a copy here:
it('QaClickAcademy Portal is reachable', async() => {
let userCountBefore= await getQuery('SELECT count(*) FROM users');
//extra stuff//
let userCountAfter= await getQuery('SELECT count(*) FROM users');
if( userCountBefore === userCountAfter){
assert.fail("count shouldn't be the same");
}
});
function getQuery(sqlQuery) {
return new Promise((resolve) => {
cy.sqlServer(sqlQuery).then((result) => {
resolve(result);
});
});
};
I would really appreciate any help on this. Thanks
Please do try to simplify the examples provided so we can have the bare minimum required to run.
I boiled down the above code to doing the following:
describe('tests', () => {
before('Clear cookies and visit login page.', () => {
cy.clearCookies();
cy.visit("http://www.qaclickacademy.com");
});
beforeEach('Preserve Session Before Each Testcase.', () => {
Cypress.Cookies.preserveOnce("__smToken");
});
it('QaClickAcademy Portal is reachable', () => {
cy.get('h2').invoke('text').should('eq', 'Featured CoursesWhat Our Students Say')
cy.get('h2').invoke('text').should('eq', 'Featured CoursesWhat Our Students Say')
})
})
Of course, this is not what your example is doing and I believe the problem stems from the last couple of assertions and how they are written.
let homePage=new PortalPages.Dashboard;
assert.equal("Featured CoursesWhat Our Students Say",await homePage.pageContent);
assert.equal("Featured CoursesWhat Our Students Say",await homePage.pageContent);
This essentially boils down to
assert.equal("blah blah", await new Cypress.Promise((resolve) => {
cy.get('h2').then(($el) => {
cy.wrap($el);
let message = $el.text();
resolve(message);
});
});)
I'm not exactly sure why this is failing except that there is a lot going on here outside of the normal async flow of Cypress that may be causing a timeout. I would suggest reading through our guides and trying the above syntax of cy.get('h2').invoke('text').should('eq', 'Featured CoursesWhat Our Students Say') to assert against the value instead of using await and Promises.
See also this thread about await syntax https://github.com/cypress-io/cypress/issues/1417
Thank you Jennifer Shehane but I have tried that and it didn't work for me. I have simplified the test so it would be easier to dig into it. Thanks again!
@aartisoni1 I don't have access to the cy.sqlServer command so cannot run the code provided.
Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.
Please comment in this issue with a reproducible example and we will reopen the issue.
Please try running below :
it('reset password', async() => {
cy.get('#Email').clear().type("[email protected]");
cy.get('#ResetPasswordButton').click();
//check if there's proper message in toast message and toast title
const toastTitle=await getText(cy.get('.toast-title'));
assert.equal("sent email",toastTitle);
const toastMessage = await getText(cy.get('.toast-message'));
assert.equal("email has been sent",toastMessage);
});
function getText(htmlElement){
return new Cypress.Promise((resolve) => {
htmlElement.then(($el)=> {
cy.wrap($el);
let message=$el.text();
return resolve(message);
});
});
}
@aartisoni1 Please provide a completely running example in a single comment. There is no url specified in the test above, so this test code is not runnable. I cannot piece together several comments to get a reproducible example because I am not familiar with your application.
Please provide something that I can directly copy paste into a single test and run that shows the error or a repo that can be cloned and run.
@jennifer-shehane I am running into the exact same problem. In the Cypress UI Tool (node_modules/.bin/cypress open) the test runs perfectly fine. But it fails in the command line headed and headless because it stucks after or in the first Promise.
describe('github example', () => {
it('github example', async () => {
cy.visit('https://www.cypress.io/blog/');
const link1 = await getNavElement(0);
expect(link1).to.equal('Features');
const link2 = await getNavElement(1);
expect(link2).to.equal('How it works');
const link3 = await getNavElement(2);
expect(link3).to.equal('Dashboard');
const link4 = await getNavElement(3);
expect(link4).to.equal('Pricing');
});
});
function getNavElement(i) {
return new Promise((resolve) => {
cy.get('#left-nav li')
.eq(i)
.then((result) => {
resolve(result.text());
});
});
}
Cypress 3.8.3
Chrome 79.0
Hi Jennifer, Please try running following test and you will be able to replicate the issue. It will pass using Chrome79 but will always fail in CI and Electron 78.
it('example test', async () => {
cy.visit('http://www.qaclickacademy.com/')
const content = await getText(cy.get('#content').find('h2'))
assert.equal('Featured Courses', content)
const reviews = await getText(cy.get('#about-reviews').find('h2'))
assert.equal('What Our Students Say', reviews)
})
function getText(htmlElement) {
return new Cypress.Promise((resolve) => {
htmlElement.then(($el) => {
cy.wrap($el)
let message = $el.text()
return resolve(message)
})
})
}
Can you please re open the issue now that I have provided code to replicate?
@aartisoni1 I don't understand why these await and Promise definitions are necessary. Cypress commands are promises and everything is automatically awaited. Again, I would refer you to read the entirety of the introduction to cypress.
Why not write the code you have like below? Are the Promise definitions inherently necessary for your tests?
Cypress.Commands.add('getText', { prevSubject: 'element' }, (subject, options) => {
return cy.wrap(subject).invoke('text')
})
it('example test', async () => {
cy.visit('http://www.qaclickacademy.com/')
cy.get('#content').find('h2').getText().should('eq', 'Featured Courses')
cy.get('#about-reviews').find('h2').getText().should('eq', 'What Our Students Say')
})
If you need reusable code, look into using Custom Commands https://on.cypress.io/custom-commands
I'm not sure why this runs differently in Electron but there's probably some variability in the timing of how it runs.
Please check out our community chat for further question, it can be helpful for debugging or answering questions on how to use Cypress.
@rehne Same advice as above. Work within Cypress' recommended use. It will make working with your tests a lot easier.
Rewrite of your code using https://on.cypress.io/custom-commands and general advice from https://on.cypress.io/introduction-to-cypress
Cypress.Commands.add('getNavElement', (subject, options) => {
return cy.get('#left-nav li').eq(subject).invoke('text')
})
it('github example', () => {
cy.visit('https://www.cypress.io/blog/')
cy.getNavElement(0).should('eq', 'Features')
cy.getNavElement(1).should('eq', 'How it works')
cy.getNavElement(2).should('eq', 'Dashboard')
cy.getNavElement(3).should('eq', 'Pricing')
})
Please check out our community chat for further question, it can be helpful for debugging or answering questions on how to use Cypress.
I am having kind of the same problem.
Most helpful comment
@jennifer-shehane I am running into the exact same problem. In the Cypress UI Tool (node_modules/.bin/cypress open) the test runs perfectly fine. But it fails in the command line headed and headless because it stucks after or in the first Promise.
Cypress 3.8.3
Chrome 79.0