Validate Location of element - centre of screen
Validate Location of element - centre of screen
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
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?
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 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
Re: Validate Location of element - centre of screen
Most helpful thanks.... I just wondered if this was pre-built in to Ranorex already.
Evidently not.
Thanks again.
Evidently not.
Thanks again.
Re: Validate Location of element - centre of screen
You are welcome!
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
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