为什么在量角器测试中实际执行步骤之前,等待计时器是否开始? Eventloop争吵

时间:2018-03-14 17:15:27

标签: javascript angular jasmine protractor automated-tests

tl; dr:当我运行我的测试用例时,执行的步骤似乎有效,但是在找不到尚未加载的元素的情况下,测试很快就会失败。看起来我在定位某些元素时等待测试启动后立即加载/启动,而不是在测试用例中实际执行行时。我认为这种情况正在发生,因为在“搜索”元素之前,页面几乎没有(正确地)加载,以验证页面已加载出来。我如何处理事件循环?

这可能是一个承诺问题,这很好,但我不明白发生了什么。如何实现我的下面的代码按预期工作?我正在使用Angular2 Web应用程序中的Jasmine2和Protractor 5.3.0创建自动化E2E测试用例。

describe('hardware sets', () => {
    it('TC3780:My_Test', async function() {
      const testLogger = new CustomLogger('TC3780');
      const PROJECT_ID = '65';

      // Test Setup
      browser.waitForAngularEnabled(false); // due to nature of angular project, the app never leaves zones, leaving a macrotask constantly running, thus protractor's niceness with angular is not working on our web app
      // Navigate via URL to planviewer page for PROJECT_ID
      await planListingPage.navigateTo(PROJECT_ID);  // go to listing page for particular project
      await planListingPage.clickIntoFirstRowPlans(); // go to first plan on listing page
      await planViewerPage.clickOnSetItem('100'); // click on item id 100 in the plan
    });
  });

planViewerPage.po.ts函数:

  clickOnSetItem(id: string) {
    element(by.id(id)).click();
    browser.wait(until.visibilityOf(element(by.css('app-side-bar .card .info-content'))), 30000); // verify element I want to verify is present and visible
    return expect(element(by.css('app-side-bar .card .info-content')).getText).toEqual(id);  //Verify values match, This line specifically is failing. 
  }

这是迄今为止的测试案例。我需要更多验证,但大部分时间都已完成。我切换到使用异步函数并等待而不是典型的(完成)和'.then(()=> {'语句链接,因为我不需要做一堆嵌套来让事情以正确的顺序执行。我来自一个java背景,所以这种疯狂的不得不迫使你按照你编写它们的顺序运行它有时对我来说有点多了。我已经指出像event loop上的Mozilla那样的信息,但是这一行让我更加困惑:

  

每当函数运行时,它都不能被抢占,并且会在任何其他代码之前完全运行   运行(并且可以修改函数操作的数据)。

因此,为什么测试用例似乎是预先评估的,并且在点击/加载任何页面之前定时器已经启动?我已经在这里实现了解决方案:{{ 3}}几乎逐字逐句,它仍然不等待。

奖金问题:有没有办法输出事件循环的预期事件执行和时间戳?也许那时我能理解它在做什么。

行为

1 个答案:

答案 0 :(得分:0)

函数中的代码是异步运行的

    clickOnSetItem(id: string) {
        element(by.id(id)).click().then(function(){
            return browser.wait(until.visibilityOf(element(by.css('app-side-bar .card .info-content'))), 30000); 
        }).then(function(){
           expect(element(by.css('app-side-bar .card .info-content')).getText).toEqual(id); 
         }).catch(function(err){
            console.log('Error: ' + err);
         })       
     }