如何使用量角器检查元素是否可见?

时间:2021-03-16 08:41:11

I am using the Protractor software for my end to end tests. Some elements are being set with ng-show.

我正在使用Protractor软件进行端到端测试。一些元素正在使用ng-show设置。

Can someone tell me how I can check if these elements are visible or not with Protractor ?

有人能告诉我如何用Protractor检查这些元素是否可见?

4 个解决方案

#1


14  

Assuming that your element has the ID "foo", you could do, for example

假设您的元素具有ID“foo”,例如,您可以这样做

expect($('#foo').isDisplayed()).toBe(true); // or false to test that it's hidden

or

要么

expect(element(by.id('foo')).isDisplayed()).toBe(true); 

#2


2  

I've found that isDisplayed() returns a promise, and in the .then, you are passed the boolean. So looks more like this:

我发现isDisplayed()返回一个promise,然后在.then中,你传递了boolean。所以看起来更像这样:

$('#foo').isDisplayed().then(function(isDisplaying) {
    expect(isDisplaying).toBe(true);
  });

#3


1  

expect knows to deal with promise so the following works.

期望知道处理承诺所以以下工作。

expect($('#foo').isDisplayed()).toBe(true);

#4


1  

Assuming you have many elements by the same id/class and you want to assert on the count of number of visible elements

假设您有相同ID /类的许多元素,并且您想要断言可见元素的数量

var elements = element.all(by.id('foo'))
               .filter(function(el){
                    return el.isDisplayed();
                });

expect(elements.count()).toEqual(2);

#1


14  

Assuming that your element has the ID "foo", you could do, for example

假设您的元素具有ID“foo”,例如,您可以这样做

expect($('#foo').isDisplayed()).toBe(true); // or false to test that it's hidden

or

要么

expect(element(by.id('foo')).isDisplayed()).toBe(true); 

#2


2  

I've found that isDisplayed() returns a promise, and in the .then, you are passed the boolean. So looks more like this:

我发现isDisplayed()返回一个promise,然后在.then中,你传递了boolean。所以看起来更像这样:

$('#foo').isDisplayed().then(function(isDisplaying) {
    expect(isDisplaying).toBe(true);
  });

#3


1  

expect knows to deal with promise so the following works.

期望知道处理承诺所以以下工作。

expect($('#foo').isDisplayed()).toBe(true);

#4


1  

Assuming you have many elements by the same id/class and you want to assert on the count of number of visible elements

假设您有相同ID /类的许多元素,并且您想要断言可见元素的数量

var elements = element.all(by.id('foo'))
               .filter(function(el){
                    return el.isDisplayed();
                });

expect(elements.count()).toEqual(2);