2012-03-16 76 views
0
var vatDisplay = driver.FindElement(By.CssSelector("#tax-amount")); 
Assert.AreEqual("£13.80", vatDisplay.Text); 

我想以另一種方式來做到這一點類似的項目,而不是告訴變量值測試

Assert.AreEqual("£13.80", vatDisplay.Text);  

我需要說的如果是vatDisplay.Text > £0.00,我不知道該怎麼做。 任何人都可以告訴我該怎麼做。

預先感謝您

回答

0

你可以parse the string value into a decimal然後驗證結果值爲正數:

var vatDisplay = driver.FindElement(By.CssSelector("#tax-amount")); 
var culture = new CultureInfo("en-GB"); 
decimal amount; 
if (!decimal.TryParse(vatDisplay, NumberStyles.Currency, culture, out amount)) 
{ 
    Assert.Fail(
     "{0} is an invalid currency and it cannot be verified that the amount is positive", 
     vatDisplay 
    ); 
} 
Assert.IsTrue(amount > 0); 
+0

尤其在使用'Decimal.Parse'重載需要一個'NumberStyles'選項,以便你可以傳遞'NumberStyles.AllowCurrencySymbol'。 – Richard 2012-03-16 10:47:48

+0

@理查德,是的,特別是。還應該使用'NumberStyles.Currency'。 – 2012-03-16 10:53:01

+0

@Darin謝謝你的回覆 – Leo 2012-03-26 09:33:49