簡體   English   中英

如何在Selenium c#中聲明分配給ElementId的值?

[英]How to Assert value assigned to ElementId in Selenium c#?

這本來應該很容易,但是以某種方式,我卻無法按照我的意願進行操作。 我正在嘗試從下面的代碼中斷言Account值 ,該代碼確實分配了ElementId。

我的目的是驗證帳戶值是否大於零(或者也可以做到!=零)

我是否使用GetAttribute感到困惑,還是應該從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>

我在NUnit上使用Selenium(C#)。 任何幫助表示贊賞..!

您可以執行以下操作:

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.");

}

讓我知道,如果您還有其他疑問。

您可以使用以下邏輯進行斷言

碼:

var accountValue=driver.FindElement(By.Id("account-value")).Text;
accountValue = accountValue.Substring(1);//Inorder to remove the currency symbol.

var amount = Decimal.Parse(accountValue);

Assert.IsTrue(amount > 0);//If the Amount value is greater than 0, then assertion wil be passed.

假設,如果帳戶值也持有負余額,則用以下條件替換子字符串語句

var accountValue=driver.FindElement(By.Id("account-value")).Text;

if (accountValue.Contains('-')){
    accountValue = "-" + accountValue.Substring(2);//Inorder to remove the currency symbol and negative sign
}
else
{
    accountValue = accountValue.Substring(1);//Inorder to remove the currency symbol.
}

var amount=Decimal.Parse(accountValue);

Assert.IsTrue(amount > 0);//If the Amount value is greater than 0, then assertion wil be passed.

我在定價服務中使用了這個:

Assert.Greater(Convert.ToDouble(driver.FindElement(By.Id("account-value")).Text), 0,  
 "Acccount value is zero 0");

-表示,如果“帳戶”值為零,則失敗。

或者如果您想添加斷言為零。 它可能像:

string accountValue = driver.FindElement(By.Id("account-value")).Text;  
Assert.IsFalse(accountValue.Contains("0"), "Account value is zero");  

-我希望將其顯示為文本,否則,您需要使用.GetAttribute("value") = number。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM