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

How to find System.Windows.Controls.TextBlock ?

10 Answers 228 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Tomasz
Top achievements
Rank 1
Tomasz asked on 07 Mar 2017, 10:30 AM

Hi,

I have a conversion problem:

In my application I have a 'System.Windows.Controls.TextBlock' type textblock and I need to find it in my coded step and make on it a MauseClick.

This is fragment of my code:

System.Windows.Controls.TextBlock textblock = app.MainWindow.Find.ByTextContent(text_content.ToString());

and in test log I have a information:

"Cannot implicitly convert type 'ArtOfTest.WebAii.Silverlight.FrameworkElement' to 'System.Windows.Controls.TextBlock'"

Is there a way to make that conversion or some other way to resolve this problem?

10 Answers, 1 is accepted

Sort by
0
Nikolay Petrov
Telerik team
answered on 10 Mar 2017, 09:02 AM
Hi Tomasz,

What is the nature of the application you are automating?

If it is a WPF one - add this using:
using Telerik.WebAii.Controls.Xaml.Wpf;

and this element identifier:
ArtOfTest.WebAii.Controls.Xaml.Wpf.TextBlock

I hope it will solve the problem.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Tomasz
Top achievements
Rank 1
answered on 10 Mar 2017, 09:54 AM
Hi Nikolay,

Yes, it is a WPF application (sorry for a non complete information).

using Telerik.WebAii.Controls.Xaml.Wpf; - I have it on my code

When I try to use 'ArtOfTest.WebAii.Controls.Xaml.Wpf.TextBlock' there is still conversion problem. I tried to find this conversion but without success. In Test Log I have the information:

"Cannot implicitly convert type 'ArtOfTest.WebAii.Silverlight.FrameworkElement' to 'ArtOfTest.WebAii.Controls.Xaml.Wpf.TextBlock'. An explicit conversion exists (are you missing a cast?)"
0
Tomasz
Top achievements
Rank 1
answered on 10 Mar 2017, 11:07 AM

And when I use:

"ArtOfTest.WebAii.Silverlight.UI.TextBlock gTextBlock = app.MainWindow.Find.ByTextContent(text_content.ToString()).As<TextBlock>();"

then the test is executing, but it's end with the filure:

"InnerException:
System.ArgumentException: The control type 'ArtOfTest.WebAii.Silverlight.UI.TextBlock' does not match the xaml tag 'htmltextblock'
   at ArtOfTest.WebAii.Silverlight.FrameworkElement.As[T]()"

The 'htmltextblock' is my own textblock type, but it's inherits from 'System.Windows.Controls.TextBlock'

That is why I need to convert 'ArtOfTest.WebAii.Silverlight.FrameworkElement' to 'System.Windows.Controls.TextBlock' - my textblock is System.Windows.Controls.TextBlock type and I need to find it in code step. To find it I use 'app.MainWindow.Find.ByTextContent' method which returns 'ArtOfTest.WebAii.Silverlight.FrameworkElement'.

So, do You knew whether such a conversion is possible?

0
Nikolay Petrov
Telerik team
answered on 14 Mar 2017, 04:13 PM
Hi Tomasz,

Unfortunately there is not possible to use directly System.Windows.Controls.TextBlock cast. In the framework there are wrappers of such controls. If the intend here is to search the element by text content the cast should be as follows:

var myTextBlock = ActiveApplication.MainWindow.Find.ByTextContent(@"my text is here").CastAs<ArtOfTest.WebAii.Controls.Xaml.Wpf.TextBlock>();

I hope these pointers are useful to you.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Tomasz
Top achievements
Rank 1
answered on 23 Mar 2017, 01:11 PM
Hi Nikolay,

Thanks very much :) Your advice ware very helpful and now I can find my TextBlock.

Unfortuneately I have another problem:

The Find.ByTextContent is working good. When I check the text content of myTextBlock it is always the right text, but when I try to do the right click on it:

myTextBlock.User.Click(MouseClickType.RightClick)

it sometimes hits the right element and sometimes hits wrong one. Maybe You have some idea, why it not always clicks the myTextBlock element?
0
Nikolay Petrov
Telerik team
answered on 28 Mar 2017, 07:58 AM
Hi Tomasz,

I'm glad that this problem is solved.

Does the other element has same content like the one that you are trying to automate? The find logic will find the first element that fits to the given criteria. I suggest that this seems to be the problem here. Try to adjust the find logic to be the unique or search for a list of elements and then choose the one from the list that is the correct one.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Tomasz
Top achievements
Rank 1
answered on 04 Apr 2017, 11:22 AM

Hi Nikolay,

There is only one element with that search content and that is why I know that Find.ByTextContent method is working good - when I check the text content of found myTextBlock, it is always the right content.

The problem is with myTextBlock.User.Click(MouseClickType.RightClick) method. For some reason this method sometimes hits the textblock which was found by Find.ByTextContent method and sometimes it hits the wrong one (but always in the same column of RadGridView)

The question is, how this method (myTextBlock.User.Click(MouseClickType.RightClick)) is tracking down the element for click and why it not always clicks the right one?

0
Nikolay Petrov
Telerik team
answered on 07 Apr 2017, 07:36 AM
Hello Tomasz,

The method .User.Click() performs click action on the element that is already found by the previously implemented search action - myTextBlock. To debug and to understand if this element is found every time correctly - try to print in the execution log its content or other attribute before to perform click action. Then check when it is not clicked correctly if the element is the initially found right one.

I hope these directions are helpful.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Tomasz
Top achievements
Rank 1
answered on 13 Apr 2017, 11:05 AM

Hi Nikolay,

I found a solution of my problem. The Find.ByTextContent method is working good - I know that, because when I'm checking (printing) content after each searching action, it is always the right content. But in some reason the User.Click() method sometimes failed. To resolve this problem, I used the myTextBlock.Refresh() method. After that, the User.Click() method hits the target every time :)

0
Elena
Telerik team
answered on 17 Apr 2017, 03:15 PM
Hello Tomasz,

Thanks for getting back to us and sharing your experience. I hope it could be also helpful to some other customers! Thanks again! 

Regards,
Elena Tsvetkova
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Tomasz
Top achievements
Rank 1
Answers by
Nikolay Petrov
Telerik team
Tomasz
Top achievements
Rank 1
Elena
Telerik team
Share this question
or