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

Don't throw exception when TimeOut using HtmlElementContainer.Get() method

3 Answers 81 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chhuong
Top achievements
Rank 1
Chhuong asked on 02 Sep 2010, 10:50 PM
When I use HtmlElementContainer.Get<HtmlDiv>(...), it will throw exception if it can't find element in the page for a specified period. Is there any setting to return null if the control is not found? I don't want to catch exception every time I find an element.

Example: this is what I want to do

HtmlDiv divUserName = HtmlElementContainer.Get<HtmlDiv>(new HtmlFindExpression(...), true, 5000);
Assert.IsNotNull(divUserName , "Cannot find username control");

3 Answers, 1 is accepted

Sort by
0
Nikolai
Telerik team
answered on 03 Sep 2010, 07:46 AM
Hello Chhuong,

In general you should not catch the exceptions because this is how all .NET tests are organized. If there is something wrong with your test exception will occur to notify the run-time that something is wrong and the test should fail. After that you examine the stack trace to see what went wrong but if you catch the exception the run-time will continue and ignore the current error so next error will be misleading. In fact all the Assert methods throw exception if their condition is not met so even if you catch the Find exception the Assert will throw one.  

Currently there is no way to stop the "Get"  method from throwing exception. You can however use Find instead of Get this way if element is not found a Null object can be returned.
 

Manager.ActiveBrowser.Find.ThrowIfNullOrEmpty = false;
var divUserName = Manager.ActiveBrowser.Find.ByAttributes<HtmlDiv>("id=MyID");
Assert.IsNotNull(divUserName , "Cannot find username control");


All the best,
Nikolai
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chhuong
Top achievements
Rank 1
answered on 03 Sep 2010, 08:07 AM
Can I specify the timeout with Find like Get() method does (wait for 5000 milliseconds for control to be visible)?

e.g. HtmlDiv divUserName = HtmlElementContainer.Get<HtmlDiv>(new HtmlFindExpression(...), true, 5000);
0
Nikolai
Telerik team
answered on 03 Sep 2010, 08:23 AM
Hi Chhuong,

Unfortunately no you cannot set a TimeOut interval for the Find. You must be sure that the element exists before trying to find it.  

Regards,
Nikolai
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Chhuong
Top achievements
Rank 1
Answers by
Nikolai
Telerik team
Chhuong
Top achievements
Rank 1
Share this question
or