How to setup automation POM framework using selenium and C# language with Test Project
Setting up an automation framework using Selenium and C# with the Page Object Model (POM) design pattern involves several steps. The Page Object Model is a popular design pattern for enhancing the maintainability and reusability of your Selenium automation code. Here's a step-by-step guide on how to set up a basic POM framework using Selenium and C#:
- Before you begin, make sure you have the following installed:
- Visual Studio (or any other C# IDE of your choice).
- Selenium WebDriver for C#.
- MSTest (or any other testing framework like NUnit or xUnit).
- WebDriver Chrome Driver (or any other WebDriver for your preferred browser).
Open Visual Studio and create a new C# project. You can choose "Class Library" for a simple setup.
In your project,
you'll need to add the Selenium WebDriver and MSTest packages. Right-click on
your project in Solution Explorer, select "Manage NuGet Packages,"
and search for and install the following packages:
- Selenium.WebDriver
- Selenium.Support
- MSTest
- MSTestAdapter (if using MSTest)
Create a folder
structure for your page objects. In this structure, create a class for each
page or component you want to interact with in your application. These classes
should represent the elements and actions available on that page.
public class LoginPage
{
private IWebDriver driver;
public LoginPage(IWebDriver driver)
{
this.driver = driver;
}
// Page elements
IWebElement UsernameInput => driver.FindElement(By.Id("username"));
IWebElement PasswordInput => driver.FindElement(By.Id("password"));
IWebElement LoginButton => driver.FindElement(By.Id("loginbutton"));
// Page actions
public void EnterUsername(string username)
{
UsernameInput.SendKeys(username);
}
public void EnterPassword(string password)
{
PasswordInput.SendKeys(password);
}
public void ClickLoginButton()
{
LoginButton.Click();
}
}
Step 5: Create Test Cases
Create test case
classes using MSTest (or your chosen testing framework). In these classes,
you'll use the page objects you created to interact with your application.
[TestFixture]
public class LoginTests
{
private IWebDriver driver;
private LoginPage loginPage;
[SetUp]
public void Setup()
{
driver = new ChromeDriver(); // Initialize the WebDriver
loginPage = new LoginPage(driver);
driver.Navigate().GoToUrl("https://example.com/login"); // Replace with your application URL
}
[Test]
public void ValidLoginTest()
{
loginPage.EnterUsername("yourUsername");
loginPage.EnterPassword("yourPassword");
loginPage.ClickLoginButton();
// Add assertions here to verify the login was successful
}
[TearDown]
public void Teardown()
{
driver.Quit(); // Close the WebDriver
}
}
Step 6: Run Tests
Build your project,
and you can now run your test cases using MSTest or your chosen testing
framework. Make sure to add appropriate assertions to verify the expected
behavior of your application.
This is a basic
setup for a Selenium WebDriver automation framework using C# and the Page
Object Model. You can expand on this foundation by adding more page objects,
reusable methods, and configurations based on the needs of your automation
project.

Comments
Post a Comment