Step 1: Setting up the Project Create a new SpecFlow project in Visual Studio and add necessary NuGet packages: SpecFlow, Selenium WebDriver, NUnit, NUnit3TestAdapter, and AventStack.ExtentReports. You can also add other required packages as needed.
Add the necessary NuGet packages:
- Selenium.WebDriver
- Selenium.WebDriver.ChromeDriver
- SpecFlow
- SpecFlow.Tools.MsBuild.Generation
Step 2: Folder Structure Organize your project with a proper folder structure to manage your pages, step definitions, and utilities. Here's an example of how you can structure your project:
Step 3: Creating Page Objects Create two page object classes for the Login page and Home page, which will contain locators and methods to interact with the elements on the respective pages.
LoginPage.cs:
using OpenQA.Selenium;
namespace ProjectName.Pages
{
public class LoginPage
{
private IWebDriver driver;
// Locators for elements on the Login page
private By usernameInput = By.Id("username");
private By passwordInput = By.Id("password");
private By loginButton = By.Id("loginBtn");
public LoginPage(IWebDriver driver)
{
this.driver = driver;
}
public void EnterUsername(string username)
{
driver.FindElement(usernameInput).SendKeys(username);
}
public void EnterPassword(string password)
{
driver.FindElement(passwordInput).SendKeys(password);
}
public void ClickLoginButton()
{
driver.FindElement(loginButton).Click();
}
}
}
HomePage.cs:
using OpenQA.Selenium;
namespace ProjectName.Pages
{
public class HomePage
{
private IWebDriver driver;
// Locators for elements on the Home page
private By welcomeMessage = By.CssSelector(".welcome-message");
private By logoutButton = By.Id("logoutBtn");
public HomePage(IWebDriver driver)
{
this.driver = driver;
}
public string GetWelcomeMessage()
{
return driver.FindElement(welcomeMessage).Text;
}
public void ClickLogoutButton()
{
driver.FindElement(logoutButton).Click();
}
}
}
Step 4: Creating Step Definitions Next, create two step definition classes for the Login page and Home page. These classes will contain the implementation of the steps defined in the feature file.
LoginSteps.cs:
using ProjectName.Pages;
using TechTalk.SpecFlow;
namespace ProjectName.Steps
{
[Binding]
public class LoginSteps
{
private LoginPage loginPage;
public LoginSteps()
{
loginPage = new LoginPage(Driver.Instance);
}
[Given("I am on the login page")]
public void GivenIAmOnLoginPage()
{
// Navigate to the login page URL
Driver.Instance.Navigate().GoToUrl(Config.BaseUrl + "/login");
}
[When("I enter the username '(.*)'")]
public void WhenIEnterUsername(string username)
{
loginPage.EnterUsername(username);
}
[When("I enter the password '(.*)'")]
public void WhenIEnterPassword(string password)
{
loginPage.EnterPassword(password);
}
[When("I click the login button")]
public void WhenIClickLoginButton()
{
loginPage.ClickLoginButton();
}
}
}
HomeSteps.cs:
using ProjectName.Pages;
using TechTalk.SpecFlow;
using NUnit.Framework;
namespace ProjectName.Steps
{
[Binding]
public class HomeSteps
{
private HomePage homePage;
public HomeSteps()
{
homePage = new HomePage(Driver.Instance);
}
[Then("I should see the welcome message '(.*)'")]
public void ThenIShouldSeeWelcomeMessage(string welcomeMessage)
{
Assert.AreEqual(welcomeMessage, homePage.GetWelcomeMessage());
}
[When("I click the logout button")]
public void WhenIClickLogoutButton()
{
homePage.ClickLogoutButton();
}
}
}
Step 5: Creating the Feature File Create a feature file (Login.feature) with scenarios representing the steps of the Login and Home page.
Login.feature:
Feature: Login Functionality
Scenario: Login with valid credentials
Given I am on the login page
When I enter the username 'testuser'
And I enter the password 'password123'
And I click the login button
Then I should see the welcome message 'Welcome, testuser'