gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:42 25 4
gpt4 key购买 nike

我在购物车中添加了 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) OrderSummary 页面。在此步骤中,我断言值,这​​是订单摘要页面。 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]);
}
}

篮子页面.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com