5

Good morning dear colleagues. I have a question about Selenium methods. In my case I'm testing angular application with protractor and I want to compare returns value from getSize function with set values in the my test. Here is code below -

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

});

please help me to solve trouble. I hope you will help me.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
Maksim
  • 155
  • 1
  • 2
  • 17

2 Answers2

12

Protractor getSize() function returns a promise with the dimensions object containing width, height, hcode and class of the element specified. Wait until the promise is returned and then resolve the promise to get the dimensions of the element. Here's how -

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

Also it is not recommended to write anything outside a spec it in automation using jasmine. Hope it helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • Your answer is helped me enough, but I always recieved this massage: Expected Object({ width: 401, hCode: 1642496, class: 'org.openqa.selenium.Dimension', height: 480 }) to equal '{401, 480}'. What is it: hCode: 1642496 ? – Maksim Oct 06 '15 at 09:01
  • @Maksim did you try the code that i updated above? That error is coming out from the expect statement that you have used to check if the `eleSize` is equal to `{401, 480}`. Let me know if the above code doesn't work. – giri-sh Oct 06 '15 at 10:06
0

There's a usefull method for all kind of element attributes:

var elemHeight = element.getAttribute('height')

it returns a promise for the element's attribute, so you can use

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

or just expect(elemHeight).toEqual(400); in your case

vrachlin
  • 817
  • 5
  • 15
  • What is it: hCode: 1642496, class: 'org.openqa.selenium.Dimension' ? – Maksim Oct 06 '15 at 09:27
  • @Maksim I would guess `hCode` is actually short for "hashCode", and not relevant really to JS usages of WebDriver. It's my (not very) educated guess that its a carry-over from some other language that WebDriver is written in or something. – Zach Lysobey Dec 06 '15 at 00:52