Assert in Selenium C#

后端 未结 8 758
[愿得一人]
[愿得一人] 2021-01-07 11:45

Is there a Assert class present in Selenium C# just like we have in Coded UI test.

Or I should use the Microsoft.VisualStudio.TestTools.UnitTesting.Asser

相关标签:
8条回答
  • 2021-01-07 11:55

    You can use MSTest or I would rather prefer to write simple assertion method. In Selenium, most of the use cases would be a Boolean check, IsTrue | IsFalse (you can even extend to write more complex assertions), so when you define your own assertion you will get more control of your script, like,

    • Taking a screenshot if the assertion fails
    • You can live with the failure and continue the test
    • You can mark the script as partially passing
    • Extract more information from the UI, like JavaScript errors or server errors
    0 讨论(0)
  • 2021-01-07 11:57

    This way assert can be used for switching into popup's displayed:

    IAlert alert = driver.SwitchTo().Alert();
    String alertcontent = alert.Text;
    Assert.AreEqual(alertcontent, "Do you want to save this article as draft?");
    obj.waitfn(5000);
    alert.Accept();
    
    0 讨论(0)
  • 2021-01-07 11:58
    Assert.Equals(obj1, obj2); // Object comparison
    Assert.AreEqual(HomeUrl, driver.Url); // Overloaded, comparison. Same object value
    Assert.AreNotEqual(HomeUrl, driver.Url);
    Assert.AreSame("https://www.google.com", URL); // For the same object reference
    
    Assert.IsTrue(driver.WindowHandles.Count.Equals(2));
    Assert.IsFalse(driver.WindowHandles.Count.Equals(2));
    
    Assert.IsNull(URL); Assert.IsNotNull(URL);
    
    0 讨论(0)
  • 2021-01-07 12:01

    The Assert class is available with either the MSTest or NUnit framework.

    I used NUnit, and there is the Assert class as in below line of code.

    Sample code:

    Assert.AreEqual(AmericaEmail, "SupportUsa@Mail.com", "Strings are not matching");
    
    0 讨论(0)
  • 2021-01-07 12:04

    To use Assert you have to first create the unit testing project in Visual Studio.

    Or import the following reference to the project.

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    // Using this you can use the Assert class.
    
    Assert.IsTrue(bool);
    Assert.IsFalse(bool);
    Assert.AreEqual(string,string);
    
    0 讨论(0)
  • 2021-01-07 12:12

    Selenium C# is not offering an assert class. You have to borrow it from somewhere or write your own implementation.

    Writing your own implementation will give you more freedom to manage assertions, as Peter said.

    Here is the basic idea. Just create a static class having assert methods like this:

    public static class MyAssertClass
    {
        public static void MyAreEqualMethod(string string1, string string2)
        {
            if (string1 != string2)
            {
              // Throw an exception
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题