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
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,
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();
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);
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");
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);
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
}
}
}