mocha.jswebdriver-iowdio-v5

How can I close the browser after a mocha test?


I am new to WebdriverIO and Mocha and I wrote 2 tests in order to check our web app.

I want to close the browser and sign-in again, after I run the first test. When I used browser.close(), I got an error the browser.close() is not a function and basically the second test runs right after the first test with the browser being open.

Is there a way to close to browser after a test in Mocha?

describe('Verify that a signed-in user can get to the page', () => {
  it('Title assertion ', () => {
    const viUrl = 'https://buyermanage.com/bmgt/lock?useMock=pre.json';
    signInPage.signIn('rich221', 'password', viUrl);
    assert.equal(preApp.getTitle(), 'Pre-App', 'Title Mismatch');
  });
});

describe("Verify that a not signed-in user can't get to the page and is redirected to login page", () => {
  it('Title assertion ', () => {
    const viUrl = 'https://buyermanage.com/bmgt/lock?useMock=pre.json';
    assert.equal(preApp.getTitle(), 'Pre-App', 'Title Mismatch');
  });
});

Solution

  • Try using browser.reloadSession():

    after(() => {
        // Start a new session for every 'locale' (wipe browser cache):
        browser.reloadSession();
    });
    

    In your particular example, you'd need to use it in the afterEach() hook, and wrap the describes, respectively the it statements (depending on your test-suite requirements) inside a parent describe block:

    describe('The world is your oister', () => {
    
      describe('Verify that a signed-in user can get to the page', () => {
        it('Title assertion ', () => {
          // bla bla bla
        });
      });
    
      describe("Verify that a not signed-in user can't get to the page and is redirected to login page", () => {
        it('Title assertion ', () => {
          // bla bla bla
        });
      });
    
      afterEach(() => {
        browser.reloadSession();
      });
    });