This is a migrated thread and some comments may be shown as answers.

Retrieving co-ordinates using GetRectangle().X and GetRectangle().Y

7 Answers 126 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
PP
Top achievements
Rank 1
PP asked on 17 May 2013, 05:46 AM
Hi,

I am trying to find an element using Find.ByContent("abc") and then trying to retrieve the co-ordinates of this element using GetRectangle method so that i can perform a click using the retrieved X and Y co-ordinates. But am facing errors in the same.

My code looks like this:

            Element MyElement = Find.ByContent("p:Complete", FindContentType.TextContent);
           
            int x = MyElement.GetRectangle().X;
            int y = MyElement.GetRectangle().Y;
            Log.WriteLine(x.ToString());
            Log.WriteLine(y.ToString());

Is there a way I can load DOM using code? And is this the correct way to find out the co-ordinates of the element?
Can somebody please help me with the same?

Thanks in advance.
Prerna

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 17 May 2013, 07:08 AM
Hi Prerna,

There's no need to get the element's coordinates in order to click on it. All you need to do is to use the MouseClick method of the HtmlControl class. This method internally calls GetRectangle and clicks on the element using its coordinates.
HtmlControl myElement = Find.ByContent("p:Complete", FindContentType.TextContent).As<HtmlControl>();
myElement.MouseClick();

However if you really need to get the coordinates, simply change your code like this:
Element myElement = Find.ByContent("p:Complete", FindContentType.TextContent);          
 
System.Drawing.Rectangle rect = myElement.GetRectangle();
int x = rect.X;
int y = rect.Y;
Log.WriteLine("x coordinates = " + x);
Log.WriteLine("y coordinates = " + y);

Note that this approach requires to add an assembly reference to System.Drawing.dll.

Let me know if you need further assistance on this.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
PP
Top achievements
Rank 1
answered on 17 May 2013, 07:28 AM
Thanks for your quick reply.
But the code is not working for me. It fails with the error saying "Object reference not set to the instance of an object"
I think the element is not getting retrieved. Is there any way I can check if element has been found?

Attaching the error zip file herewith.
0
Plamen
Telerik team
answered on 17 May 2013, 08:33 AM
Hi Prerna,

Yes, the error means that the code you're using to locate the element reruns 'null'. You can verify that using the following code:
HtmlControl myElement = Find.ByContent("p:Complete", FindContentType.TextContent).As<HtmlControl>();
 
//Check whether the element is successfully found
Assert.IsNotNull(myElement);

What is the type of the element you're searching for? Also is it a Silverlight or an HTML element? I noticed in the failure details that your application is not only HTML, but it also contains Silverlight. Could you please provide some screenshots showing how this element looks like in the DOM tree? 
 
Kind regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
PP
Top achievements
Rank 1
answered on 17 May 2013, 09:14 AM
The element I am trying to search for is a ListBox Cell Value and another one is a GridItem(First Column Cell, which is being used of selecting the row). Attaching the DOM screenshot related to ListBox. I am searching for TextBlock content "Vacant Property" in there.

Yes, myElement returns null. It is a Silverlight application I am trying to automate.

Thanks,
Prerna
0
Accepted
Plamen
Telerik team
answered on 17 May 2013, 11:43 AM
Hi Prerna,

Thanks for providing the screenshot. If you want to find the ListBoxItem and click on it, you should be able to do that using the following code:
//Get the ListBox control
ListBox listBox = ActiveBrowser.SilverlightApps()[0].Find.ByAutomationId<ListBox>("lstMessages");
 
//Create collection of all ListBoxItems
IList<ListBoxItem> listBoxItems = listBox.Find.AllByType<ListBoxItem>();
 
//Find ListBoxItem with text content "Vacant Property"
ListBoxItem item = listBoxItems.Where<ListBoxItem>(_a => _a.TextBlockContent.Contains("Vacant Property")).FirstOrDefault();
 
//Click the item
item.User.Click();

If you want to click on the TextBlock directly, use the following code instead:
//Get the ListBox control
ListBox listBox = ActiveBrowser.SilverlightApps()[0].Find.ByAutomationId<ListBox>("lstMessages");
 
//Find TextBlock with content "Vacant Property"
TextBlock textBlock = listBox.Find.ByTextContent("Vacant Property").As<TextBlock>();
 
//Click on it
textBlock.User.Click();
 
Let me know if this helps.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
PP
Top achievements
Rank 1
answered on 20 May 2013, 02:44 AM
Hi Plamen,

Thanks a lot for the solution. It works fine :)
I am trying the same for a grid now. Will surely get back on the forum if help will be needed.

Regards,
Prerna
0
Plamen
Telerik team
answered on 20 May 2013, 07:32 AM
Hello Prerna,

I am glad to hear it! Please contact us again if have further problems. However please create a separate ticket or forum post for new issues. It's much easier for us to track all the various issues that way.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
PP
Top achievements
Rank 1
Answers by
Plamen
Telerik team
PP
Top achievements
Rank 1
Share this question
or