0

I am trying to provide a confirmation message that the record has been updated successfully.

The HTML is:

<div class="hide">
    <div class="fullRow">
    <div class="notice success">
        The user record has been updated.
        <a href="#close" class="icon-remove"/>
    </div>

The C# code that I am using is:

Assert.IsTrue(UserUpdateAdmin.IsAt, "Fialed to Update");

The UserUpdateAdmin class contains the following:

public class UserUpdateAdmin
{
    public static bool IsAt
    {
        get
        {
            var h1 = Driver.Instance.FindElements(By.ClassName("notice.success"));
            if (h1.Count > 0)
                return true;
            return false;
        }

It is throwing an exception: An exception of type

'Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException' occurred in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll but was not handled in user code

Additional information: Assert.IsTrue failed. Failed to Update

Pooja S
  • 550
  • 2
  • 9

2 Answers2

0

You are using By.ClassName() but it can't contain more than one class. You can get around this by using By.CssSelector(). I also simplified your code.

public static bool IsAt
{
    get
    {
        return (Driver.Instance.FindElements(By.CssSelector("div.notice.success")).Count > 0);
    }
}

NOTE: You have a typo in your error message, it should be "Failed to Update".

Recommendation: Always use {}s... even on a 1-liner if, see below. It prevents confusion, etc.

if (h1.Count > 0)
{
    return true;
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
-1

If your class name contains space, finding by class name usually does not work. Try to use XPath instead:

var h1 = Driver.Instance.FindElements(By.XPath("//div[@class='notice success']"));
kurakura88
  • 2,185
  • 2
  • 12
  • 18
  • The problem with doing it this was is that if the class names aren't exactly, 'notice success', then the locator will fail. So, if the class names get reordered or if another class gets added, it will not work. This is basically doing a string compare. Finding by CSS selector should be preferred to this method. – JeffC Jul 21 '16 at 15:44
  • good point. I failed to understand the detailed requirement and just assumed that it would be exact. – kurakura88 Jul 22 '16 at 01:09