基于POM框架的C#产品折扣价验证页面对象实现需求
Got it, let's break down how to implement this discount price validation using the Page Object Model (POM) in C#. Here's a practical, maintainable solution tailored to your requirement:
Implementing Discount Price Validation with POM in C#
Step 1: Build the Product Details Page Object
First, we'll create a page object class that wraps all elements and validation logic for the product details page. This keeps test code focused on business rules instead of low-level element interactions.
using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; using System; using System.Linq; public class ProductDetailsPage { private readonly IWebDriver _driver; private readonly WebDriverWait _wait; // Adjust these locators to match your actual HTML elements private By RspPriceLocator => By.CssSelector(".product-rsp-price"); // Example: uses CSS class private By RequestedPriceInputLocator => By.Id("requested-price-input"); private By ValidationMessageLocator => By.Id("price-validation-error"); public ProductDetailsPage(IWebDriver driver) { _driver = driver; _wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); } // Helper to clean price strings (remove currency symbols/extra text) private decimal ParsePrice(string priceText) { // Handles formats like "$199.99", "£45.00", or "RSP: 120.00" var cleanedPrice = new string(priceText.Where(c => char.IsDigit(c) || c == '.').ToArray()); return decimal.TryParse(cleanedPrice, out decimal result) ? result : 0; } // Get the current RSP/MRP value from the page public decimal GetRspPrice() { var rspElement = _wait.Until(d => d.FindElement(RspPriceLocator)); return ParsePrice(rspElement.Text.Trim()); } // Enter a value into the Requested Price field public void EnterRequestedPrice(string price) { var inputField = _wait.Until(d => d.FindElement(RequestedPriceInputLocator)); inputField.Clear(); inputField.SendKeys(price); } // Core validation: Check if requested price meets 50% threshold public bool IsRequestedPriceValid(string enteredPrice) { var rspPrice = GetRspPrice(); var minimumAllowed = rspPrice * 0.5m; var parsedEnteredPrice = ParsePrice(enteredPrice); return parsedEnteredPrice >= minimumAllowed; } // Optional: Verify UI feedback after submission public bool DoesValidationMessageShowForInvalidPrice() { try { var errorElement = _wait.Until(d => d.FindElement(ValidationMessageLocator)); return errorElement.Text.Contains("Must be at least 50% of RSP"); } catch (WebDriverTimeoutException) { // No error message means price is valid return false; } } }
Step 2: Use the Page Object in Your Test Class
Now you can call the page object's methods from your test class to run validations. This keeps tests clean and easy to maintain.
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using NUnit.Framework; [TestFixture] public class ProductPriceValidationTests { private IWebDriver _driver; private ProductDetailsPage _productPage; [SetUp] public void TestSetup() { _driver = new ChromeDriver(); _driver.Navigate().GoToUrl("https://your-product-page-url.com"); // Replace with your actual URL _productPage = new ProductDetailsPage(_driver); } [Test] public void RequestedPrice_At50PercentOfRsp_ShouldPassValidation() { // Arrange var rspPrice = _productPage.GetRspPrice(); var validPrice = (rspPrice * 0.5m).ToString("0.00"); // Act _productPage.EnterRequestedPrice(validPrice); var isPriceValid = _productPage.IsRequestedPriceValid(validPrice); // Assert Assert.IsTrue(isPriceValid, "Price at 50% of RSP should be accepted"); } [Test] public void RequestedPrice_Below50PercentOfRsp_ShouldFailValidation() { // Arrange var rspPrice = _productPage.GetRspPrice(); var invalidPrice = (rspPrice * 0.49m).ToString("0.00"); // Act _productPage.EnterRequestedPrice(invalidPrice); var isPriceValid = _productPage.IsRequestedPriceValid(invalidPrice); var showsError = _productPage.DoesValidationMessageShowForInvalidPrice(); // Assert Assert.IsFalse(isPriceValid, "Price below 50% of RSP should be rejected"); Assert.IsTrue(showsError, "Error message should display for invalid price"); } [TearDown] public void TestTeardown() { _driver.Quit(); } }
Important Notes:
- Locator Tweaks: Update the element locators (like
RspPriceLocator) to match your actual HTML structure. Use browser dev tools to inspect elements and pick the most reliable locator (ID, CSS selector, or XPath). - Price Parsing: The
ParsePricemethod handles common currency formats, but adjust it if your page uses unique formatting (e.g., thousands separators like "1,200.00"). - UI Feedback: The
DoesValidationMessageShowForInvalidPricemethod is optional—use it if your page displays a visible error message when the price is too low.
内容的提问来源于stack exchange,提问作者Hargovind
相关产品推荐
相关产品推荐

