angularjs - Protractor 异步/等待 UnhandledPromiseRejectionWarning : Unhandled promise rejection

标签 angularjs node.js asynchronous promise protractor

所以我正在使用 async/await( Link ) 迁移我的 Protractor 测试。

到目前为止,在我不断遇到这个问题之前,迁移是有点成功的。 因此,以下是我的测试步骤,然后是代码作为我正在处理的示例:

  • 导航到特定页面
  • 改变环境(从学校转到高中)
  • 获取高中房间列表
  • 再次改变环境(从学校转到中学)
  • 获取中学房间列表
  • 比较两个列表

上述步骤的相关代码:

Test.ts

describe("Update room list based on changing context values", () => {
  let page: HelperClass;
  let highSchoolRoomNameList: string[] = [];
  let middleSchoolRoomNameList: string[] = [];

  beforeAll(async () => {
    page = new HelperClass();
    await page.setBrowserSize();
    await page.commonContextTestSteps();
  });

  it("Changing school dropdown value", async () => {
    await page.waitForElement(page.schoolDropDown, 5000);
    await page.schoolDropDown.click();

    await page.waitForElement(page.dropDownList, 5000);
    await page.dropDownList.get(0).click();

    await browser
      .switchTo()
      .frame(element(by.tagName("iframe")).getWebElement());

    await page.roomList.each( item => {
      item.getAttribute("innerText").then(text => {
        highSchoolRoomNameList.push(text);
      });
    });

    await page.waitForElement(page.schoolDropDown, 5000);
    await page.schoolDropDown.click();

    await page.waitForElement(page.dropDownList, 5000);
    await page.dropDownList.get(1).click();

    await browser.switchTo().defaultContent();

    await browser
      .switchTo()
      .frame(element(by.tagName("iframe")).getWebElement());

    await page.roomList.each(item => {
      item.getAttribute("innerText").then(text => {
        middleSchoolRoomNameList.push(text);
      });
    });

    await protractor.promise.controlFlow().execute(() => {
      expect(highSchoolRoomNameList).not.toEqual(middleSchoolRoomNameList);
    });
  });
});

我一直收到这个错误:

(node:13672) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): StaleElementReferenceError: stale element reference: element is not attached to the page document (Session info: chrome=65.0.3325.181) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64) (node:13672) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:13672) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): StaleElementReferenceError: stale element reference: element is not attached to the page document (Session info: chrome=65.0.3325.181)

调试后发现在以下步骤失败

await browser
      .switchTo()
      .frame(element(by.tagName("iframe")).getWebElement());

    await page.roomList.each( item => {
      item.getAttribute("innerText").then(text => {
        highSchoolRoomNameList.push(text);
      });
    });

在我开始迁移到 async/await 之前,整个测试工作正常。这是我的 protractor.conf.js 文件:

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require("jasmine-spec-reporter");

exports.config = {

  SELENIUM_PROMISE_MANAGER: false,
  allScriptsTimeout: 11000,
  suites: {
    navigation_suite: "./nav-wrapper/e2e/nav-wrapper-navigationTests/**/*.ts"
  },
  specs: [
     "Test.ts"
  ],
  capabilities: {
    browserName: "chrome"
  },
  directConnect: true,
  baseUrl: "http://localhost:3000",

  framework: "jasmine",
  jasmineNodeOpts: {
    showColors: true,
    // Set a longer Jasmine default Timeout for debugging purposes
    defaultTimeoutInterval: 999999,
    print: function() {}
  },

  onPrepare() {
    require("ts-node").register({
      project: "./nav-wrapper/e2e/tsconfig.e2e.json"
    });
    jasmine
      .getEnv()
      .addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

任何关于如何重写这些方法的建议将不胜感激!

最佳答案

这不是对重写方法问题的直接回答,只是想帮助您获得比 Unhandled promise rejection 更好的错误(这并不是很有帮助)

在你使用 .then 的地方如果你把 .catch() 放在那个序列的末尾,你可以对错误做一些事情,比如记录它控制台。这是您无论如何都应该做的事情,但也将帮助您找出导致此处错误的原因。

下面是我的意思的一个例子:

asyncFuncA.then(AsyncFuncB)
      .then(AsyncFuncC)
      .then(AsyncFuncD).catch(e => {
          //Do something with the error here 
            console.error(e) 
            })

以及您的代码示例

await page.roomList.each((item) => {
        item
            .getAttribute('innerText')
            .then((text) => {
                highSchoolRoomNameList.push(text);
            })
            .catch((e) => {
                console.error(e);
            });
    });

关于angularjs - Protractor 异步/等待 UnhandledPromiseRejectionWarning : Unhandled promise rejection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120651/

相关文章:

node.js - 在每个源文件更改时自动运行 Mocha 测试

node.js - Nginx docker 作为本地的 nodejs 代理

html - 样式表上是否允许延迟或异步包括?

java - Android LRU 缓存未显示正确的位图?

ng-repeat 中的 AngularJS 必填字段验证

javascript - 解析高度属性时出现意外值 {{specs.height}}。索引.html

mysql - 如何在 Node js中销毁后连接mysql连接

javascript - Angular 1 - 切换单选按钮

angularjs - 如何在不重新加载 ui-view 的情况下更新 $stateParams? Angular ui路由器

android - 如何在不阻塞的情况下等待 Android UI 线程中异步填充的对象?