Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing whether certain elements are visible or not

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

My DOM looks like:

<div class="span5 value-entry">
    <input type="text" ng-model="query.value" placeholder="Enter value" class="input-large" ng-show="genericInput(criteria.attribute)">
    <select ng-model="query.value" ng-options="entry for entry in filteredValue(criteria.attribute)" class="input-medium" ng-show="!genericInput(criteria.attribute)">
        <option value="">-- Select Value --</option>.
    </select>
</div>

Either the select is shown or the input box, but not both. I wish to check which element is visible (based on some other criteria), but I can't seem to figure out how to get the code working. I have written the following code:

expect(element('.value-entry input').is(':visible')).toBe(true);

But I get an error:

TypeError: Object #<Object> has no method 'is'

How do I check if the input is visible and the select is hidden at the same time (and vice versa)?

EDIT : I wish to add here that this is an end to end test

like image 245
callmekatootie Avatar asked Apr 15 '13 18:04

callmekatootie


People also ask

How do you know if an element is visible or not in Cypress?

Check visibility I'll check the visibility of my board with following code: it('has a board', () => { cy . visit('/'); cy . get('[data-cy=board-item]') .

How check element is displayed or not in jQuery?

How to Check an Element is Visible or not using jQuery. You can use .is(':visible') to check whether an element is visible in the layout or not. Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero.


3 Answers

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); 
like image 103
georgiosd Avatar answered Sep 20 '22 04:09

georgiosd


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

expect(element('#some-id:visible').count()).toBe(1); 
like image 27
Shadowedged Avatar answered Sep 21 '22 04:09

Shadowedged


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');
like image 22
Brent Morrow Avatar answered Sep 23 '22 04:09

Brent Morrow