Page 1 of 1

Validate Location of element - centre of screen

Posted: Wed May 14, 2014 9:05 am
by brgibb
This may seem an obvious question but how do you tell if an element is correctly located (horizontally) in the middle of the browser?

Re: Validate Location of element - centre of screen

Posted: Wed May 14, 2014 12:19 pm
by odklizec
Hi,

Below code checks if the object is centered both in horizontal and vertical direction. Tested with this page: http://www.wpdfd.com/editorial/thebox/deadcentre2.html
Simply add this code as a new UserCode action and edit paths to your repo elements. "obj" variables refer to the rectangle you are going to check, "scr" variables refer to DOM client rectangle.

Hope this helps?
public void isObjectCentered()
	{
		int objX, objY, objW, objH, scrW, scrH, centerX, centerY = 0;
		// get element x/y pos
		objX = repo.DeadCentre.Rectangle.Element.ClientRectangle.X;
		objY = repo.DeadCentre.Rectangle.Element.ClientRectangle.Y;
		// get element width/height
		objW = repo.DeadCentre.Rectangle.Element.ClientRectangle.Width;
		objH = repo.DeadCentre.Rectangle.Element.ClientRectangle.Height;
		// get cleint rectangle width/height
		scrW = repo.DeadCentre.Self.Element.ClientRectangle.Width;
		scrH = repo.DeadCentre.Self.Element.ClientRectangle.Height;
		// get expected (centered) x/y pos of given element
 		centerX = scrW/2-objW/2;
		centerY = scrH/2-objH/2;
		// compare actual x/y position with expected client rectangle x/y position
		if (objX == centerX && objY == centerY)
		{
			Validate.IsFalse(false,"Element centered!");
		}
		else
		{
			Validate.IsFalse(true,"Element NOT centered! " + "Expected X pos:" + centerX.ToString() + "\n" + "Actual X pos:" + objX.ToString() + "\n"  + "Expected Y pos:" + centerY.ToString() + "\n" + "Actual Y pos:" + objY.ToString() + "\n");
		}        						
	}

Re: Validate Location of element - centre of screen

Posted: Thu May 15, 2014 10:20 am
by brgibb
Most helpful thanks.... I just wondered if this was pre-built in to Ranorex already.

Evidently not.

Thanks again.

Re: Validate Location of element - centre of screen

Posted: Thu May 15, 2014 10:31 am
by odklizec
You are welcome! ;)