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
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
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?)"
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?
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
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?
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
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?
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
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 :)
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