Skip to content

jagobond/PageObjectGeneratorWithFries

Repository files navigation

.NET 8 Selenium Page Object Generator for Reqnroll

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.

Purpose

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.

Technology Stack

  • 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#

Features

  • 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:
    1. ID (if stable)
    2. Name
    3. Link Text
    4. Button Text/Value
    5. Placeholder
    6. Aria-Label
    7. Stable CSS Class
    8. 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 .cs file including:
    • Namespace and Class definition (class name derived from URL).
    • IWebDriver and WebDriverWait fields.
    • Constructor injecting IWebDriver.
    • Private By fields for each discovered element locator.
    • Public interaction methods (e.g., ClickElement, EnterElementText, GetElementText, IsElementDisplayed, SelectElementByText) for elements.
  • Integrated Waits: Generated interaction methods automatically use WebDriverWait and ExpectedConditions (e.g., ElementToBeClickable, ElementIsVisible) before interacting with elements.
  • Basic Exception Handling: Generated methods include basic try-catch blocks 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.

Prerequisites

  1. .NET 8.0 SDK: Required to build and run the application. Download from https://dotnet.microsoft.com/download/dotnet/8.0
  2. Web Browser: A modern web browser supported by Selenium (e.g., Google Chrome, Microsoft Edge, Firefox).
  3. WebDriver Executable: The corresponding WebDriver executable for your chosen browser and version (e.g., chromedriver.exe, msedgedriver.exe).

Setup & Running

  1. Clone/Download: Get the project code onto your local machine.
    git clone <repository-url> # Or download ZIP
    cd PageObjectGenerator
  2. Install WebDriver: Download the WebDriver executable matching your browser version.
  3. 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 PATH environment variable.
  4. Build (Optional but good practice):
    dotnet build
  5. Run the Application:
    dotnet run
  6. Access the UI: The console will display the URLs the application is listening on (e.g., http://localhost:5127 or https://localhost:7019). Open one of these URLs in your web browser.

How to Use

  1. Enter the full URL (including http:// or https://) of the web page you want to generate a Page Object for into the input field.
  2. Click the "Generate Page Object" button.
  3. Wait for the process to complete (a loading indicator will show). Selenium will launch headlessly in the background.
  4. The generated C# code will appear in the text area.
  5. 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.
  6. Click the "Copy Code" button.
  7. Paste the code into a new .cs file within your Reqnroll/Selenium test project.
  8. When you are finished using the tool, click the "Quit Application" button in the UI to stop the local server.

WebDriver Configuration Notes

  • The application currently attempts to instantiate the ChromeDriver by default. If you need to use a different browser, you will need to modify the PageObjectGeneratorService.cs file (specifically the WebDriver initialization part).
  • It expects the WebDriver executable to be found automatically, either alongside the application's .dll or in the system PATH. Explicit path configuration is not currently implemented via the UI.

Limitations

  • 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 use element.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.

Contributing

Contributions are welcome! Please feel free to fork the repository, create a feature branch, and submit a pull request.

License

(Optional: Specify a license here, e.g., MIT)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors