Avoiding exception when trying to find a non existing element

6 posts, 0 answers
  1. VVP
    VVP avatar
    61 posts
    Member since:
    Mar 2015

    Posted 19 Jun 2015 Link to this post

    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

  2. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 24 Jun 2015 Link to this post

    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!
  3. VVP
    VVP avatar
    61 posts
    Member since:
    Mar 2015

    Posted 28 Jun 2015 in reply to Boyan Boev Link to this post

    How to find out whether element is null?

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

    Thanks,

    VVP

  4. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 01 Jul 2015 Link to this post

    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!
  5. VVP
    VVP avatar
    61 posts
    Member since:
    Mar 2015

    Posted 03 Jul 2015 in reply to Boyan Boev Link to this post

    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

     

  6. Ivaylo
    Admin
    Ivaylo avatar
    750 posts

    Posted 07 Jul 2015 Link to this post

    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!
Back to Top