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

How to judge whether the element exists

3 Answers 303 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
lucia
Top achievements
Rank 1
lucia asked on 06 Jan 2012, 02:32 AM
Here is a scenario as below:
I want to know whether the element exists, if exists, the test will execute some codes, if not exists, test will execute other codes.
Now, I use find to get the element, but if it does not exist, an exception with "Element not found"  throws, and so left codes will not be executed.
So please give me some hlep to handle this situation, Thanks!! 

3 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 06 Jan 2012, 11:09 AM
Hi lucia,

There is a really simple solution to the problem you are experiencing, you can simply change the Find Strategy as seen in this Blog. Here's a sample code:
    SilverlightApp app = this.ActiveBrowser.SilverlightApps()[0];
    // save the original strategy to set it back at test end
    FindStrategy originalStrategy = app.Find.Strategy;
      
    try
    {
        // reset the find strategy
        app.Find.Strategy = FindStrategy.WhenNotVisibleReturnNull;
      
        FrameworkElement element = app.Find.ByTextContent("Content");
                  
        if(element == null)
        {
            //Element does not exists, execute some code
        }
        else
        {
            //Element exists, execute other code
        }
      
    }
        finally
    {
        // Set back the original find strategy
        app.Find.Strategy = originalStrategy;
     
    }  
}

Using the new strategy "WhenNotVisibleReturnNull", the Find method will return null if the element doesn't exist, instead of throwing "Element Not Found" exception.

Hope this helps!

Regards,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
lucia
Top achievements
Rank 1
answered on 17 Jan 2012, 03:14 AM
Hi Plamen 
I have tried your sample code in my test code, but the same exception is still thrown.
Here is my code:
 
silApp.Find.Strategy = FindStrategy.WhenNotVisibleReturnNull;
Console.WriteLine(silApp.Find.Strategy);
if (silApp.FindName("panelFavorite").Find.ByName("lstFavorite") ==
null)
{
Console.WriteLine("non");
}

It throws "Element not found".


0
Plamen
Telerik team
answered on 19 Jan 2012, 07:13 PM
Hello Lucia,

The problem is that these Find methods are connected to each other. If you use them separately the FindStrategy will work. For example if you use it like this:
if (silApp.FindName("panelFavorite")== null){}
This code will return null if the element doesn't exists.

In your case, I think the best possible solution is to use XamlFindExpression and Chained Identification. The code provided by you can be easily replaced by this one:
if (silApp.Find.ByExpression(new XamlFindExpression("Name=panelFavorite","|","Name=lstFavorite"), false)== null)
{
    Log.WriteLine("Element doesn't exists!");
}
else
{
    Log.WriteLine("Element exists!");
}

Note that using this code there is no need to change the FindStrategy.  

Let me know if you need further assiatance on this.

All the best,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
lucia
Top achievements
Rank 1
Answers by
Plamen
Telerik team
lucia
Top achievements
Rank 1
Share this question
or