mhossen / FluentAssertionWithSelenium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FluentAssertion with Selenium

Notes: This tests project doesn't focus on building a framework for Selenium, but uses selenium library with Fluent Assertion library to demonstrate uses for Assertion.


Packages Version
Target Framework netcoreapp3.1
FluentAssertions 5.10.3
nunit 3.12.0
NUnit3TestAdapter 3.17.0
Selenium.Support 3.14.0
Selenium.WebDriver 3.14.0
WebDriverManager 2.11.0

Before running this project I would check for the .Net SDK installed on my machine then either right click on the project name in Visual Studio Solution Explorer --> Properties --> Application and change the Target framework. Or you can follow the instruction on the following link How to target Multiple Framework to change at .csproj file.

Target Framework


Basic Text Evaluation

string message = "Welcome user John Smith";

Start With

Evaluates string starts with specific character(s) from the above example if we want to check if the message has the word Welcome.

message.Should().StartWith("Welcome");

End With

Evaluates string ends with specific character(s) from the above example if we want to check if the message has John Smith.

message.Should().EndWith("John Smith");

Contains

Evaluates that string contains a series of character(s) from above example if we want to check if the word user exist.

message.Should().Contain("user");

Assertion Scope

Assertion Scope batches multiple assertions so that when evaluating multiple facts, FluentAssertions throws one collective exception summary at the end of the scope for all failures.

using (new AssertionScope())
{
    message.Should().StartWith("Welcome")
    .And.EndWith("John Smith")
    .And.Contain("user");
}

Working With Collections

<ul class="list-group" id="unorderedList">
   <li class="list-group-item">Apple</li>
   <li class="list-group-item">Orange</li>
   <li class="list-group-item">Lychee</li>
   <li class="list-group-item">Pomegranate</li>
   <li class="list-group-item">Watermelon</li>
   <li class="list-group-item">Mangosteen</li>
</ul>

Checking Unique Items In Collection

Checking for a list of element is coming back as unique.

driver.FindElements(By.XPath(".//ul/li")).Select(e => e.Text)
.Should().OnlyHaveUniqueItems();

Checking For Collection In Ascending Order

driver.FindElements(By.XPath(".//ul/li")).Select(e => e.Text)
.Should().BeInAscendingOrder();

For more information checkout the Fluent Assertion Official Documentation


© 2020 Mohammed Hossen

About


Languages

Language:C# 78.1%Language:HTML 16.4%Language:JavaScript 4.5%Language:CSS 1.0%