Introduction:
In this blog post, we will explore how to build a robust C# NUnit Selenium framework using the Page Object Model (POM). The POM is a design pattern that enhances test maintenance and reusability by encapsulating web page elements and interactions into separate classes, called Page Objects. We'll create a framework that handles multiple pages, such as the 'SignInPage,' 'ProfilePage,'. Additionally, we'll utilize a CommonDriver class to manage the web driver instance gracefully across tests.
Prerequisites:
Before getting started, ensure you have the following tools installed:
- Visual Studio (or any preferred C# IDE)
- NUnit Test Adapter (for running NUnit tests in Visual Studio)
Step 1: Project Setup
Create a new C# Class Library (.NET Framework) project in Visual Studio and name it 'YourTestFramework.' Add the necessary NuGet packages for NUnit and Selenium WebDriver.
Step 2: Page Objects
Create separate classes for each page, encapsulating the web elements and interactions in the Page Object Model.
// SignInPage.cs
using OpenQA.Selenium;
public class SignInPage
{
private IWebDriver driver;
public SignInPage(IWebDriver driver)
{
this.driver = driver;
}
// Define web elements for the SignInPage
// Add methods for interactions with these elements (e.g., sign-in method)
}
// ProfilePage.cs, ManageListingPage.cs, and SharePage.cs
// Similar structure for other pages
Step 3: CommonDriver Class
Create a CommonDriver class to handle browser initialization and closure.
// CommonDriver.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
public static class CommonDriver
{
public static IWebDriver driver;
public static void Initialize()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
public static void CloseBrowser()
{
driver.Quit();
}
}
Step 4: Base Test Class
Build a base test class to handle common setup and teardown logic that will be shared across test classes.
// BaseTest.cs
using NUnit.Framework;
public class BaseTest
{
protected SignInPage signInPage;
protected ProfilePage profilePage;
protected ManageListingPage manageListingPage;
protected SharePage sharePage;
[OneTimeSetUp]
public void BeforeTestSuite()
{
CommonDriver.Initialize();
signInPage = new SignInPage(CommonDriver.driver);
profilePage = new ProfilePage(CommonDriver.driver);
manageListingPage = new ManageListingPage(CommonDriver.driver);
sharePage = new SharePage(CommonDriver.driver);
}
[OneTimeTearDown]
public void AfterTestSuite()
{
CommonDriver.CloseBrowser();
}
}
Step 5: Test Classes
Create test classes that inherit from the BaseTest class and write the test methods for each page.
// EducationAndCertTest.cs
using NUnit.Framework;
[TestFixture]
public class EducationAndCertTest : BaseTest
{
[Test]
public void TestSignInAndProfileVerification()
{
// Test Sign In and Profile Verification
// Use methods from the SignInPage and ProfilePage classes
}
[Test]
public void TestNavigateToManageListingsPage()
{
// Test Navigate to Manage Listings Page
// Use methods from the SignInPage and ManageListingPage classes
}
[Test]
public void TestNavigateToSharePage()
{
// Test Navigate to Share Page
// Use methods from the SignInPage and SharePage classes
}
// Add other test methods for the new pages as needed
}
Step 6: Running the Tests
To run the tests, open the Test Explorer in Visual Studio, and click the "Run All" button. The tests will execute in sequence, and the browser instance will be reused throughout the test suite, resulting in faster test execution.
Conclusion:
In this blog post, we've learned how to build a powerful C# NUnit Selenium framework using the Page Object Model. The POM promotes maintainability and reusability by encapsulating web elements and interactions in separate Page Object classes. The CommonDriver class gracefully handles browser initialization and closure, while the BaseTest class streamlines common setup and teardown logic across test classes. By following these guidelines, you can create a scalable and maintainable test automation framework to efficiently test web applications.