Search code examples
c#.netseleniumselenium-webdriverassertions

How to Assert value assigned to ElementId in Selenium c#?


This is supposed to be easy, but somehow I couldn't get this to work as I would have liked to. I am trying to assert on Account value from below piece of code, which does have an ElementId assigned.

My aim is to verify that Account value is greater than zero (or can also do != zero)

I am confused whether to use the GetAttribute or shall I just extract the text out of the ElementId?

<div class="col-md-6 col-sm-12 w-create-account">
<label class="w-label">Value</label>
<h2 id="account-value">$1,00,000</h2>
</div>

I am using Selenium(C#) on NUnit. Any help is appreciated..!!


Solution

  • You can do something similar to following:

    public static decimal ConvertToNumber(string str)
    {
        return decimal.Parse(str, NumberStyles.Currency);
    }
    
    
    [Test]
    public void TestAccountValue()
    {
    
        Assert.IsTrue(ConvertToNumber(driver.FindElement(By.XPath("//div[contains(@class, 'w-create-account']/h2")).Text) > 0, "Account value is not greater than zero.");
    
    }
    

    Let me know, if you have any further queries.