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

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

早上好亲爱的同事们。 我对 Selenium 方法有疑问。 在我的例子中,我正在使用量角器测试 angular 应用程序,我想将 getSize 函数的 returns 值与我的测试中的设置值进行比较。这是下面的代码 -

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);

});

请帮我解决问题。希望大家多多帮助。

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

var elemHeight = element.getAttribute('height')

它 returns 对元素属性的承诺,因此您可以使用

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

或者只是 expect(elemHeight).toEqual(400); 你的情况

Protractor getSize() 函数 returns 包含指定元素的 width, height, hcode and class 维度对象的承诺。等到 promise 被返回,然后 resolve 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 的内容。希望对你有帮助。