Testing whether certain elements are visible or not

前端 未结 3 1867
春和景丽
春和景丽 2021-02-11 16:06

How do I find out if an element is visible or hidden in testacular (jasmine)?

My DOM looks like:

相关标签:
3条回答
  • 2021-02-11 16:27

    Visibility Test

    By default, display is set to inline for input, and inline-block for select. Therefore, you can determine if either are currently shown by testing for the existence of the default CSS property.

    expect(element('.value-entry input').css('display')).toBe('inline');
    expect(element('.value-entry select').css('display')).toBe('inline-block');
    

    To check if either are hidden, replace inline and inline-block with a check for none, which is how ngShow hides an element.

    expect(element('.value-entry input').css('display')).toBe('none');
    expect(element('.value-entry select').css('display')).toBe('none');
    
    0 讨论(0)
  • 2021-02-11 16:28

    You were close. However, here is how you should test visibility:

    expect(element('#some-id:visible').count()).toBe(1);
    
    0 讨论(0)
  • 2021-02-11 16:38

    This behavior has changed in Angular 1.2 because of ng-animate.

    The code for ngShow is:

    var ngShowDirective = ['$animate', function($animate) {
      return function(scope, element, attr) {
        scope.$watch(attr.ngShow, function ngShowWatchAction(value){
          $animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'ng-hide');
        });
      };
    }];
    

    Which means that it will add/remove class ng-hide to hide/show the element.

    Thus, as an example, the right way to test if the element is hidden would be:

    expect(element.find('.value-entry input').hasClass('ng-hide')).toBe(true);
    
    0 讨论(0)
提交回复
热议问题