如何使用Protractor测试的getSize()函数比较元素的宽度和高度

How to compare width and height of element with getSize() function with Protractor test?

本文关键字:比较 元素 高度 函数 Protractor 何使用 测试 getSize      更新时间:2023-09-26

早上好,亲爱的同事们。我有一个关于硒方法的问题。在我的例子中,我正在用量角器测试角度应用程序,我想将getSize函数的返回值和测试中的设置值进行比较。下面是代码-

var searchForm = element(by.id('search'));
 it('searchForm must have width: 400px and height: 400px', function(){
  //expect(browser.driver.manager().window().getSize()).toEqual(400, 400);
  searchForm.getSize();
  searchForm.width.toEqual(400);
  searchForm.height.toEqual(400);
});

请帮我解决麻烦。我希望你能帮助我。

Protractor getSize()函数返回一个promise,其中dimensions对象包含指定元素的width, height, hcode and class。等待promise返回,然后解析promise以获取元素的维度。以下是如何-

 it('searchForm must have width: 400px and height: 400px', function(){
    var searchForm = element(by.id('search'));
    searchForm.getSize().then(function(eleSize){
        console.log('element size: '+eleSize); //eleSize is the element's size object
        expect(eleSize.width).toEqual(400);
        expect(eleSize.height).toEqual(400);
    });
});

此外,不建议使用jasmine在自动化中编写规范it之外的任何内容。希望能有所帮助。

对于所有类型的元素属性都有一个有用的方法:

var elemHeight = element.getAttribute('height')

它返回元素属性的promise,因此您可以使用

elemHeight.then(function (height) {...

或者在您的情况下仅为expect(elemHeight).toEqual(400);