简体   繁体   中英

Handling alert in Protractor

I have a simple case of handling an alert with Protractor which I could not solve. I simply want to navigate to another application page after accepting the alert. I've tried the following cases which did not succeed: (PS, if I comment out the failing tests all other tests run as expected) (PS2, the alert is activate via a @HostListener('window:beforeunload') annotation)

1)

const alert = await browser.get('/homepage')
  .catch(function () {
    return browser.switchTo().alert();
});
await alert.accept();
await browser.get('/homepage');
const url = await browser.getCurrentUrl();
expect(url).endsWith('/homepage');
await browser.get('/homepage').catch(function () {
  return browser.switchTo().alert().then(function (alert) {
    alert.accept();
  });
});
await browser.get('/homepage');
const url = await browser.getCurrentUrl();
expect(url).endsWith('/homepage');

In this cases, the browser navigates to data:text/html,<html></html> and I get an error like: Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:52132

3)

await browser.get('/homepage');
await browser.wait(ExpectedConditions.alertIsPresent(), 10000);
await browser.switchTo().alert().accept();
await browser.get('/homepage');
const url = await browser.getCurrentUrl();
expect(url).endsWith('/homepage');

In this case I get an error like: UnexpectedAlertOpenError: unexpected alert open: {Alert text : }

4)

browser.ignoreSynchronization = true;
await browser.get('/homepage');
await browser.wait(ExpectedConditions.alertIsPresent(), 10000);
await browser.switchTo().alert().accept();

In this case, the browser navigates to the homepage. But, subsequent tests fail with the error: Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:60759

I also tried to revert the ignoreSynchronization to false at the end of the test but it did not make a difference.

I run the tests by something like: npm run e2e -- --baseUrl=http://localhost:9001

I think you have a list of problems

  1. ignoreSynchronization has no effect since protractor v5, use browser.waitForAngularEnabled instead

  2. Re: UnexpectedAlertOpenError: unexpected alert open: {Alert text : } All of sudden I started getting that error today too, and I recently updated my chrome and the driver to the latest version. So I suppose it's related. No solution so far though, but problems like this sometimes resolved within the next chromedriver update

  3. your #3 should be the working code, but because of what I mentioned in my #2 it doesn't work. Try to check it you can update your chromedriver, and please let me know if helps. I'll update mine too

PS

Just checked my code, not related to chromedriver. the solution is this function below

/**
* accept alert popup if present
*/
async acceptAlertPopup() {
    if (await ExpectedConditions.alertIsPresent()())
        await browser
            .switchTo()
            .alert()
            .accept();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM