This application provides a simple web interface to automatically generate C# Page Object Model (POM) classes for use with Selenium WebDriver in a .NET context, particularly aimed at projects using Reqnroll (formerly SpecFlow).
It analyzes a given URL, identifies interactive elements, generates stable CSS or XPath locators, assigns meaningful names, and outputs a C# class file containing locators and basic interaction methods incorporating Selenium's WebDriverWait for improved stability.
The primary goal of this tool is to accelerate the creation of Page Object classes, reducing boilerplate code and encouraging the use of stable locators and explicit waits within your Selenium test automation framework.
- Backend: .NET 8.0 ASP.NET Core Minimal API
- Web Automation: Selenium WebDriver
- Waiting Mechanism: SeleniumExtras.WaitHelpers (
ExpectedConditions) - Frontend: HTML, CSS, JavaScript (served via ASP.NET Core static files)
- Language: C#
- Web-Based UI: Simple interface to input a URL and trigger generation.
- Automatic Element Discovery: Scans the page for common interactive elements (
input,button,a,select,textarea, etc.). - Stable Locator Generation: Prioritizes stable locators in the order of:
- ID (if stable)
- Name
- Link Text
- Button Text/Value
- Placeholder
- Aria-Label
- Stable CSS Class
- Fallback XPath (less stable)
- Meaningful Naming: Generates PascalCase names for elements based on attributes or text content, handling duplicates.
- C# Class Generation: Outputs a complete
.csfile including:- Namespace and Class definition (class name derived from URL).
IWebDriverandWebDriverWaitfields.- Constructor injecting
IWebDriver. - Private
Byfields for each discovered element locator. - Public interaction methods (e.g.,
ClickElement,EnterElementText,GetElementText,IsElementDisplayed,SelectElementByText) for elements.
- Integrated Waits: Generated interaction methods automatically use
WebDriverWaitandExpectedConditions(e.g.,ElementToBeClickable,ElementIsVisible) before interacting with elements. - Basic Exception Handling: Generated methods include basic
try-catchblocks for common Selenium exceptions. - Code Output & Copy: Displays the generated C# code directly in the browser with a "Copy Code" button.
- Quit Functionality: A "Quit Application" button to gracefully shut down the local web server.
- .NET 8.0 SDK: Required to build and run the application. Download from https://dotnet.microsoft.com/download/dotnet/8.0
- Web Browser: A modern web browser supported by Selenium (e.g., Google Chrome, Microsoft Edge, Firefox).
- WebDriver Executable: The corresponding WebDriver executable for your chosen browser and version (e.g.,
chromedriver.exe,msedgedriver.exe).
- Clone/Download: Get the project code onto your local machine.
git clone <repository-url> # Or download ZIP cd PageObjectGenerator
- Install WebDriver: Download the WebDriver executable matching your browser version.
- Place WebDriver:
- Recommended: Place the WebDriver executable (e.g.,
chromedriver.exe) inside the project's build output directory after building it once (e.g.,.\bin\Debug\net8.0\). The application is configured to look here or in the PATH. - Alternative: Ensure the directory containing the WebDriver executable is included in your system's
PATHenvironment variable.
- Recommended: Place the WebDriver executable (e.g.,
- Build (Optional but good practice):
dotnet build
- Run the Application:
dotnet run
- Access the UI: The console will display the URLs the application is listening on (e.g.,
http://localhost:5127orhttps://localhost:7019). Open one of these URLs in your web browser.
- Enter the full URL (including
http://orhttps://) of the web page you want to generate a Page Object for into the input field. - Click the "Generate Page Object" button.
- Wait for the process to complete (a loading indicator will show). Selenium will launch headlessly in the background.
- The generated C# code will appear in the text area.
- Review the generated locators and method names. You may need to adjust them based on your specific needs or if the automatic generation wasn't perfect.
- Click the "Copy Code" button.
- Paste the code into a new
.csfile within your Reqnroll/Selenium test project. - When you are finished using the tool, click the "Quit Application" button in the UI to stop the local server.
- The application currently attempts to instantiate the
ChromeDriverby default. If you need to use a different browser, you will need to modify thePageObjectGeneratorService.csfile (specifically the WebDriver initialization part). - It expects the WebDriver executable to be found automatically, either alongside the application's
.dllor in the systemPATH. Explicit path configuration is not currently implemented via the UI.
- Locator Stability: While the tool prioritizes stable locators, it cannot guarantee they will work perfectly on all websites, especially those with highly dynamic IDs or complex structures generated by JavaScript frameworks. Manual review and adjustment are often necessary.
- Naming Conventions: Naming is based on heuristics. You might want to refactor names for better clarity or consistency with your project's conventions.
- Frames and Shadow DOM: This tool currently does not automatically handle elements within
<iframe>elements or inside a Shadow DOM. You will need to add logic manually to switch contexts (driver.SwitchTo().Frame(...)or useelement.GetShadowRoot()) if required. - Complex Interactions: Only basic interaction methods are generated. More complex user interactions or assertions will need to be added manually.
- Error Handling: Exception handling in generated methods is basic. Robust test frameworks may require more sophisticated error handling or retry mechanisms.
Contributions are welcome! Please feel free to fork the repository, create a feature branch, and submit a pull request.
(Optional: Specify a license here, e.g., MIT)