ionic-framework - 使用 Protractor 等待 ionic 加载对话框

标签 ionic-framework protractor ionic angularjs-e2e e2e-testing

有类似的问题(链接如下),但没有一个能解决这个问题。我正在为 Ionic 项目编写 Protractor 测试。我需要在 ionic 加载对话框出现和消失时执行测试。

我已经使用应用程序的基本框架和需要进行的测试创建了一个 repo。解决这个问题你就解决了(我在下面描述了这个问题):https://github.com/TmanTman/StackoverflowQ .只需在 conf.js 中为您的系统调整 Chrome 的路径。

要模拟异步 Ionic Loading 对话框,我只需将其添加到空白 Ionic 项目中的 Controller 中:

$interval( function() {
        $ionicLoading.show({
            template: 'Async ionicLoading',
            duration: 5000
        });
      }, 5000 , 1);
    })

我需要让 Protractor 等待对话框出现,做一些测试,等待对话框消失,然后再做一些测试。我在测试文件中的最新尝试是:
it('should only test when ionicLoading appears', function() {
  browser.wait(function(){
    return element(by.css('.loading-container.visible.active')).isPresent();
  }, 10000);
  var ionicLoadingText = element(by.css('.loading-container.visible.active')).getText();
  expect(ionicLoadingText).toEqual('Async IonicLoading');
})



it('should only test once ionicLoading disappears', function() {
  browser.wait(function() {
    var deferred = protractor.promise.defer();
    var q = element(by.css('.loading-container.visible.active')).isPresent()
      q.then( function (isPresent) {
        deferred.fulfill(!isPresent);
      });
      return deferred.promise;
    });
  expect(1).toEqual(1);
})

我试图避免使用同步 sleep 功能,因为我的代码是高度异步的。我尝试了无数变体,但我无法让它发挥作用。我用于信息的链接包括:
  • Protractor blocking wait
  • Asynchronous Testing with Protractor's ControlFlow
  • Protractor wait for isElementPresent and click on waited element fails
  • 最佳答案

    问题有两个:

    1)据我所知,$ionicLoading 方法的持续时间属性是通过超时函数实现的。 Protractor 不适用于 $timeout。因此,可以使用 $interval 调用隐藏 $ionicLoading 对话框,而不是使用持续时间属性(改编问题中的代码):

    $interval( function() {
          $ionicLoading.show({
              template: 'Async IonicLoading'
          });
          $interval( function() {
            $ionicLoading.hide();
          }, 5000, 1)
      }, 5000 , 1);
    

    2)检测异步变化的代码如下:
    it('should only test when ionicLoading appears', function() {
        browser.wait(function() {
          var deferred = protractor.promise.defer();
          var q = element(by.css('.loading-container.visible.active')).isPresent()
          q.then( function (isPresent) {
              deferred.fulfill(isPresent);
          });
          return deferred.promise;
        }, 10000);
          var ionicLoadingText = element(by.css('.loading-container.visible.active')).getText();
          expect(ionicLoadingText).toEqual('Async IonicLoading');
        })
    
        it('should only test once ionicLoading disappears', function() {
          browser.wait(function() {
            var deferred = protractor.promise.defer();
            var q = element(by.css('.loading-container.visible.active')).isPresent()
              q.then( function (isPresent) {
                deferred.fulfill(!isPresent);
              });
              return deferred.promise;
            }, 10000);
          expect(1).toEqual(1);
        })
    

    然后两个测试都通过了。

    关于ionic-framework - 使用 Protractor 等待 ionic 加载对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30817451/

    相关文章:

    ionic-framework - 在 ionic 上更改 View 过渡动画样式

    angular - 无法从测试文件中找到模块@ionic/angular (ionic 4)

    selenium - 在 Internet Explorer 10 中使用 Selenium 进行基本身份验证

    javascript - 使用 phantomjs 和 Protractor 访问 iframe 内的 Web 元素

    angularjs - Grails-Ionic-AngularJS-在这个Env中工作是个好主意吗?

    ionic-framework - 如何在 Ionic Creator 中填充选择

    ios - Xcode 10:多个命令产生[CP] Embed Pods Framework

    android - Google Tag Manager 无法在 Ionic+Angular App 中工作

    android - 现有的 IONIC 1 项目迁移到 IBM Worklight (MobileFirst Platform 'MFP')

    javascript - 升级到 Protractor 4 后无法读取未定义的属性 'prototype'