Beginner Testing with C# — Pragmatic Software Testing

Suman Tomer
5 min readAug 9, 2021

--

Here we are going to learn how to write first test by using C#, Selenium and SpecFlow.

Prerequisite

Open Visual Studio 2019

Click on Create new Project

Click Next and select location where you want to save your project and then name your project

Click Create and then your project and solution window will look like this

Right click on project and click on Manage NuGet package. It will open new window.

Click on Browse and search for NUnit and install 3.11.0 version. And also install Nunit3TestAdapter (version-3.16.1), Selenium. Support(3.141.0 version), Selenium.WebDriver.ChromeDriver (2.43.0 version), Shouldly (4.0.3), Specflow(2.2.1), SpecFlow.NUnit(2.2.1) and FluentAssertions(5.10.3 version).

Now add 4 new folder by right clicking on Project and add new Folder and named them Steps, Base, Pages and Test cases.

Check by right clicking on your Testcases and then clicking on add new item a window will appear with specflow on it , if doesn’t appear like the pic below then we have to get it by going to extensions. Check again once done.

Go to Extensions on the top bar and then click on Manage Extensions , a small window will get open, go to Online option and search for SpecFlow with Visual Studio and NUnit 3 Test Adapter download them one by one. Then restart you visual studio and open your project.

Then after that go to Tools on top bar and click on Options a small window will get open. This is a one time thing you have to do. Go to Spec flow and on right window go to Legacy and make it True.

And then click Ok.

Next step is to create a SpecFlow Feature File , so for that right click on your testcases , Add New Item and a small window will pop up , there select SpecFlow and on the right side click on SpecFlow Feature File , and give name to your feature file with extension feature(BBCHomefeature.feature) and then click Add.

If you look at that feature.cs file , it will only appear if you make that SpecFlow legacy true . If it is false it wont appear and your program will never run.

Now we have to write our user story in our feature file.

As a user
I want to view the BBc home page
So that I can see the latest news.

@tagname means test cases like regression . So all regression test cases will run. Here we will write @BBCHomPagefeature1 as we are running single testcase. Scenario means what we want to achieve. It is a description of our test case.

Now right click on Base folder and add new item, then select SpecFlow and then Event bindings(SpecFlow hooks) on right and then name the file Setup.cs and then Add. File will look like this.

Delete Binding and all comments and these lines to your file.

Now go to your Feature file , do right click and click on Generate Steps Definitions File. A window will get pop up.

Click on Generate and save it in Steps folder.

Create class in your Pages Folder by right clicking on Pages, add class and then naming it ‘BBCHomeage1.cs’.

Add following code to the file.

using OpenQA.Selenium; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FluentAssertions; using OpenQA.Selenium.Support.PageObjects;  namespace BBCHomePageProj.Pages {     //public IWebDriver driver;     public class BBCHomepage1     {         public IWebDriver Driver;       public BBCHomepage1(IWebDriver Browser)// created constructor         {             Driver = Browser;             PageFactory.InitElements(Driver, this);         }         String BBCurl = "https://www.bbc.co.uk/";         String WelcomeMessage = "Welcome to the BBC";          [FindsBy(How=How.LinkText, Using ="Home")]//to locate Home Link          public IWebElement Homelink;         //to locate webelement in java pagefactory         //@FindBy(how = How.ID, using = "userName")         // WebElement username;         [FindsBy(How = How.CssSelector, Using = ".ssrcss-cl4b5i-MastheadText.e9p57e3")]         public IWebElement Message;        public void NavigateMethod()         {             Driver.Navigate().GoToUrl("https://www.bbc.co.uk/");             //Go to url bbc.co.uk         }         public void HomeClick()         {             Homelink.Click();// Click on Home link         }         public void HomeMessageAssert()         {             Message.Text.Contains(WelcomeMessage).Should().BeTrue();             //Verifying that Welcome message is present on the web page             //should be true.             //Driver.Url.Contains(BBCurl).Should().BeTrue();             //Verifying URl         }     } }

Now create new class file in Steps Folder and add the code.

using System; using TechTalk.SpecFlow; using BBCHomePageProj; using OpenQA.Selenium; using BBCHomePageProj.Base; using BBCHomePageProj.Pages; namespace BBCHomePageProj.Steps { [Binding] [Scope(Tag = "BBCHomePagefeature")]//its a tag name similar to given in feature //which test cases needs to run. public class BBCHomePagefeature1Steps : Setup { public BBCHomepage1 page; public IWebDriver Browser; [Given(@"I navigate to BBC home page")] public void GivenINavigateToBBCHomePage() { Browser = driver; page = new BBCHomepage1(Browser); page.NavigateMethod(); } [When(@"I click Home menu")] public void WhenIClickHomeMenu() { page.HomeClick(); } [Then(@"I see Home page loads")] public void ThenISeeHomePageLoads() { page.HomeMessageAssert(); } } }

Build the solution .

Run the test

Congratulations!! your first test passed.

--

--

Suman Tomer
Suman Tomer

Written by Suman Tomer

Test Automation, QA Engineer, Azure Cloud, Helping woman who wants to learn testing and join industries

No responses yet