c# - 使用 Selenium webdriver + Specflow + C#+ 页面对象模式验证订单摘要页面中添加到购物车的多个产品的功能是否相同

标签 c# selenium-webdriver specflow pageobjects

我在我的购物篮中添加了 2 件产品。在我测试的第一步。 在最后一步中,我断言在测试的第一步中添加的相同产品出现在最后一步中,即“订单摘要页面”中。请在下面找到代码和屏幕截图。 这里有 2 个项目,显示的 2 个项目的所有特征都具有相同的类别。只是div的索引不同。其余的都是一样的。

我正在使用 Specflow 的场景上下文功能。

Mindwell,我想实现像这样的图像,我目前只有 1 个产品的代码,并且我想对多个产品执行相同的操作。

1) 购物篮页面。在此步骤中,我获取页面的所有元素并在场景上下文中获取它们的值。

enter image description here

string productname = pdpPage.getBrandName();
pdpPage.ExpandSideBar();
pdpPage.SelectProductQuantity(Quantity);
var hp = new HeaderPage(driver);
int currentBagQuantity = hp.getBagQuantity();
decimal currentTotalBagPrice = hp.getBagTotalPrice();

ScenarioContext.Current.Add("Product Name",productname);
ScenarioContext.Current.Add("QuantityAdded", int.Parse(Quantity));
ScenarioContext.Current.Add("BagQuantity", currentBagQuantity);
ScenarioContext.Current.Add("CurrentBagPrice", currentTotalBagPrice);
ScenarioContext.Current.Add("ProductPrice", pdpPage.getProductPriceInDecimal());

2) 订单摘要页面。在此步骤中,我断言值,这​​是订单摘要页面。 enter image description here

var os = new OrderSummaryPage(driver);
string brandname = os.getOrderProductName();
            int quantity = os.getOrderQuantity();
            decimal price = os.getOrderPrice();

Assert.IsTrue(brandname.Equals((string)ScenarioContext.Current["Product Name"]), "Err! Product is different!, on pdp is :" + ScenarioContext.Current["Product Name"] + "on order summary is" + brandname);
Assert.IsTrue(quantity.Equals((int)ScenarioContext.Current["QuantityAdded"]), "Err! Quantity is different from ordered!");
Assert.IsTrue(price.Equals((decimal)ScenarioContext.Current["ProductPrice"]), "Err! Product price is appearing to be different!");
Assert.IsTrue(GenericFunctions.isElementPresent(os.Delivery_Address), "Delivery Address details are not present");
Assert.IsTrue(GenericFunctions.isElementPresent(os.Billing_Address), "Billing Address details are not present!!");

我对这个东西很陌生!如何循环这些并获得动态的东西。我想检查并验证每件商品的产品名称、价格、数量。

这样做:

我的步骤文件:

[When(@"I check the items on basket page")]
        public void WhenICheckTheItemsOnBasketPage()
        {
            var bp = new BasketPage(driver);
            var h = bp.getLISTItemsFromOrderPage();
        for (int i = 0; i <= h.Count; i++)
        {
            ScenarioContext.Current.Add("item", h[i]);
        }
         }

BasketPage.cs

public IList getLISTItemsFromOrderPage()
        {
          List<BasketItems> orderProducts = new List<BasketItems>();
          var elements = (driver.FindElements(By.Id("basketitem")));

          foreach (IWebElement element in elements)
          {
            orderProducts.Add(CreateOrderProduct(element));
          }
          return orderProducts;
        }

public BasketItems CreateOrderProduct(IWebElement item)
                    {
                        return new BasketItems()
                       {
                           BrandName = item.FindElement(By.TagName("a")).Text.Trim(),
                           Quantity = GenericFunctions.DropDown_GetCurrentValue(item.FindElement(By.TagName("select"))),
                           Price = Convert.ToDecimal(item.FindElement(By.ClassName("col-md-2")).Text.Substring(1))
                       };
                    }

BasketItem.cs

public class BasketItems : BasketPageOR
    {
        public string BrandName { get; set; }
        public string Quantity { get; set; }
        public decimal Price { get; set; } 
    }

请帮忙!提前致谢!!

最佳答案

如果一个订单可以有多个产品,那么您的方法os.getOrderProductName();没有任何意义。

您应该有一个方法os.getOrderProducts();,它将返回OrderProduct对象的集合。它应该通过查找所有具有 id="productorderelement" 的元素(尽管您不应该拥有具有相同 id 的元素,但您实际上应该为此使用一个类)然后循环遍历每个元素来实现此目的提取信息来构建 OrderProduct 这样的事情应该允许您获取具有 id 的元素:

List<OrderProduct> orderProducts = new List<OrderProduct>();
var elements = (Driver.FindElements(By.XPath("//*[@id=\"productorderelement\"]")))
foreach (element in elements)
{
    orderProducts.Add(CreateOrderProduct(element));
}

public class OrderProduct
{
    public string BrandName{get;set;}
    public int Quantity{get;set;}
    public double Price{get;set;}    
}

public OrderProduct CreateOrderProduct(IWebElement element)
{
     return new OrderProduct()
     {
          BrandName= element.Something, //you need to extract the appropriate bit of the webelement that holds the brandname, quantity and price, but you don't show us the structure so I can't help there
          Quantity= element.FindElement(By.Class("quantity")).Text, //for example
          Price= element.GetAttribute("Price") //again another example
     }
}

关于c# - 使用 Selenium webdriver + Specflow + C#+ 页面对象模式验证订单摘要页面中添加到购物车的多个产品的功能是否相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484519/

相关文章:

c# - Thread.Abort() 损坏是否已本地化?

java - Selenium 2 Webdriver 与 JUnit 或 TestNG for PageObject 设计模式

c# - 生成 Nunit 和 Specflow 报告的 Cake 构建

c# - 是否可以以编程方式运行 specflow 测试?

c# - 为什么 "using System;"不被认为是不好的做法?

C# - 将不安全的 byte* 转换为 byte[]

c# - 是否可以在没有 Visual Studio 的情况下安装 C# 编译器?

java - 如何在没有 By 定位器的情况下使用 WebDriverWait.until?

java - TestNG - 为什么 testNG 报告器不打印出 else 语句?

c# - Selenium .NET core 2.2 发布的项目 exe 即使在 Windows x64 上也无法运行