Search code examples
c#watin

Watin Find by title issue


I'm having an issue with the Find.ByTitle method. My code is:

if (IE.Exists<IE>(Find.ByTitle(FirstLinkText)))
{
    mainPage = Browser.AttachTo<IE>(Find.ByTitle(new Regex(FirstLinkText))); 
}
else
{
    browser = new IE();
}

If FirstLinkText is "Product and Fund Recommendations" for example it will operate as expected and enter the if statement if the browser page exists. For some reason if FirstLinkText is "Fund Recommendations" it will also enter the if statement even when there is no page with that title but there is a page with the title "Product and Fund Recommendations".

It almost seems as if the Find.ByTitle() method is checking does any page Contain the string being passed rather than does any page have a title that is the exact value of the string being passed.

If this is too confusing give me a shout and I can try rewrite it to make it simpler.

Edit: Just to say that if I do it the opposite way, it works perfectly. Further giving me the impression it is more of a .Contains() operation.


Solution

  • From the meta data for the ByTitle method it states

    // Parameters:
    //   title:
    //     The title to match partially.
    

    Therefore it is similar to the string.Contains() method. This can be over come by using the following:

     if (IE.Exists<IE>(Find.By("Title", FirstLinkText)))
     {
         mainPage = Browser.AttachTo<IE>(Find.ByTitle(new Regex(FirstLinkText))); 
     }
     else
     {
         browser = new IE();
     }
    

    This approach is more manual and has the exact contents checked.