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

Avoiding exception when trying to find a non existing element

5 Answers 50 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
VVP
Top achievements
Rank 2
VVP asked on 19 Jun 2015, 10:38 AM

Hi All,

 I am excepting an element which sometimes shows up and someother time doesnt and it depends on testcase. But in our code we have something like below.

Element adjustmentDetailsWindow = Manager.ActiveBrowser.Find.ById("viewDetailsContentWindow");

So problem is when window is present, we get correct results, but if window is not present, then we get an exception saying "Object Reference not set to isntance of object"

I put them in try catch block.But is there any better way to avoid exception as well when window is not present.?

Thanks,

VVP

5 Answers, 1 is accepted

Sort by
0
Boyan Boev
Telerik team
answered on 24 Jun 2015, 08:37 AM
Hi Vishnu,

The try/catch block is probably the best option here.

Another option is to use if/else block to check whether the element is null or not and then take some action against it.

Hope this helps.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
VVP
Top achievements
Rank 2
answered on 28 Jun 2015, 08:35 AM

How to find out whether element is null?

I couldn't find any options to check for null.

Thanks,

VVP

0
Boyan Boev
Telerik team
answered on 01 Jul 2015, 12:34 PM
Hi Vishnu,

Just simply write:

if(element==null)
{
}
else
{
}

Let me know if this helps.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
VVP
Top achievements
Rank 2
answered on 03 Jul 2015, 05:34 AM

Hi Boyan Boev,

Exception will be thrown when we try to find the element. So your code wont work.

Element e1 = Manager.ActiveBrowser.Find.ById("duplicateview");

Will throw exception if element with id "duplicateview" is not present.

 

Thanks,

VVP

 

0
Ivaylo
Telerik team
answered on 07 Jul 2015, 12:21 PM

Hi,

You can try the following:

Element e1 = Manager.ActiveBrowser.Find.ById("duplicateview");
Assert.IsNull(e1);


Regards,

Ivaylo
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
VVP
Top achievements
Rank 2
Answers by
Boyan Boev
Telerik team
VVP
Top achievements
Rank 2
Ivaylo
Telerik team
Share this question
or