Richard77
Richard77

Reputation: 21621

how to close the current window/tab using cypress

I need to close the tab/window after each test so I can start the next from scratch

describe('theImplementationIamTesting', () => {

   after(() => {
        // CLOSE THE TAB AFTER THE TEST...
   });
});

I am looking a way to close the current tab after the test. I am not talking about closing a child tab/window. I am talking about the initial tab.

In selenium, it will be something like webdriver.close().

I cannot find a single place online, including the cypress website, where it said how to close the tab browser.

Thanks for helping

Upvotes: 4

Views: 9355

Answers (2)

SSH
SSH

Reputation: 29

The way I resolved this was to actually add an extra line at the end of each test which would click to navigate to a page from where the other tests could continue, say the 'home page'.

describe('Test Inline Text Entry Interactions', () => {
beforeEach('Log in as CypressEditor', () => {
    cy.MockLoginUser('cypressEditor');
    cy.visit('http://localhost:4200/homepage');
    })

it('should test 1st thing', () => {
    //Test something, then...
    cy.get('#logo-label').click(); //To navigate back to http://localhost:4200/homepage
});

it('should test the 2nd thing', () => {
    //Test something else...
    cy.get('#logo-label').click(); //To navigate back to http://localhost:4200/homepage
});

it('should test the 3rd thing', () => {
    //Test some more stuff, then...
    cy.get('#logo-label').click(); //this might not be necessary since it's the last one.
});

For me this ensured that each test could finish and continue with the next.

Upvotes: 0

Rosen Mihaylov
Rosen Mihaylov

Reputation: 1427

If you separate the cases in different test files it will close the whole browser and reopen it every time. This is the only way I had found so far and works for me very well to start every case from scratch since sometimes it continues to run unfinished API requests from the first case after the start of the second case.

The downside is you need to make the initial preparation of the system every time and it increases the runtime.

Upvotes: 3

Related Questions