Selenium C# Test Automation Project Structure
Below is a mockup design for a UI & API test automation project structure leveraging C#, Selenium, Page Object Model (POM), Utilities, and GitHub features. This includes examples for critical files and folders.
Project Structure
ProjectRoot
│
├── src
│ ├── Pages
│ │ ├── HomePage.cs
│ │ ├── LoginPage.cs
│ │ └── DashboardPage.cs
│ │
│ ├── Utilities
│ │ ├── DriverFactory.cs
│ │ ├── ConfigReader.cs
│ │ ├── Logger.cs
│ │ └── HelperFunctions.cs
│ │
│ ├── Tests
│ │ ├── UI
│ │ │ ├── BaseUITest.cs
│ │ │ └── LoginTests.cs
│ │ │
│ │ ├── API
│ │ ├── BaseAPITest.cs
│ │ └── UserApiTests.cs
│ │
│ ├── Models
│ │ ├── ApiResponseModel.cs
│ │ └── UserDataModel.cs
│ │
│ ├── Resources
│ └── AppConfig.json
│
├── tests
│ ├── ParallelExecution
│ ├── ChromeTests.cs
│ ├── FirefoxTests.cs
│ ├── EdgeTests.cs
│ └── SafariTests.cs
│
├── .github
│ ├── CODEOWNERS
│ └── pull_request_template.md
│
├── .gitignore
├── README.md
├── TestRun.sln
├── TestRun.csproj
└── pipelines
└── nightly.yml
File Examples
1. DriverFactory.cs (Reusable WebDriver Setup)
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Safari;
public static class DriverFactory
{
public static IWebDriver InitializeDriver(string browser)
{
return browser.ToLower() switch
{
"chrome" => new ChromeDriver(),
"firefox" => new FirefoxDriver(),
"edge" => new EdgeDriver(),
"safari" => new SafariDriver(),
_ => throw new ArgumentException("Unsupported browser: " + browser)
};
}
}
2. HomePage.cs (Page Object Model)
using OpenQA.Selenium;
public class HomePage
{
private readonly IWebDriver _driver;
public HomePage(IWebDriver driver)
{
_driver = driver;
}
private IWebElement SearchBox => _driver.FindElement(By.Id("search-box"));
private IWebElement SearchButton => _driver.FindElement(By.Id("search-button"));
public void Search(string keyword)
{
SearchBox.SendKeys(keyword);
SearchButton.Click();
}
}
3. BaseUITest.cs (Base Class for UI Tests)
using NUnit.Framework;
using OpenQA.Selenium;
public class BaseUITest
{
protected IWebDriver Driver;
[SetUp]
public void Setup()
{
string browser = "chrome"; // Read from config or CLI args
Driver = DriverFactory.InitializeDriver(browser);
Driver.Manage().Window.Maximize();
}
[TearDown]
public void Cleanup()
{
Driver.Quit();
}
}
4. BaseAPITest.cs (Base Class for API Tests)
using RestSharp;
public class BaseAPITest
{
protected RestClient Client;
public BaseAPITest()
{
Client = new RestClient("https://api.example.com");
}
}
5. AppConfig.json (Configuration)
{
"BaseUrl": "https://example.com",
"ApiBaseUrl": "https://api.example.com",
"Browser": "chrome",
"ParallelExecution": true
}
6. CODEOWNERS
# Specify code owners for specific paths
* @project-owner
src/ @ui-team
tests/ @qa-team
7. pull_request_template.md
# Pull Request Checklist
- [ ] Code builds and tests pass.
- [ ] New tests have been added for new functionality.
- [ ] Documentation has been updated (if applicable).
## Description
Provide a summary of your changes and link any relevant issues.
8. .gitignore
# Ignore bin and obj folders
bin/
obj/
# Ignore Selenium Drivers
*.exe
*.dll
# Ignore Config Files
*.config
AppConfig.json
9. README.md
# UI & API Test Automation Project
## Technologies
- C#
- Selenium WebDriver
- NUnit
- RestSharp (for API testing)
- GitHub Actions
## Project Features
- UI Tests (Chrome, Firefox, Edge, Safari)
- API Tests
- Page Object Model Design
- Parallel Execution
- Reusable Utility Functions
## Run Tests
1. Clone the repository.
2. Update configuration in `AppConfig.json`.
3. Execute tests:
- UI: `dotnet test --filter "UI"`
- API: `dotnet test --filter "API"`
## Continuous Integration
Nightly builds run via GitHub Actions. Review the pipeline configuration in `pipelines/nightly.yml`.
10. pipelines/nightly.yml
name: Nightly Build
on:
schedule:
- cron: "0 2 * * *" # Runs nightly at 2:00 AM UTC
push:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0
- name: Install Dependencies
run: dotnet restore
- name: Run Tests
run: dotnet test --logger:trx
This structure ensures modularity, reusability, and maintainability while leveraging GitHub for CI/CD and team collaboration. Let me know if you need further assistance!