Validate Location of element - centre of screen

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
brgibb
Posts: 39
Joined: Wed Jun 05, 2013 2:15 pm

Validate Location of element - centre of screen

Post by brgibb » Wed May 14, 2014 9:05 am

This may seem an obvious question but how do you tell if an element is correctly located (horizontally) in the middle of the browser?

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Validate Location of element - centre of screen

Post by odklizec » Wed May 14, 2014 12:19 pm

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");
		}        						
	}
Last edited by odklizec on Thu May 15, 2014 10:31 am, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

brgibb
Posts: 39
Joined: Wed Jun 05, 2013 2:15 pm

Re: Validate Location of element - centre of screen

Post by brgibb » Thu May 15, 2014 10:20 am

Most helpful thanks.... I just wondered if this was pre-built in to Ranorex already.

Evidently not.

Thanks again.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Validate Location of element - centre of screen

Post by odklizec » Thu May 15, 2014 10:31 am

You are welcome! ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration