简体   繁体   English

Selenium 框架使用 C# 关于断言语句

[英]Selenium Framework Using C# regarding with assert statement

I am trying to provide a confirmation message that the record has been updated successfully.我正在尝试提供一条记录已成功更新的确认消息。

The HTML is: HTML是:

<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:我使用的 C# 代码是:

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

The UserUpdateAdmin class contains the following: UserUpdateAdmin 类包含以下内容:

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 “Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException”发生在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中,但未在用户代码中处理

Additional information: Assert.IsTrue failed.附加信息:Assert.IsTrue 失败。 Failed to Update更新失败

You are using By.ClassName() but it can't contain more than one class.您正在使用By.ClassName()但它不能包含多个类。 You can get around this by using By.CssSelector() .您可以通过使用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.建议:始终使用{} s... 即使在 1-liner if ,请参见下文。 It prevents confusion, etc.它可以防止混淆等。

if (h1.Count > 0)
{
    return true;
}

If your class name contains space, finding by class name usually does not work.如果您的类名包含空格,则按类名查找通常不起作用。 Try to use XPath instead:尝试改用 XPath:

var h1 = Driver.Instance.FindElements(By.XPath("//div[@class='notice success']"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM