Artem Krynychyi
Artem Krynychyi

Reputation: 25

I need close and open new browser in protractor

I have a simple test:

beforeEach(function () {
    lib.startApp(constants.ENVIRONMENT, browser);//get url
    loginPageLoc.loginAs(constants.ADMIN_LOGIN,constants.ADMIN_PASSWORD, 
browser);// log in
    browser.driver.sleep(5000); //wait
});

afterEach(function() {
    browser.restart(); //or browser.close()
});

it('Test1' , async() => {
lib.waitUntilClickable(adminManagersPage.ButtonManagers, browser);
    adminManagersPage.ButtonManagers.click();
expect(element(by.css('.common-popup')).isPresent()).toBe(false);
});
it('Test2' , async() => {
lib.waitUntilClickable(adminManagersPage.ButtonManagers, browser);
    adminManagersPage.ButtonManagers.click();
expect(element(by.css('.common-popup')).isPresent()).toBe(false);
});

The first iteration looks fine, but after .restart() I get:

Failed: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used. NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.

If I use .close() I get:

Failed: invalid session id

But if I change Test2 on simple console.log('case 1'); it looks fine.

Please explain what am I doing wrong?

Upvotes: 0

Views: 2466

Answers (2)

DublinDev
DublinDev

Reputation: 2348

You are declaring your functions as async but are not awaiting the any actions within. If you are not setting your SELENIUM_PROMISE_MANAGER to false in your config then you will see unexpected behavior throughout your test when declaring async functions. This async behavior is likely the cause of your issue so I would ensure SELENIUM_PROMISE_MANAGER:false and ensure your awaiting your actions in each function.

The reason your test passes if you change the second test to just be console.log() is because you are not interacting with the browser and therefore the selenium session ID is not required. Every time the browser is closed the selenium session id will be destroyed and a new one created when a new browser window is launched.

Also you should be aware that there is a config setting you can enable so you do not need to do it manually in your test.

Update: Adding code examples of what I have described:

Note: If you have a lot of code already developed it will take serious effort to convert your framework to Async/await syntax. For a quicker solution you could try removing the async keywords from your it blocks

Add these to your config

SELENIUM_PROMISE_MANAGER:false,
restartBrowserBetweenTests:true

and change you spec to

beforeEach(async function () {
    await lib.startApp(constants.ENVIRONMENT, browser);//get url
    await loginPageLoc.loginAs(constants.ADMIN_LOGIN, constants.ADMIN_PASSWORD,
        browser);// log in
    await browser.driver.sleep(5000); //wait
});

it('Test1', async () => {
    await lib.waitUntilClickable(adminManagersPage.ButtonManagers, browser);
    await adminManagersPage.ButtonManagers.click();
    expect(await element(by.css('.common-popup')).isPresent()).toBe(false);
});
it('Test2', async () => {
    await lib.waitUntilClickable(adminManagersPage.ButtonManagers, browser);
    await adminManagersPage.ButtonManagers.click();
    expect(await element(by.css('.common-popup')).isPresent()).toBe(false);
});

Upvotes: 2

Madhan Raj
Madhan Raj

Reputation: 1442

There is a relevant configuration option:

// If true, protractor will restart the browser between each test.

restartBrowserBetweenTests: true,

Add the above in your config to restart browser between your tests.

Hope it helps you.

Upvotes: 1

Related Questions