Thunfische
Thunfische

Reputation: 1157

Protractor test fails for redirected urls that are written with target='_blank_'

fit('try google',async ()=>{    
        let logo = $('.logo')
        await logo.click();
        expect(await browser.driver.getCurrentUrl()).toMatch('https://www.google.com/');

    });

<div class="logo">
            <a href="https://www.google.com/" target="_blank">google</a>
        </div>

Error

Expected 'http://localhost/thunfisch/home.aspx' to match 'https://www.google.com/'.

It works when target is not _blank. It looks pretty straight forward but i could not manage to get it worked. Thanks

Upvotes: 1

Views: 102

Answers (1)

Kacper
Kacper

Reputation: 1199

target="_blank" opens the link in a new tab.

You may still be on the first tab, but the test looks for google.com.

You should switch to last tab before assertion. See the documentation: http://www.protractortest.org/#/api?view=webdriver.WebDriver.prototype.switchTo

You can also take a look on example usage here: https://stackoverflow.com/a/32515194/6331748

Edit: In non async way it looks like:

return.browser.getAllWindowHandles().then((availableTabs) => {
    return browser.switchTo().window(availableTabs[availableTabs.length - 1]);
});

voilà!

Upvotes: 3

Related Questions