Search code examples
seleniumtfsspecflowspecrunatdd

Selenium webdriver returns string.Empty for breadcrumb text (IwebElement) when deployed onto TFS


This is my first ever post on StackOverflow and i hope i have been able to provide the entire issue in detail. Please do let me know in case i need to provide any other information. Issue Description: I am using a regular breadcrumb that displays my navigation order from one page to the other. The breadcrumb has an id = “divbreadcrumb” in html format.

Eg: If I navigate from Home Page (Home ) to Bookings Page(Bookings), then the navigation is as below:

Home > Bookings

I wish to automate my testing of the breadcrumb and check the breadcrumb text displayed on page versus the actual breadcrumb text it should be. I am trying to achieve that by the use of selenium Webdriver along with ATDD tools namely Specflow/SpecRun and Nunit using the code as mentioned below.

Now,when I try to run the test on my local, it works fine but when i check in the code into TFS and build it, the VS Test Runner fails the build because the it cannot find text from the IwebElement element or it returns the text as String.Empty. Now, the same test, when run on my local machine, works perfectly fine giving the expected test results and the expected values for my Iwebelement (for breadcrumb). Also, the above breadcrumb’s HTML structure is being rendered at the backend, which I can say so because I tried to see HTML it renders using the pagesource property of webdriver at runtime and displayed it in my Exception message.

Also, my back-end HTML code is displayed as:

       <div class="breadcrumb" >
               <div class="breadcrumb-content" >
        <div class="breadcrumb" id="divBreadcrumb" >                        
        <a onclick="some code written here" href="xyz.com">Home</a>
        &nbsp;&nbsp;&gt;&nbsp;&nbsp;<a href="LMN.com">Bookings </a>                      
</div>               
     </div>   
    </div>

PFB the code that I have used in my automation script to check the text coming from the breadcrumb on the page:

public void TestingBreadcrumb(int p0)
{
InternetExplorerOptions IEOptions = new InternetExplorerOptions();
IEOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
IEOptions.IgnoreZoomLevel = true;
IEOptions.UnexpectedAlertBehavior = InternetExplorerUnexpectedAlertBehavior.Ignore;

IWebDriver iEDriver = new InternetExplorerDriver(IEOptions);
iEDriver.Manage().Window.Maximize();
iEDriver.Navigate().GoToUrl("http://example.com");
var breadcrumb = iEDriver.FindElement(By.Id("divBreadcrumb")).Text.ToString();
Assert.AreEqual("Home  >  Bookings ", breadcrumb);
}

The error I get is

[ERROR]   Expected string length 18 but was 0. Strings differ at index 0.  Expected: "Home  > Bookings " 
But was:  <string.Empty>  
-----------^

Solution

  • This was my first question that i posted on Stachoverflow and I am happy that i could find the reason due to which it was giving the error when deployed onto TFS. It is because when the application was run on TFS server, even though the HTML was being generated at the back end but it was not being displayed (may be because the TFS runs the application on low resolution and this was a bit difficult to troubleshoot) and the application was responsive to hide the breadcrumb.

    So even though the html was being rendered, the selenium tool can only find the element but its value would be empty text until the element is visible on front end. we need to check if the element is visible or not and then try to display the element to fetch its value.

    This solved my error above.