Skip to content

Commit

Permalink
simplify by not overwriting cy.get
Browse files Browse the repository at this point in the history
  • Loading branch information
lukpsaxo committed Aug 26, 2020
1 parent 8ff9974 commit ae67c76
Showing 1 changed file with 13 additions and 41 deletions.
54 changes: 13 additions & 41 deletions cypress/integration/spec.js
@@ -1,41 +1,13 @@
// overwrite cy.get to call waitForTimers when its called directly
// e.g.
// cy.get().get().click(); // calls waitForTimers once
// this ensures that all timers have run and dom updates are complete before getting elements
// we don't use cypress overwrite because its difficult with cypress to overwrite child commands
// and logging wise you end up with get being prefixed with - or not being visible.
// The below is simpler.

const existingCyGet = cy.get;

cy.get = function (selectorOrAlias, options) {
if (this !== cy) {
return existingCyGet.call(this, selectorOrAlias, options);
}

function get(selectorOrAlias, options) {
cy.window().then(
(win) =>
new Promise((resolve) => {
// we occasionally get timeouts here, probably caused by the complexities of mocking some time
// and not other time.
// The page itself can't do a timeout since it cannot get a reliable timer.
// If we use cypress timeout then the test will fail.
// so we timeout here and move on
let isTimedOut = false;
let timeoutTimer;
win.getPromiseForTimersToComplete().then(() => {
if (!isTimedOut) {
clearTimeout(timeoutTimer);
resolve();
}
});
timeoutTimer = setTimeout(() => {
isTimedOut = true;
resolve();
}, 7000);
}),
);
return existingCyGet(selectorOrAlias, options);
return cy.get(selectorOrAlias, options);
}

it('works', {
Expand All @@ -50,17 +22,17 @@ it('works', {
// if you set this to false `yarn test` will not hang
const breakCypress = true;
if (breakCypress) {
cy.get('[data-test-id="instrument-search-textbox"]')
cy.get('[data-test-id="navbar"]').contains('Trading').trigger('tap');
cy.get('[data-id="2"]').should('be.visible').click();
cy.get('[data-test-id="tst-btn-cardcarousel-moreactions"]').should('be.visible').click();
cy.get('[data-test-id="tst-action-menu"] .tst-chart').click();
get('[data-test-id="instrument-search-textbox"]')
get('[data-test-id="navbar"]').contains('Trading').trigger('tap');
get('[data-id="2"]').should('be.visible').click();
get('[data-test-id="tst-btn-cardcarousel-moreactions"]').should('be.visible').click();
get('[data-test-id="tst-action-menu"] .tst-chart').click();
}

cy.get('[data-test-id="open-positions"]').trigger('tap');
cy.get('[data-test-id="watchlist"]').trigger('tap', { force: true });
cy.get('[data-id="2"]').should('be.visible').click();
cy.get('[data-test-id="tst-btn-cardcarousel-moreactions"]').should('be.visible').click();
cy.get('[data-test-id="tst-action-menu"] .tst-tradingconditions').click();
cy.get('[data-test-id="saxoselect-portfolios"]').should('be.visible');
get('[data-test-id="open-positions"]').trigger('tap');
get('[data-test-id="watchlist"]').trigger('tap', { force: true });
get('[data-id="2"]').should('be.visible').click();
get('[data-test-id="tst-btn-cardcarousel-moreactions"]').should('be.visible').click();
get('[data-test-id="tst-action-menu"] .tst-tradingconditions').click();
get('[data-test-id="saxoselect-portfolios"]').should('be.visible');
})

0 comments on commit ae67c76

Please sign in to comment.