I have added 2 products in my basket. in the first step of my Test. In the last step I assert that same product that were added in the first step of the test comes in the Last step which is the "Order Summary page". Please find below the code and screen shots. Here there are 2 items, all the features of the 2 items displayed have same classes. just the indexing of the div is different. rest is the same.
I am using the Scenario Context functionality of the Specflow.
Mindwell, i want to achieve like this image , i have code currently for only 1 product, and i want to do the same for multiple products.
1) Basketpage. In this step i take all the elements of the page and take their values in the scenario context.
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 Page. In this step i assert the values , This is the order summary page.
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!!");
I am new to this stuff!! How to loop these and get the dynamic stuff. I want to check and verify each items Product name , price , quantity.
Doing this :
My Step File :
[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; }
}
Please Help! Thanks in Advance!!