-3

I need to assert if page body contains text "error", how would I do that? I got to this point and I am getting error on .getText

driver.FindElement(By.TagName("body")).getText().Equals("Error")
safary
  • 325
  • 2
  • 7
  • 26
  • 2
    What have you tried so far? Please remember that stackoverflow is not a website where you'd turn to get code written for you. – Joakim Hansson Sep 28 '15 at 13:42
  • http://stackoverflow.com/a/18338377/43846 – stuartd Sep 28 '15 at 13:51
  • driver.FindElement(By.TagName("body")).getText().Equals("Error") and gettext is marked as error saying "IWebElement does not contain a definition..." – safary Sep 28 '15 at 13:58
  • 1
    Ah, that's the Java version. For C#, use [`.Text`](http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/P_OpenQA_Selenium_IWebElement_Text.htm). Also, you should be checking if the text _Contains_ "error", rather than Equals – stuartd Sep 28 '15 at 15:02

1 Answers1

1

You don't really want to do it this way... but since you asked, here is the answer.

driver.FindElement(By.TagName("body")).Text.Contains("Error");

You were using .getText() which is Java. You were also using .Equals() but what you meant was .Contains() because the entirety of the body tag is likely not to be only "Error".

What I would recommend is for you to narrow down to the element that actually contains the "Error" text and then use the line above (with the proper locator), e.g.

driver.FindElement(By.Id("the ID of the element that contains Error")).Text.Contains("Error");

You'd have to provide the surrounding HTML for the element for us to help you any more specifically.

JeffC
  • 22,180
  • 5
  • 32
  • 55