在量角器中处理警报

问题描述

我有一个简单的案例,无法使用量角器处理警报。我只想在接受警报后导航到另一个应用程序页面。我已经尝试了以下失败的情况: (PS,如果我将失败的测试注释掉,则所有其他测试均按预期运行) (PS2,通过@HostListener('window:beforeunload')注释激活了警报)

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');

在这种情况下,浏览器导航到data:text/html,<html></html>,我收到类似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');

在这种情况下,我会收到类似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();

在这种情况下,浏览器导航到主页。但是,后续测试失败,并显示以下错误Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:60759

在测试结束时,我还尝试将ignoreSynchronization还原为false,但没有任何改变。

我通过类似npm run e2e -- --baseUrl=http://localhost:9001

的方式运行测试

解决方法

我认为您有问题列表

  1. ignoreSynchronization自量角器v5起无效,请改用browser.waitForAngularEnabled

  2. Re:UnexpectedAlertOpenError: unexpected alert open: {Alert text : }我今天突然也开始收到该错误,最近我将chrome和驱动程序更新为最新版本。所以我想这是相关的。到目前为止,还没有解决方案,但是有时在下一个chromedriver更新中解决了此类问题

  3. 您的#3应该是有效的代码,但但是由于我在我的#2中提到的原因,它无法正常工作。尝试检查一下是否可以更新chromedriver,如果有帮助,请告诉我。我也会更新我的

P.S。

只需检查我的代码即可,与chromedriver无关。解决方法是下面的功能

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