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

8 posts, 1 answers
  1. PP
    PP avatar
    7 posts
    Member since:
    Apr 2013

    Posted 17 May 2013 Link to this post

    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
  2. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 17 May 2013 Link to this post

    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
  3. PP
    PP avatar
    7 posts
    Member since:
    Apr 2013

    Posted 17 May 2013 Link to this post

    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.
  4. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 17 May 2013 Link to this post

    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
  5. PP
    PP avatar
    7 posts
    Member since:
    Apr 2013

    Posted 17 May 2013 Link to this post

    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
  6. Answer
    Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 17 May 2013 Link to this post

    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
  7. PP
    PP avatar
    7 posts
    Member since:
    Apr 2013

    Posted 19 May 2013 Link to this post

    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
  8. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 20 May 2013 Link to this post

    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
Back to Top